diff --git a/NSspi.sln b/NSspi.sln
index 2b40dc2..0c252f2 100644
--- a/NSspi.sln
+++ b/NSspi.sln
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestClient", "TestClient\Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProtocol", "TestProtocol\TestProtocol.csproj", "{9BFD94E1-D9FB-44D7-A6E7-8BAC2620E535}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestServer", "TestServer\TestServer.csproj", "{35B8280A-8EB1-4FB5-B448-B4E9F132317F}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -25,6 +27,10 @@ Global
{9BFD94E1-D9FB-44D7-A6E7-8BAC2620E535}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BFD94E1-D9FB-44D7-A6E7-8BAC2620E535}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BFD94E1-D9FB-44D7-A6E7-8BAC2620E535}.Release|Any CPU.Build.0 = Release|Any CPU
+ {35B8280A-8EB1-4FB5-B448-B4E9F132317F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {35B8280A-8EB1-4FB5-B448-B4E9F132317F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {35B8280A-8EB1-4FB5-B448-B4E9F132317F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {35B8280A-8EB1-4FB5-B448-B4E9F132317F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/TestClient/Form1.cs b/TestClient/Form1.cs
index e37c2ce..51d9c6c 100644
--- a/TestClient/Form1.cs
+++ b/TestClient/Form1.cs
@@ -11,9 +11,12 @@ using System.Windows.Forms;
using NSspi;
using NSspi.Contexts;
using NSspi.Credentials;
+using TestProtocol;
namespace TestClient
{
+ using Message = TestProtocol.Message;
+
public partial class Form1 : Form
{
private ClientContext context;
diff --git a/TestProtocol/CustomConnection.cs b/TestProtocol/CustomConnection.cs
index b8468f0..43b02ac 100644
--- a/TestProtocol/CustomConnection.cs
+++ b/TestProtocol/CustomConnection.cs
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using NSspi;
-namespace TestClient
+namespace TestProtocol
{
public class CustomConnection
{
diff --git a/TestProtocol/CustomServer.cs b/TestProtocol/CustomServer.cs
new file mode 100644
index 0000000..8493470
--- /dev/null
+++ b/TestProtocol/CustomServer.cs
@@ -0,0 +1,191 @@
+using System;
+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.Sockets;
+ using System.Text;
+ using System.Threading;
+ using System.Threading.Tasks;
+ using System.Windows.Forms;
+ using NSspi;
+
+ public class CustomServer
+ {
+ private Thread receiveThread;
+
+ private Socket serverSocket;
+
+ private Socket readSocket;
+
+ private bool running;
+
+ public CustomServer()
+ {
+ this.running = false;
+ }
+
+ public delegate void ReceivedAction( Message message );
+
+ public event ReceivedAction Received;
+
+ public void StartServer( int port )
+ {
+ if( this.running )
+ {
+ throw new InvalidOperationException( "Already running" );
+ }
+
+ this.serverSocket = new Socket( SocketType.Stream, ProtocolType.Tcp );
+
+ this.running = true;
+
+ this.receiveThread = new Thread( ReceiveThreadEntry );
+ this.receiveThread.Name = "SSPI Server Receive Thread";
+ this.receiveThread.Start();
+ }
+
+ public void Stop()
+ {
+ if( this.running == false )
+ {
+ throw new InvalidOperationException( "Already stopped" );
+ }
+
+ this.serverSocket.Close();
+
+ if( this.readSocket != null )
+ {
+ this.readSocket.Close();
+ }
+
+ this.receiveThread.Join();
+ }
+
+ public void Send( Message message )
+ {
+ if( this.running == false )
+ {
+ throw new InvalidOperationException( "Not connected" );
+ }
+
+ byte[] outBuffer = new byte[message.Data.Length + 8];
+
+ ByteWriter.WriteInt32_BE( (int)message.Operation, outBuffer, 0 );
+ ByteWriter.WriteInt32_BE( message.Data.Length, outBuffer, 4 );
+
+ Array.Copy( message.Data, 0, outBuffer, 8, message.Data.Length );
+
+ this.serverSocket.Send( outBuffer, 0, outBuffer.Length, SocketFlags.None );
+ }
+
+ private void ReceiveThreadEntry()
+ {
+ try
+ {
+ while( this.running )
+ {
+ try
+ {
+ this.readSocket = this.serverSocket.Accept();
+ }
+ catch( SocketException e )
+ {
+ if( e.SocketErrorCode == SocketError.ConnectionAborted ||
+ e.SocketErrorCode == SocketError.Interrupted ||
+ e.SocketErrorCode == SocketError.OperationAborted ||
+ e.SocketErrorCode == SocketError.Shutdown )
+ {
+ // Shutting down.
+ break;
+ }
+ else
+ {
+ throw;
+ }
+ }
+
+ ReadLoop();
+
+ }
+ }
+ catch( Exception e )
+ {
+ MessageBox.Show( "The SspiConnection receive thread crashed:\r\n\r\n" + e.ToString() );
+ this.running = false;
+ }
+ }
+
+ private void ReadLoop()
+ {
+ byte[] readBuffer = new byte[65536];
+
+ ProtocolOp operation;
+ int length;
+
+ while( this.running )
+ {
+ try
+ {
+ // |--4 bytes--|--4 bytes--|---N--|
+ // Every command is a TLV - | Operation | Length | Data |
+
+
+ // Read the operation.
+ this.serverSocket.Receive( readBuffer, 4, SocketFlags.None );
+
+ // Check if we popped out of a receive call after we were shut down.
+ if( this.running == false ) { break; }
+
+ operation = (ProtocolOp)ByteWriter.ReadInt32_BE( readBuffer, 0 );
+
+ // Read the length
+ this.serverSocket.Receive( readBuffer, 4, SocketFlags.None );
+ length = ByteWriter.ReadInt32_BE( readBuffer, 0 );
+
+ // Read the data
+ this.serverSocket.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 )
+ {
+ // Shutting down.
+ break;
+ }
+ else
+ {
+ throw;
+ }
+ }
+
+
+ if( this.Received != null )
+ {
+ byte[] dataCopy = new byte[length];
+ Array.Copy( readBuffer, 0, dataCopy, 0, length );
+ Message message = new Message( operation, dataCopy );
+
+ try
+ {
+ this.Received( message );
+ }
+ catch( Exception e )
+ { }
+ }
+
+ }
+ }
+ }
+
+}
diff --git a/TestProtocol/Message.cs b/TestProtocol/Message.cs
index 517a66a..de2b5f5 100644
--- a/TestProtocol/Message.cs
+++ b/TestProtocol/Message.cs
@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
-namespace TestClient
+namespace TestProtocol
{
public class Message
{
diff --git a/TestProtocol/ProtocolOp.cs b/TestProtocol/ProtocolOp.cs
index 89b6d74..dd36445 100644
--- a/TestProtocol/ProtocolOp.cs
+++ b/TestProtocol/ProtocolOp.cs
@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
-namespace TestClient
+namespace TestProtocol
{
public enum ProtocolOp : uint
{
diff --git a/TestProtocol/TestProtocol.csproj b/TestProtocol/TestProtocol.csproj
index c5aa8a5..15c2491 100644
--- a/TestProtocol/TestProtocol.csproj
+++ b/TestProtocol/TestProtocol.csproj
@@ -47,6 +47,7 @@
+
diff --git a/TestServer/TestServer.csproj b/TestServer/TestServer.csproj
index 936a7be..b9d20f1 100644
--- a/TestServer/TestServer.csproj
+++ b/TestServer/TestServer.csproj
@@ -4,7 +4,7 @@
Debug
AnyCPU
- ccc5f303-782b-427d-855d-b86452c40033
+ {35B8280A-8EB1-4FB5-B448-B4E9F132317F}
WinExe
Properties
TestServer