Implemented the server form. Client <-> Server interaction actually works.

Fixed a few bugs in the CustomServer - wasn't configuring the listen socket correctly, doing read/write operations from the wrong socket, etc. Reworked how the CustomConnection class handles serialization.

Added Disconnected events to the sample's CustomConnection and CustomServer classes so the forms could reset state.
This commit is contained in:
antiduh
2014-06-26 18:00:50 +00:00
parent 81ed80a4d0
commit 5b3a92ee66
8 changed files with 626 additions and 17 deletions

View File

@@ -27,6 +27,8 @@ namespace TestProtocol
public event ReceivedAction Received;
public event Action Disconnected;
public void StartClient( string server, int port )
{
if( this.running )
@@ -49,7 +51,7 @@ namespace TestProtocol
{
if( this.running == false )
{
throw new InvalidOperationException( "Already stopped" );
return;
}
this.socket.Close();
@@ -64,13 +66,19 @@ namespace TestProtocol
}
byte[] outBuffer = new byte[ message.Data.Length + 8 ];
int position = 0;
ByteWriter.WriteInt32_BE( (int)message.Operation, outBuffer, 0 );
ByteWriter.WriteInt32_BE( message.Data.Length, outBuffer, 4 );
ByteWriter.WriteInt32_BE( (int)message.Operation, outBuffer, position );
position += 4;
Array.Copy( message.Data, 0, outBuffer, 8, message.Data.Length );
ByteWriter.WriteInt32_BE( message.Data.Length, outBuffer, position );
position += 4;
Array.Copy( message.Data, 0, outBuffer, position, message.Data.Length );
this.socket.Send( outBuffer, 0, outBuffer.Length, SocketFlags.None );
Console.Out.WriteLine( "Client: Sent " + message.Operation );
}
private void ReceiveThreadEntry()
@@ -82,7 +90,20 @@ namespace TestProtocol
catch( Exception e )
{
MessageBox.Show( "The SspiConnection receive thread crashed:\r\n\r\n" + e.ToString() );
}
finally
{
this.running = false;
try
{
if( this.Disconnected != null )
{
this.Disconnected();
}
}
catch
{ }
}
}
@@ -133,6 +154,7 @@ namespace TestProtocol
}
}
Console.Out.WriteLine( "Client: Received " + operation );
if( this.Received != null )
{

View File

@@ -9,6 +9,7 @@ namespace TestProtocol
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
@@ -35,6 +36,8 @@ namespace TestProtocol
public event ReceivedAction Received;
public event Action Disconnected;
public void StartServer( int port )
{
if( this.running )
@@ -43,6 +46,8 @@ namespace TestProtocol
}
this.serverSocket = new Socket( SocketType.Stream, ProtocolType.Tcp );
this.serverSocket.Bind( new IPEndPoint( IPAddress.Any, port ) );
this.serverSocket.Listen( 1 );
this.running = true;
@@ -55,7 +60,7 @@ namespace TestProtocol
{
if( this.running == false )
{
throw new InvalidOperationException( "Already stopped" );
return;
}
this.serverSocket.Close();
@@ -82,7 +87,9 @@ namespace TestProtocol
Array.Copy( message.Data, 0, outBuffer, 8, message.Data.Length );
this.serverSocket.Send( outBuffer, 0, outBuffer.Length, SocketFlags.None );
this.readSocket.Send( outBuffer, 0, outBuffer.Length, SocketFlags.None );
Console.Out.WriteLine( "Server: Sent " + message.Operation );
}
private void ReceiveThreadEntry()
@@ -112,13 +119,25 @@ namespace TestProtocol
}
ReadLoop();
}
}
catch( Exception e )
{
MessageBox.Show( "The SspiConnection receive thread crashed:\r\n\r\n" + e.ToString() );
}
finally
{
this.running = false;
try
{
if( this.Disconnected != null )
{
this.Disconnected();
}
}
catch
{ }
}
}
@@ -138,7 +157,7 @@ namespace TestProtocol
// Read the operation.
this.serverSocket.Receive( readBuffer, 4, SocketFlags.None );
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
// Check if we popped out of a receive call after we were shut down.
if( this.running == false ) { break; }
@@ -146,11 +165,11 @@ namespace TestProtocol
operation = (ProtocolOp)ByteWriter.ReadInt32_BE( readBuffer, 0 );
// Read the length
this.serverSocket.Receive( readBuffer, 4, SocketFlags.None );
this.readSocket.Receive( readBuffer, 4, SocketFlags.None );
length = ByteWriter.ReadInt32_BE( readBuffer, 0 );
// Read the data
this.serverSocket.Receive( readBuffer, length, SocketFlags.None );
this.readSocket.Receive( readBuffer, length, SocketFlags.None );
}
catch( SocketException e )
@@ -169,6 +188,7 @@ namespace TestProtocol
}
}
Console.Out.WriteLine( "Server: Received " + operation );
if( this.Received != null )
{
@@ -180,7 +200,7 @@ namespace TestProtocol
{
this.Received( message );
}
catch( Exception e )
catch( Exception )
{ }
}