indy IdTCPServer的一个疑问(100分)

  • 主题发起人 主题发起人 masm
  • 开始时间 开始时间
Event handler for peer thread execution.

property OnExecute: TIdServerThreadEvent;

Description

OnExecute is an event handler for TIdServerThreadEvents. OnExecute occurs when a TIdPeerThread attempts to perform the TIdPeerThread.Run method. OnExecute receives AThread as a parameter, representing the TIdPeerThread thread that will be started.

Assign a TIdServerThreadEvent event handler procedure to OnExecute to respond to the event notification.

Use CommandHandlers and CommandHandlersEnabled to provide finer control over commands executed for a peer thread connection.
-----
procedure TIdListenerThread.Run;
var
LIOHandler: TIdIOHandler;
LPeer: TIdTCPServerConnection;
LThread: TIdPeerThread;
begin
try
if Assigned(Server) then begin // This is temporary code just to test one exception
while True do begin
LThread := nil;
LPeer := TIdTCPServerConnection.Create(Server);
LIOHandler := Server.IOHandler.Accept(Binding.Handle, SELF);
if LIOHandler = nil then begin
FreeAndNil(LPeer);
Stop;
Exit;
end
else begin
LThread := TIdPeerThread(Server.ThreadMgr.GetThread);
LThread.FConnection := LPeer;
LThread.FConnection.IOHandler := LIOHandler;
LThread.FConnection.FFreeIOHandlerOnDisconnect := true;
end;

// LastRcvTimeStamp := Now; // Added for session timeout support
// ProcessingTimeout := False;
if (Server.MaxConnections > 0) and // Check MaxConnections
NOT TIdThreadSafeList(Server.Threads).IsCountLessThan(Server.MaxConnections)
then begin
Server.ThreadMgr.ActiveThreads.Remove(LThread);
LPeer.WriteRFCReply(Server.MaxConnectionReply);
LPeer.Disconnect;
FreeAndNil(LThread); // This will free both Thread and Peer.
end else begin
Server.Threads.Add(LThread); //APR
// Start Peer Thread
LThread.Start;
Break;
end;
end;
end;
except
on E: Exception do begin
if Assigned(LThread) then
FreeAndNil(LThread);
Server.DoListenException(Self, E);
end;
end;
End;

由上述源码可以看到,TCPServer每次侦听到一个连接,就会新建一个idPeerThread,
而当这个idPeerThread触发OnExecute事件的时候,就会调用IdTCPServer1Execute,
所以indy支持多进程是无疑的,而在IdTCPServer1Execute中一定要考虑同步问题
 
这么简单的问题还是想不通吗?我也郁闷了啊。zyxxyz 的代码我没细看,单看他下面的
说明就知道没错。明白了么?
 
茅塞顿开!!!
我以前也这么想过,但是有一个难题又让我推翻了,呵呵,傻啊!
 
多人接受答案了。
 
后退
顶部