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:
antiduh
2014-07-08 18:56:52 +00:00
parent eee7b16b2c
commit 35ace4f24c
2 changed files with 44 additions and 12 deletions

View File

@@ -112,7 +112,10 @@ namespace TestProtocol
byte[] readBuffer = new byte[65536];
ProtocolOp operation;
int length;
int messageLength;
int remaining;
int chunkLength;
int position;
while( this.running )
{
@@ -132,10 +135,24 @@ namespace TestProtocol
// Read the length
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
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 )
@@ -159,8 +176,8 @@ namespace TestProtocol
if( this.Received != null )
{
byte[] dataCopy = new byte[length];
Array.Copy( readBuffer, 0, dataCopy, 0, length );
byte[] dataCopy = new byte[messageLength];
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
Message message = new Message( operation, dataCopy );
try

View File

@@ -149,7 +149,9 @@ namespace TestProtocol
byte[] readBuffer = new byte[65536];
ProtocolOp operation;
int length;
int messageLength;
int position;
int remaining;
while( this.running )
{
@@ -157,7 +159,7 @@ namespace TestProtocol
{
// |--4 bytes--|--4 bytes--|---N--|
// Every command is a TLV - | Operation | Length | Data |
int chunkLength;
// Read the operation.
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
@@ -169,11 +171,24 @@ namespace TestProtocol
// Read the length
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
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 )
{
@@ -196,8 +211,8 @@ namespace TestProtocol
if( this.Received != null )
{
byte[] dataCopy = new byte[length];
Array.Copy( readBuffer, 0, dataCopy, 0, length );
byte[] dataCopy = new byte[messageLength];
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
Message message = new Message( operation, dataCopy );
try