IdTCPServer1Execute事件在什么情况下被触发
我用的indy 10.1.6,请问IdTCPServer1Execute事件在什么条件下才被触发?
1、在一次connect之后触发一次IdTCPServer1Execute事件。
2、服务器端收到一个TCP数据包就触发一次IdTCPServer1Execute事件。
3、服务器端收到一个带有数据部分的数据包就触发一次IdTCPServer1Execute事件。
还有在其他的情况下被触发?
[最优解释]
Summary
Event handler used to execute the task for a client connection.
Description
OnExecute is a TIdServerThreadEvents event handler used to execute the task for a client connection to the server. OnExecute is signalled when the Scheduler executes the thread or fiber associated with the client connection.
OnExecute receives a TIdContext argument that represents the task for the client connection, and contains a TIdTCPConnection instance for the client connection, and the thread or fiber that controls execution of the task. A protected procedure in the server that triggers OnExecute is assigned to the TIdContext, and eventually the thread or fiber acquired from the Scheduler for the server.
Use OnExecute to control the interaction between the client and the server during the lifetime of the client connection. This includes reading and/or writing commands and/or data supported in exchanges between the client and server. TIdTCPServer does not implement support for any given protocol used for communication exchanges between clients and the server.
When the thread or fiber is running, the OnExecute event handler is triggered inside a loop that continues to execute the event handler until the client is disconnected. This allows the OnExecute event handler procedure to be written in a straight-forward manner, and still yield to the Scheduler for the server.
The event handler can be used to interact with the client connection by read a command and/or writing a response dependent on the protocol in use for the session. When the command and/or response is handled, the event handler should be exited to allow the Scheduler to respond other client connections to the server. Using loops or polling inside the OnExecute event handler should be avoided to reduce the chance of interferring with the Scheduler for the server.
The preferred way to interrupt execution of the looped OnExecute event handler is to call the TIdContext.Connection.Disconnect method for the context instance.
For example:
TMyForm.MyServerExecute(AContext: TIdContext);
var
lCmd: string;
begin
lCmd := Trim(AContext.Connection.IOHandler.ReadLn);
if AnsiSameText(lCmd, 'HELP') then
begin
AContext.Connection.IOHandler.WriteLn('HELP');
AContext.Connection.IOHandler.WriteLn('QUIT');
AContext.Connection.IOHandler.WriteLn('GETTIMESTAMP');
AContext.Connection.IOHandler.WriteLn('');
end
else if AnsiSameText(lCmd, 'QUIT') then
begin
AContext.Connection.IOHandler.WriteLn('Goodbye...');
AContext.Connection.IOHandler.WriteLn('');
AContext.Connection.Disconnect;
end
else if AnsiSameText(lCmd, 'GETTIMESTAMP') then
begin
AContext.Connection.IOHandler.WriteLn(
FormatDateTime(Now, 'yyyy-mm-ddThh:nn:ss.zzz'));
AContext.Connection.IOHandler.WriteLn('');
end;
end;
Exceptions raised in the OnExecute event handler are caught in the executing thread or fiber. If the exception cannot be handled by the thread or fiber, it is re-raised and causes termination of the thread or fiber.
Use OnConnect to perform actions for the client after it is connected and prior to execution in the OnExecute event handler.
Use OnDisconnect to perform actions for the client after is has been disconnected, and exited from the OnExecute event handler.
Assign a TIdServerThreadEvent event handler procedure to OnExecute to respond to the event notification.
See Also
TIdServerThreadEvents
Class
TIdTCPServer
[其他解释]
只要有客户发送数据,就会激活 Server 的 OnExecute 事件
http://blog.csdn.net/gencheng/article/details/7275230