datasnap 如何监控客户端的连接情况

type
pClientConns = ^TClientConns; // 客户连接
TClientConns = record
clientid: integer;
ip: string;
port: string;
logintime: TDateTime;
end;

type
G_ClientConnects: TDictionary<TIdTCPConnection, pClientConns>; // 客户端连接字典

procedure TServerContainer1.DSTCPServerTransport1Connect
(Event: TDSTCPConnectEventObject);
var
p: pClientConns;
begin
try
if G_ConnnectCount >= G_MaxConnNum then
begin
LogInfo('已超过系统授权的客户连接数');
TIdTCPConnection(Event.Connection).Disconnect;
exit;
end;
InterlockedIncrement(G_ConnnectCount);
New(p);
if Assigned(p) then
begin
p^.clientid := Event.Channel.ChannelInfo.Id;
p^.ip := Event.Channel.ChannelInfo.ClientInfo.IpAddress;
p^.port := Event.Channel.ChannelInfo.ClientInfo.ClientPort;
p^.logintime := Now;
G_ClientConnects.Add(TIdTCPConnection(Event.Connection), p);
PostMessage(Application.MainForm.Handle, WM_ADDUSER, wParam(p),
lParam(TIdTCPConnection(Event.Connection)));
end;
except
exit;
end;
end;

procedure TServerContainer1.DSTCPServerTransport1Disconnect
(Event: TDSTCPDisconnectEventObject);
var
p: pClientConns;
begin
try
if G_ConnnectCount >= 1 then
InterlockedDecrement(GlobalVar.G_ConnnectCount);
p := G_ClientConnects.Items[TIdTCPConnection(Event.Connection)];
if Assigned(p) then
begin
SendMessage(Application.MainForm.Handle, WM_DELUSER, wParam(p), 0);
G_ClientConnects.Remove(TIdTCPConnection(Event.Connection));
end;
except
exit;
end;
end;

procedure Tf_MainForm.AddUser(var msg: TMessage);
var
p: pClientConns;
begin
try
Label4.Caption := IntToStr(G_ConnnectCount);
p := pClientConns(msg.WParam);
if Assigned(p) then
begin
ClientDataSet1.Append;
ClientDataSet1.FieldByName('id').AsInteger := p^.clientid;
ClientDataSet1.FieldByName('ip').AsString := p^.ip;
ClientDataSet1.FieldByName('port').AsString := p^.port;
ClientDataSet1.FieldByName('time').AsDateTime := p^.logintime;
ClientDataSet1.FieldByName('conn').AsInteger := msg.LParam;
ClientDataSet1.Post;
end;
except
on E: Exception do
begin
LogInfo('Tf_MainForm.AddUser---' + E.Message);
exit;
end;
end;
end;

procedure Tf_MainForm.DelUser(var msg: TMessage);
var
p: pClientConns;
begin
try
Label4.Caption := IntToStr(G_ConnnectCount);
p := pClientConns(msg.WParam);
if Assigned(p) then
begin
if ClientDataSet1.FindKey([p^.clientid]) then
ClientDataSet1.Delete;
Dispose(p);
end;
except
on E: Exception do
begin
LogInfo('Tf_MainForm.DelUser---' + E.Message);
exit;
end;
end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值