Board index » cppbuilder » replacing ReadBuffer in Indy10

replacing ReadBuffer in Indy10


2008-03-21 12:25:29 AM
cppbuilder69
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
 
 

Re:replacing ReadBuffer in Indy10

"Dennis Cote" < XXXX@XXXXX.COM >wrote in message
Quote
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
 

Re:replacing ReadBuffer in Indy10

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!
Dennis Cote wrote:
Quote
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
 

{smallsort}

Re:replacing ReadBuffer in Indy10

"Colin B Maharaj" < XXXX@XXXXX.COM >wrote in message
Quote
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