Couple bug fixes in the client and server code. Gotta remember that TCP sockets may return less data than you ask for; that and the length of the data may be longer than the buffer you give it.
This commit is contained in:
@@ -112,7 +112,10 @@ namespace TestProtocol
|
|||||||
byte[] readBuffer = new byte[65536];
|
byte[] readBuffer = new byte[65536];
|
||||||
|
|
||||||
ProtocolOp operation;
|
ProtocolOp operation;
|
||||||
int length;
|
int messageLength;
|
||||||
|
int remaining;
|
||||||
|
int chunkLength;
|
||||||
|
int position;
|
||||||
|
|
||||||
while( this.running )
|
while( this.running )
|
||||||
{
|
{
|
||||||
@@ -132,10 +135,24 @@ namespace TestProtocol
|
|||||||
|
|
||||||
// Read the length
|
// Read the length
|
||||||
this.socket.Receive( readBuffer, 4, SocketFlags.None );
|
this.socket.Receive( readBuffer, 4, SocketFlags.None );
|
||||||
length = ByteWriter.ReadInt32_BE( readBuffer, 0 );
|
messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 );
|
||||||
|
|
||||||
|
if( readBuffer.Length < messageLength )
|
||||||
|
{
|
||||||
|
readBuffer = new byte[messageLength];
|
||||||
|
}
|
||||||
|
|
||||||
// Read the data
|
// Read the data
|
||||||
this.socket.Receive( readBuffer, length, SocketFlags.None );
|
// Keep in mind that Socket.Receive may return less data than asked for.
|
||||||
|
remaining = messageLength;
|
||||||
|
chunkLength = 0;
|
||||||
|
position = 0;
|
||||||
|
while( remaining > 0 )
|
||||||
|
{
|
||||||
|
chunkLength = this.socket.Receive( readBuffer, position, remaining, SocketFlags.None );
|
||||||
|
remaining -= chunkLength;
|
||||||
|
position += chunkLength;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch( SocketException e )
|
catch( SocketException e )
|
||||||
@@ -159,8 +176,8 @@ namespace TestProtocol
|
|||||||
|
|
||||||
if( this.Received != null )
|
if( this.Received != null )
|
||||||
{
|
{
|
||||||
byte[] dataCopy = new byte[length];
|
byte[] dataCopy = new byte[messageLength];
|
||||||
Array.Copy( readBuffer, 0, dataCopy, 0, length );
|
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
|
||||||
Message message = new Message( operation, dataCopy );
|
Message message = new Message( operation, dataCopy );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -149,7 +149,9 @@ namespace TestProtocol
|
|||||||
byte[] readBuffer = new byte[65536];
|
byte[] readBuffer = new byte[65536];
|
||||||
|
|
||||||
ProtocolOp operation;
|
ProtocolOp operation;
|
||||||
int length;
|
int messageLength;
|
||||||
|
int position;
|
||||||
|
int remaining;
|
||||||
|
|
||||||
while( this.running )
|
while( this.running )
|
||||||
{
|
{
|
||||||
@@ -157,7 +159,7 @@ namespace TestProtocol
|
|||||||
{
|
{
|
||||||
// |--4 bytes--|--4 bytes--|---N--|
|
// |--4 bytes--|--4 bytes--|---N--|
|
||||||
// Every command is a TLV - | Operation | Length | Data |
|
// Every command is a TLV - | Operation | Length | Data |
|
||||||
|
int chunkLength;
|
||||||
|
|
||||||
// Read the operation.
|
// Read the operation.
|
||||||
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
|
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
|
||||||
@@ -169,11 +171,24 @@ namespace TestProtocol
|
|||||||
|
|
||||||
// Read the length
|
// Read the length
|
||||||
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
|
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
|
||||||
length = ByteWriter.ReadInt32_BE( readBuffer, 0 );
|
messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 );
|
||||||
|
|
||||||
|
if( readBuffer.Length < messageLength )
|
||||||
|
{
|
||||||
|
readBuffer = new byte[messageLength];
|
||||||
|
}
|
||||||
|
|
||||||
// Read the data
|
// Read the data
|
||||||
this.readSocket.Receive( readBuffer, length, SocketFlags.None );
|
// Keep in mind that Socket.Receive may return less data than asked for.
|
||||||
|
remaining = messageLength;
|
||||||
|
chunkLength = 0;
|
||||||
|
position = 0;
|
||||||
|
while( remaining > 0 )
|
||||||
|
{
|
||||||
|
chunkLength = this.readSocket.Receive( readBuffer, position, remaining, SocketFlags.None );
|
||||||
|
remaining -= chunkLength;
|
||||||
|
position += chunkLength;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch( SocketException e )
|
catch( SocketException e )
|
||||||
{
|
{
|
||||||
@@ -196,8 +211,8 @@ namespace TestProtocol
|
|||||||
|
|
||||||
if( this.Received != null )
|
if( this.Received != null )
|
||||||
{
|
{
|
||||||
byte[] dataCopy = new byte[length];
|
byte[] dataCopy = new byte[messageLength];
|
||||||
Array.Copy( readBuffer, 0, dataCopy, 0, length );
|
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
|
||||||
Message message = new Message( operation, dataCopy );
|
Message message = new Message( operation, dataCopy );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user