Jun*_*oor 5 delphi indy delphi-7 tcpserver indy10
经过大量的搜索,我认为Indy TCP服务器将是我正在使用的Instant Messenger服务器上最好的.我现在面临的唯一问题是向其他连接的客户端广播和转发消息,向同一客户端发回响应似乎没问题并且不会挂起其他客户端活动,但是为了将消息转发给其他客户端我知道的机制是通过使用aContext.locklist,并在连接列表之间进行迭代,以找到要接收数据的客户端连接.
我认为这里的问题是它会冻结列表,并且在调用unlocklist之前不会处理其他客户端请求.那么它不会损害服务器的性能吗?锁定列表并在连接之间进行迭代以转发每条消息(因为这是在信使中经常发生的事情).有没有更好的方法来做到这一点?
我使用的是Indy 10和Delphi 7
广播代码:
Var tmpList: TList;
i: Integer;
Begin
tmpList := IdServer.Contexts.LockList;
For i := 0 to tmpList.Count Do Begin
TIdContext(tmpList[i]).Connection.Socket.WriteLn('Broadcast message');
End;
IdServer.Contexts.UnlockList;
Run Code Online (Sandbox Code Playgroud)
转发邮件的代码:
Var tmpList: TList;
i: Integer;
Begin
tmpList := IdServer.Contexts.LockList;
For i := 0 to tmpList.Count Do Begin
If TIdContext(tmpList[i]).Connection.Socket.Tag = idReceiver Then
TIdContext(tmpList[i]).Connection.Socket.WriteLn('Message');
End;
IdServer.Contexts.UnlockList;
Run Code Online (Sandbox Code Playgroud)
Rem*_*eau 10
是的,您必须遍历Contexts列表才能向多个客户端广播消息.但是,您不(也不应该)从循环内部执行实际写入.其中一个,正如您已经注意到的那样,通过保持列表锁定一段时间可以影响服务器性能.二,它不是线程安全的.如果您的循环将数据写入连接而另一个线程同时写入同一连接,则这两个写入将相互重叠并破坏与该客户端的通信.
我通常做的是实现每个客户端的出站队列,使用TIdContext.Data属性或TIdServerContext后代来保存实际队列.当您需要从该客户端OnExecute事件外部向客户端发送数据时,请将数据放入该客户端的队列中.OnExecute然后,客户端的事件可以在安全的情况下将队列的内容发送到客户端.
例如:
type
TMyContext = class(TIdServerContext)
public
Tag: Integer;
Queue: TIdThreadSafeStringList;
...
constructor Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TThreadList = nil); override;
destructor Destroy; override;
end;
constructor TMyContext.Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TThreadList = nil);
begin
inherited;
Queue := TIdThreadSafeStringList.Create;
end;
destructor TMyContext.Destroy;
begin
Queue.Free;
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
.
procedure TForm1.FormCreate(Sender: TObject);
begin
IdServer.ContextClass := TMyContext;
end;
procedure TForm1.IdServerConnect(AContext: TIdContext);
begin
TMyContext(AContext).Queue.Clear;
TMyContext(AContext).Tag := ...
end;
procedure TForm1.IdServerDisconnect(AContext: TIdContext);
begin
TMyContext(AContext).Queue.Clear;
end;
procedure TForm1.IdServerExecute(AContext: TIdContext);
var
Queue: TStringList;
tmpList: TStringList;
begin
...
tmpList := nil;
try
Queue := TMyContext(AContext).Queue.Lock;
try
if Queue.Count > 0 then
begin
tmpList := TStringList.Create;
tmpList.Assign(Queue);
Queue.Clear;
end;
finally
TMyContext(AContext).Queue.Unlock;
end;
if tmpList <> nil then
AContext.Connection.IOHandler.Write(tmpList);
finally
tmpList.Free;
end;
...
end;
Run Code Online (Sandbox Code Playgroud)
.
var
tmpList: TList;
i: Integer;
begin
tmpList := IdServer.Contexts.LockList;
try
for i := 0 to tmpList.Count-1 do
TMyContext(tmpList[i]).Queue.Add('Broadcast message');
finally
IdServer.Contexts.UnlockList;
end;
end;
Run Code Online (Sandbox Code Playgroud)
.
var
tmpList: TList;
i: Integer;
begin
tmpList := IdServer.Contexts.LockList;
try
for i := 0 to tmpList.Count-1 do
begin
if TMyContext(tmpList[i]).Tag = idReceiver then
TMyContext(tmpList[i]).Queue.Add('Message');
end;
finally
IdServer.Contexts.UnlockList;
end;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7574 次 |
| 最近记录: |