Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

replacing ReadBuffer in Indy10

220 views
Skip to first unread message

Dennis Cote

unread,
Mar 20, 2008, 12:25:29 PM3/20/08
to
I am porting an application from Indy 9 to Indy 10.

In Indy 9 I had code to read data into a dynamically allocated char
buffer using ReadBuffer.

char* Buffer;
int Length;

...
ReadBuffer(Buffer, Length);
...

I have found that I need to do the following instead in Indy 10.

char* Buffer;
int Length;

...
TIdBytes byteBuf;
byteBuf.Length = Length;
IOHandler->ReadBytes(byteBuf, Length);
BytesToRaw(byteBuf, Buffer, Length);
...

My question is, is there a way to get Indy 10 to read directly into the
existing char buffer? I would like to avoid the extra allocations and
the buffer copy if possible.

TIA
Dennis Cote

Remy Lebeau (TeamB)

unread,
Mar 20, 2008, 12:43:28 PM3/20/08
to

"Dennis Cote" <denni...@gmail.com> wrote in message
news:47e28ff7$1...@newsgroups.borland.com...

> is there a way to get Indy 10 to read directly into the existing char
> buffer?

Indy does not support direct memory buffers anymore, by design. One thing
you can do, though, is derive a new class from TCustomMemoryStream that uses
the char* buffer as its underlying memory block. You could then use
ReadStream() to receive the data directly into the buffer. For example:

class TBufferStream : public TCustomMemoryStream
{
public:
__fastcall TBufferStream(void *Buffer, int Length)
: TCustomMemoryStream()
{
SetPointer(Buffer, Length);
}

int __fastcall Write(void *Buffer, int Count)
{
Count = min(Count, Size-Position);
if( Count > 0 )
{
memcpy(((LPBYTE)Memory) + Position, Buffer, Count);
Seek(Count, soFromCurrent);
return Count;
}
return 0;
}
};


char* Buffer;
int Length;
...

TBufferStream *strm = new TBufferStream(Buffer, Length);
IOHandler->ReadStream(strm, Length);
delete strm;


Gambit


Colin B Maharaj

unread,
Mar 22, 2008, 8:19:09 PM3/22/08
to
What you can also probably do, as I did using indy 9
(sorry I do not use indy 10 yet) is get the socket handle
similar to this...
int isocket = AThread->Connection->Socket->Binding->Handle;
and do this....
unsigned char * s = new unsigned char [8200];
int r = recv(isocket, s, 8192, 0);
if r == 0 then do this.....
AThread->Connection->Disconnect();
and terminate else if
r == -1
this is an error so go look up WSAGetLastError();
and if r > 0 then you got data in s and r is the
length!

Remy Lebeau (TeamB)

unread,
Mar 24, 2008, 12:33:39 PM3/24/08
to

"Colin B Maharaj" <nor...@myhost.com> wrote in message
news:47e5a1fb$1...@newsgroups.borland.com...

> What you can also probably do, as I did using indy 9
> (sorry I do not use indy 10 yet) is get the socket handle
> similar to this...

You shouldn't be doing that. That completely bypasses Indy's ability to
manage and track the socket state.


Gambit


0 new messages