Jump to content
alnickels

tidtcpserver read (peek) without removing from buffer.

Recommended Posts

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 in the buffer.

Share this post


Link to post

(copied from my answer to your same question on StackOverflow😞

 

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');
...

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×