【发布时间】:2010-10-07 08:24:08
【问题描述】:
我正在搞乱 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;
AContext.Connection.IOHandler.InputBuffer.Size 似乎不可靠并且经常返回 0,但在下次通过 OnExecute 运行时,它会获取正确数量的字节,但为时已晚。
基本上我希望能够抓取所有数据,将其填充到 UTF8String(不是 Unicode 字符串)中,然后解析特殊标记。所以我没有标题,消息是可变长度的。似乎 Indy 10 IOHandlers 没有为此设置,或者我只是用错了。
最好做一些事情,比如传递一个特定大小的缓冲区,尽可能多地填充它,然后返回实际填充的字节数,如果还有更多,就继续。
顺便说一句,TIdSchedulerOfFiber 的状态如何,这看起来很有趣,它有效吗?有人在用吗?我注意到它不在 Delphi 2009 的标准安装中。
更新: 我找到了 Msg := AContext.Connection.IOHandler.ReadLn(#0, enUTF8);哪个可行,但我仍然想知道上述问题的答案,是因为它是基于阻塞IO的吗?这使得 TIdSchedulerOfFiber 更加热衷。
【问题讨论】:
标签: delphi delphi-2009 indy