我正在搞乱Delphi 2009提供的Indy 10,并且在OnExecute触发时无法从IOHandler获取所有数据......
procedure TFormMain.IdTCPServerExecute(AContext: TIdContext);
var
RxBufStr: UTF8String;
RxBufSize: Integer;
begin
if AContext.Connection.IOHandler.Readable then
begin
RxBufSize := AContext.Connection.IOHandler.InputBuffer.Size;
if RxBufSize > 0 then
begin
SetLength(RxBufStr, RxBufSize);
AContext.Connection.IOHandler.ReadBytes(TBytes(RxBufStr), RxBufSize, False);
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
AContext.Connection.IOHandler.InputBuffer.Size似乎不可靠并经常返回0,但是在下一次运行时,OnExecute它将获取正确的字节数,但为时已晚.
基本上我希望能够只获取所有数据,将其填充到UTF8String(不是 Unicode字符串),然后解析一个特殊的标记.所以我没有标题,消息长度可变.似乎Indy 10 IOHandlers没有为此设置,或者我只是错误地使用它.
做一些像传递一定大小的缓冲区,尽可能多地填充它并返回实际填充的字节数然后继续运行(如果还有更多)会很好.
除了TIdSchedulerOfFiber的状态之外,这看起来非常有趣,它有用吗?有人用吗?我注意到它不在Delphi 2009的标准安装中.
更新:我发现Msg:= AContext.Connection.IOHandler.ReadLn(#0,enUTF8); 哪个有效,但我仍然想知道上述问题的答案,是因为它是基于阻止IO吗?这使得TIdSchedulerOfFiber更加热衷于此.
我\xc2\xb4m是Delphi的新手,我\xc2\xb4m尝试进行一些网络操作。在本例中,我想连接到(让\xc2\xb4s 称之为)通知服务器,该服务器将在发生某些事件时发送字符串。
\n\n我的第一种方法是这样的:\n我在自己的线程上运行 TIdTCPClient 并设置 ReadTimeout,这样我\xc2\xb4m 就不会总是被阻塞。这样我可以检查线程的终止状态。
\n\nConnectionToServer.ReadTimeout := MyTimeOut;\nwhile( Continue ) do\nbegin\n //\n try\n Command := ConnectionToServer.ReadLn( );\n except\n on E: EIdReadTimeout do\n begin\n //AnotarMensaje(odDepurar, 'Timeout ' + E.Message );\n end;\n on E: EIdConnClosedGracefully do\n begin\n AnotarMensaje(odDepurar, 'Conexi\xc3\xb3n cerrada ' + E.Message );\n Continue := false;\n end;\n on E: Exception do\n begin\n AnotarMensaje(odDepurar, 'Error en lectura ' + E.Message );\n Continue := false;\n end;\n end;\n // treat the command\n ExecuteRemoteCommand( Command ); \n if( self.Terminated ) then\n …Run Code Online (Sandbox Code Playgroud)