al nickels
al nickels

Reputation: 1

Delphi Tidtcpserver with a peek from the buffer

How can I read a message from a tidtcpserver context without removing it from the read buffer? I want to preview the message and leave it where it is.

Upvotes: 0

Views: 220

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596256

Indy is not really designed for peeking data, it would rather that you read whole data, letting it block until the requested data has arrived in full.

That being said, TIdBuffer does have a PeekByte() method:

function PeekByte(AIndex: Integer): Byte;
var
  B: Byte;

if AContext.Connection.IOHandler.InputBuffer.Size > 0 then
begin
  B := AContext.Connection.IOHandler.InputBuffer.PeekByte(0);
  ...
end;

Or, if you are looking for something in particular in the buffer (ie, a message delimiter, etc), TIdBuffer has several overloaded IndexOf() methods:

function IndexOf(const AByte: Byte; AStartPos: Integer = 0): Integer; overload;
function IndexOf(const ABytes: TIdBytes; AStartPos: Integer = 0): Integer; overload;
function IndexOf(const AString: string; AStartPos: Integer = 0;
  AByteEncoding: IIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ASrcEncoding: IIdTextEncoding = nil{$ENDIF}
): Integer; overload;    
var
  Index: Integer;

Index := AContext.Connection.IOHandler.InputBuffer.IndexOf(SingleByte);
Index := AContext.Connection.IOHandler.InputBuffer.IndexOf(ArrayOfBytes);
Index := AContext.Connection.IOHandler.InputBuffer.IndexOf('string');
...

Upvotes: 1

Related Questions