Skip to content

Commit

Permalink
Marked the code at the 0.1 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
antiduh committed Jul 8, 2014
1 parent b65b6c6 commit 5463bea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 69 deletions.
8 changes: 3 additions & 5 deletions TestClient/ClientForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 6 additions & 24 deletions TestProtocol/CustomConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ private void ReadLoop()
byte[] readBuffer = new byte[65536];

ProtocolOp operation;
int messageLength;
int remaining;
int chunkLength;
int position;
int length;

while( this.running )
{
Expand All @@ -135,33 +132,18 @@ private void ReadLoop()

// Read the length
this.socket.Receive( readBuffer, 4, SocketFlags.None );
messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 );

if( readBuffer.Length < messageLength )
{
readBuffer = new byte[messageLength];
}
length = ByteWriter.ReadInt32_BE( readBuffer, 0 );

// 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.socket.Receive( readBuffer, position, remaining, SocketFlags.None );
remaining -= chunkLength;
position += chunkLength;
}
this.socket.Receive( readBuffer, length, SocketFlags.None );

}
catch( SocketException e )
{
if( e.SocketErrorCode == SocketError.ConnectionAborted ||
e.SocketErrorCode == SocketError.Interrupted ||
e.SocketErrorCode == SocketError.OperationAborted ||
e.SocketErrorCode == SocketError.Shutdown ||
e.SocketErrorCode == SocketError.ConnectionReset )
e.SocketErrorCode == SocketError.Shutdown )
{
// Shutting down.
break;
Expand All @@ -176,8 +158,8 @@ private void ReadLoop()

if( this.Received != null )
{
byte[] dataCopy = new byte[messageLength];
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
byte[] dataCopy = new byte[length];
Array.Copy( readBuffer, 0, dataCopy, 0, length );
Message message = new Message( operation, dataCopy );

try
Expand Down
49 changes: 22 additions & 27 deletions TestProtocol/CustomServer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using NSspi;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProtocol
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NSspi;

public class CustomServer
{
private Thread receiveThread;
Expand Down Expand Up @@ -139,17 +149,15 @@ private void ReadLoop()
byte[] readBuffer = new byte[65536];

ProtocolOp operation;
int messageLength;
int position;
int remaining;
int length;

while( this.running )
{
try
{
// |--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 );
Expand All @@ -161,24 +169,11 @@ private void ReadLoop()

// Read the length
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 );

if( readBuffer.Length < messageLength )
{
readBuffer = new byte[messageLength];
}
length = ByteWriter.ReadInt32_BE( readBuffer, 0 );

// 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;
}
this.readSocket.Receive( readBuffer, length, SocketFlags.None );

}
catch( SocketException e )
{
Expand All @@ -201,8 +196,8 @@ private void ReadLoop()

if( this.Received != null )
{
byte[] dataCopy = new byte[messageLength];
Array.Copy( readBuffer, 0, dataCopy, 0, messageLength );
byte[] dataCopy = new byte[length];
Array.Copy( readBuffer, 0, dataCopy, 0, length );
Message message = new Message( operation, dataCopy );

try
Expand Down
22 changes: 10 additions & 12 deletions TestServer/ServerForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion TestServer/ServerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void server_Received( Message message )

private void server_Disconnected()
{
this.running = true;
this.running = false;
this.initializing = true;
this.connected = false;

Expand Down

0 comments on commit 5463bea

Please sign in to comment.