Search code examples
delphidelphi-2009indy

TIdTCPServer not reading data from socket sometimes


I have the following code in the OnExecute of a TIdTCPServer (Delphi 2009 and Indy 10 that came with the installation) which is very similar to other examples on this site;

   Socket := AContext.Connection.Socket;
    if Socket.CheckForDataOnSource(10) then
    begin
      if not Socket.InputBufferIsEmpty then
      begin
        Socket.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);

        SetLength(Buffer, Length(RawBytes));
        Move(RawBytes[0], Buffer[1], Length(RawBytes));

        // Do stuff with data here...
      end;
    end;
    AContext.Connection.CheckForGracefulDisconnect;

It doesn't read the data in sometimes as CheckForDataOnSource(10) returns False. However if I stop the debugger at that line I can see the data I sent in the InputBuffer's bytes. Are there any other setup things I should do or other ways to force this to work all the time. This code is run bunch of times but always fails on the CheckForDataOnSource(10).

Also as a side note, I notice in code for Indy around the place that some people grab the AContext.Connection.IOHandler instead of the AContext.Connection.Socket and do the same things as the code above, what is the "right" one to use.

Thanks

Bruce


Solution

  • The code should be more like this:

    var
      IO: TIdIOHandler.
      Buffer: RawByteString;
    begin
      IO := AContext.Connection.IOHandler;
    
      if IO.InputBufferIsEmpty then
      begin
        IO.CheckForDataOnSource(10);
        if IO.InputBufferIsEmpty then Exit;
      end;
    
      IO.InputBuffer.ExtractToBytes(RawBytes, -1, False, -1);     
      // or: IO.ReadBytes(RawBytes, -1, False);
    
      SetLength(Buffer, Length(RawBytes));
      BytesToRaw(RawBytes, Buffer[1], Length(RawBytes));
      // Do stuff with Buffer here...
    end;