德尔福2009年,印第安纳波利斯10,TIdTCPServer.OnExecute,如何抢在INPUTBUFFER所有字节
-
23-08-2019 - |
题
我正在与印地与2009年的Delphi供给10瞎搞和因与从IOHandler时OnExecute火灾...
让所有的数据的麻烦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下一次运行它会拿起字节的正确数量,但为时已晚。
基本上我希望能够以刚才的所有数据,它的东西抢为UTF8字符串(的不的Unicode字符串),然后解析一个特殊的标记。所以我没有头和消息是可变长度。看来印第安纳波利斯10 IOHandlers不安装这个或我只是用错了。
这将是很好做这样的事情经过一定大小的缓冲区,填充它尽可能与实际回报填充的字节数,然后继续下去,如果有更多的。
顺便说一句什么TIdSchedulerOfFiber的状态,这看起来很有趣,它的工作原理?在使用它的人?我注意到它是不是在标准安装2009的Delphi虽然。
<强>更新强>我发现消息:= AContext.Connection.IOHandler.ReadLn(#0,enUTF8);它的作品,但我还是想知道答案对上述问题,是因为它是基于阻塞的IO?这使得更加热衷于此TIdSchedulerOfFiber。
解决方案
您不应该使用读()这样的。尝试代替以下内容:
procedure TFormMain.IdTCPServerExecute(AContext: TIdContext);
var
RxBuf: TIdBytes;
begin
RxBuf := nil;
with AContext.Connection.IOHandler do
begin
CheckForDataOnSource(10);
if not InputBufferIsEmpty then
begin
InputBuffer.ExtractToBytes(RxBuf);
// process RxBuf as needed...
end;
end;
end;
可替换地:
procedure TFormMain.IdTCPServerExecute(AContext: TIdContext);
var
RxBufStr: String; // not UTF8String
begin
with AContext.Connection.IOHandler do
begin
CheckForDataOnSource(10);
if not InputBufferIsEmpty then
begin
RxBufStr := InputBuffer.Extract(-1, enUtf8);
// Alternatively to above, you can set the
// InputBuffer.Encoding property to enUtf8
// beforehand, and then call TIdBuffer.Extract()
// without any parameters.
//
// Or, set the IOHandler.DefStringEncoding
// property to enUtf8 beforehand, and then
// call TIdIOHandler.InputBufferAsString()
// process RxBufStr as needed...
end;
end;
end;
至于TIdSchedulerOfFiber - 超核包是有效死在这个时候。它在很长一段时间一直没有得到治理,而不是上最新与最新的印第安纳波利斯10架构。我们不妨在日后复活它,但它不是我们在不久的将来计划。
其他提示
procedure TFormMain.IdTCPServerExecute(AContext: TIdContext);
var
RxBufStr: UTF8String;
RxBufSize: Integer;
begin
if AContext.Connection.IOHandler.Readable then
begin
AContext.Connection.IOHandler.ReadBytes(TBytes(RxBufStr),-1, False);
end;
end;
不隶属于 StackOverflow