Delphi 2009, Indy 10, Tidtcpserver.onexecute, 입력 부퍼의 모든 바이트를 잡는 방법

StackOverflow https://stackoverflow.com/questions/544204

  •  23-08-2019
  •  | 
  •  

문제

나는 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에 넣을 수 있기를 원합니다 (~ 아니다 유니 코드 문자열) 다음 특수 마커를 위해 구문 분석. 따라서 헤더가없고 메시지는 가변 길이입니다. Indy 10 Iohandlers가 이것을 위해 설정되지 않았거나 그냥 잘못 사용하고있는 것 같습니다.

특정 크기의 버퍼를 통과하고 가능한 한 많이 채우고 실제로 채워진 바이트 수를 반환 한 다음 더 많은 경우 계속 진행하는 것이 좋을 것입니다.

TidschedulerOffiber의 상태는 무엇입니까? 이것은 매우 흥미로워 보입니다. 누구든지 그것을 사용하고 있습니까? 그래도 Delphi 2009의 표준 설치에 있지 않다는 것을 알았습니다.

업데이트: msg를 찾았습니다 : = 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의 경우 - 현재 슈퍼 코어 패키지는 효과적으로 죽었습니다. 그것은 오랫동안 진행되지 않았으며 최신 Indy 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; 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top