diff --git a/TestProtocol/CustomConnection.cs b/TestProtocol/CustomConnection.cs index 274b67f..1f26baf 100644 --- a/TestProtocol/CustomConnection.cs +++ b/TestProtocol/CustomConnection.cs @@ -112,7 +112,10 @@ private void ReadLoop() byte[] readBuffer = new byte[65536]; ProtocolOp operation; - int length; + int messageLength; + int remaining; + int chunkLength; + int position; while( this.running ) { @@ -132,10 +135,24 @@ private void ReadLoop() // 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 @@ private void ReadLoop() 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 diff --git a/TestProtocol/CustomServer.cs b/TestProtocol/CustomServer.cs index 103407d..60a6824 100644 --- a/TestProtocol/CustomServer.cs +++ b/TestProtocol/CustomServer.cs @@ -149,7 +149,9 @@ private void ReadLoop() byte[] readBuffer = new byte[65536]; ProtocolOp operation; - int length; + int messageLength; + int position; + int remaining; while( this.running ) { @@ -157,7 +159,7 @@ private void ReadLoop() { // |--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 @@ private void ReadLoop() // Read the length this.readSocket.Receive( readBuffer, 4, SocketFlags.None ); - length = ByteWriter.ReadInt32_BE( readBuffer, 0 ); + messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 ); - // Read the data - this.readSocket.Receive( readBuffer, length, SocketFlags.None ); + if( readBuffer.Length < messageLength ) + { + readBuffer = new byte[messageLength]; + } + // Read the data + // 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 @@ private void ReadLoop() 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