Buffering example for Indy 10

在国外论坛看到一段关于Indy 10 操作Buffer的文章,记录下。

CLIENT (Delphi)

{ vBufferIn: TIdBytes; }
{ vBufferOut: TIdBytes; }

{ Set the length of the buffer for the request }
SetLength(vBufferIn,length(pInputXML));
StringSize := Length(pInputXML);
{ Load the bytes array }
for i := 0 to StringSize - 1 do
begin
vBufferIn[i] := Byte(pInputXML[i+1]);
end;
{ Send the request }
FSocket.IOHandler.Write(vBufferIn);

BufferSize := 0;
TotalBufferSize := 0;
{ Set the length as zero means set as dynamic and leaving
Indy to work with the size of the array. }
SetLength(vBufferOut,0);
{ Loop while the socket is connected }
while (FSocket.Connected) do
begin
{ fill the output buffer with a timeout of 10 ms }
FSocket.IOHandler.CheckForDataOnSource(10);
{ Returns true if the output buffer is currently empty }
if not (FSocket.IOHandler.InputBufferIsEmpty) then
begin
{ get the size of the output buffer }
BufferSize := FSocket.IOHandler.InputBuffer.Size;
{ save the size of the output buffer and add it to the total for the array }
TotalBufferSize := TotalBufferSize + BufferSize;
{ read the output buffer and save it into the array }
{ N.B the last parameter is very important is the AAppend and if you
set as true means the buffer is added at the end otherwise if set to false
the buffer cover the array from the begin. }
FSocket.IOHandler.ReadBytes(vBufferOut, BufferSize, true);
end;
end;
{ moves the byte array into the string }
pOutPutXML := BytesToString(vBufferOut, 0, TotalBufferSize);

+------------------------------------------------------------+

CLIENT (C#) (don't care about base64 this is only because this client is a web service and the SOAP
envelope consumed by a WIN32 client SOAP had encoding problems.)

Byte[] RecvBytes = new Byte[0];
Byte[] OutRecvBytes = new Byte[0];


// Decode the input into a bytes array converting the string
// representation of a value of base 64 digits to an equivalent
// array of 8-bit unsigned integers
RecvBytes = System.Convert.FromBase64String(InputXML);
// Send the request
mySocket.IOHandler.Write(RecvBytes);
// Receive response
int BufferSize = 0;
// Loop until the socket is connected
while (mySocket.Connected())
{
// fill the input buffer with a timeout of 10 ms
mySocket.IOHandler.CheckForDataOnSource(10);
// Returns true if the input buffer is currently empty
if (! mySocket.IOHandler.InputBufferIsEmpty())
{
// get the size of the input buffer
BufferSize = mySocket.IOHandler.InputBuffer.Size;
// Read the input buffer and save it into the array
// N.B the last parameter is very important is the AAppend and if you
// set as true means the buffer is added at the end otherwise if set to false
// the buffer cover the array from the begin
mySocket.IOHandler.ReadBytes(ref OutRecvBytes, BufferSize, true);
} // end if
} // end while (mySocket.Connected())
// Converts the value of an array of 8-bit unsigned integers to equivalent String
// representation consisting of base 64 digits.
OutputXML = System.Convert.ToBase64String(OutRecvBytes,0,OutRecvBytes.Length);

+------------------------------------------------------------+

SERVER CONNECTION

{ vBuffer: TIdBytes; }
{ vInputData: WideString }

{ Get the data }
BufferSize := 0;
TotalBufferSize := 0;
{ We must wait for a request }
{ Setting the length as zero means set as dynamic and leaving
Indy to work with the size of the array. }
SetLength(vBuffer,0);
{ fill the input buffer with a timeout of 10 ms }
vTET.Connection.IOHandler.CheckForDataOnSource(10);
{ loop until the input buffer is empty }
repeat
{ Returns true if the input buffer is currently empty }
if not (vTET.Connection.IOHandler.InputBufferIsEmpty()) then
begin
{ get the size of the input buffer }
BufferSize := vTET.Connection.IOHandler.InputBuffer.Size;
{ save the size of the input buffer and add it to the total for the array }
TotalBufferSize := TotalBufferSize + BufferSize;
{ read the input buffer and save it into the array }
{ N.B the last parameter is very important is the AAppend and if you
set as true means the buffer is added at the end otherwise if set to false
the buffer cover the array from the begin. }
vTET.Connection.IOHandler.ReadBytes(vBuffer, BufferSize, true);
end;
vTET.Connection.IOHandler.CheckForDataOnSource(10);
until (vTET.Connection.IOHandler.InputBufferIsEmpty());
{ moves the byte array into the string }
vInputData := BytesToString(vBuffer, 0, TotalBufferSize);

+------------------------------------------------------------+

SERVER EXECUTE

{ vBuffer: TIdBytes; }
{ vOutputXML: WideString }

{ Send the data }
SetLength(vBuffer,length(vOutputXML));
for i := 0 to length(vOutputXML) -1 do
begin
vBuffer[i] := Byte(vOutputXML[i+1]);
end;
IOHandler.Write(vBuffer);

你可能感兴趣的:(Delphi)