Starting the refactor our server.
1.1 --- a/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 01:10:03 2013 +0100
1.2 +++ b/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 15:56:06 2013 +0100
1.3 @@ -34,61 +34,71 @@
1.4 public const uint PIPE_ACCESS_OUTBOUND = (0x00000002);
1.5 public const uint PIPE_ACCESS_INBOUND = (0x00000001);
1.6
1.7 - public class Client
1.8 - {
1.9 - public SafeFileHandle handle;
1.10 - public FileStream stream;
1.11 - }
1.12
1.13 public const int BUFFER_SIZE = 256;
1.14 - public Client clientse =null;
1.15
1.16 - public string pipeName;
1.17 - Thread listenThread;
1.18 - SafeFileHandle clientHandle;
1.19 + //
1.20 + public string iPipeNameOutbound;
1.21 + Thread iThreadOutbound;
1.22 + SafeFileHandle iPipeOutbound;
1.23 + public FileStream iStreamOutbound;
1.24 + //
1.25 + public string iPipeNameInbound;
1.26 + Thread iThreadInbound;
1.27 + SafeFileHandle iPipeInbound;
1.28 + public FileStream iStreamInbound;
1.29 +
1.30 public int ClientType;
1.31
1.32 public NamedPipeServer(string PName,int Mode)
1.33 {
1.34 - pipeName = PName;
1.35 + iPipeNameOutbound = PName;
1.36 ClientType = Mode;//0 Reading Pipe, 1 Writing Pipe
1.37
1.38 }
1.39
1.40 + /**
1.41 + * Start our services.
1.42 + */
1.43 public void Start()
1.44 {
1.45 - this.listenThread = new Thread(new ThreadStart(ListenForClients));
1.46 - this.listenThread.Start();
1.47 + //Start outbound thread to send messages
1.48 + this.iThreadOutbound = new Thread(new ThreadStart(ThreadOutbound));
1.49 + this.iThreadOutbound.Start();
1.50 + //Start inbound thread to receive messages
1.51 + //this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound));
1.52 + //this.iThreadInbound.Start();
1.53 }
1.54 - private void ListenForClients()
1.55 +
1.56 + /**
1.57 + * Outbound thread is sending messages to our client.
1.58 + */
1.59 + private void ThreadOutbound()
1.60 {
1.61 + //TODO: Check that 255
1.62 + iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);
1.63 +
1.64 while (true)
1.65 {
1.66 -
1.67 - clientHandle = CreateNamedPipe(this.pipeName, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);
1.68 -
1.69 //could not create named pipe
1.70 - if (clientHandle.IsInvalid)
1.71 + if (iPipeOutbound.IsInvalid)
1.72 return;
1.73
1.74 - int success = ConnectNamedPipe(clientHandle, IntPtr.Zero);
1.75 + //Will complete once a client connects
1.76 + int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero);
1.77
1.78 //could not connect client
1.79 if (success == 0)
1.80 return;
1.81
1.82 - clientse = new Client();
1.83 - clientse.handle = clientHandle;
1.84 - clientse.stream = new FileStream(clientse.handle, FileAccess.Write, BUFFER_SIZE, false);
1.85 -
1.86 - if (ClientType == 0)
1.87 - {
1.88 - Thread readThread = new Thread(new ThreadStart(Read));
1.89 - readThread.Start();
1.90 - }
1.91 + iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false);
1.92 }
1.93 }
1.94 - private void Read()
1.95 +
1.96 + /**
1.97 + * Inbound thread is receiving messages from our client
1.98 + */
1.99 + private void ThreadInbound()
1.100 {
1.101 //Client client = (Client)clientObj;
1.102 //clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true);
1.103 @@ -96,14 +106,13 @@
1.104 ASCIIEncoding encoder = new ASCIIEncoding();
1.105
1.106 while (true)
1.107 - {
1.108 -
1.109 + {
1.110 int bytesRead = 0;
1.111
1.112 try
1.113 {
1.114 buffer = new byte[BUFFER_SIZE];
1.115 - bytesRead = clientse.stream.Read(buffer, 0, BUFFER_SIZE);
1.116 + bytesRead = iStreamInbound.Read(buffer, 0, BUFFER_SIZE);
1.117 }
1.118 catch
1.119 {
1.120 @@ -141,32 +150,41 @@
1.121 }
1.122
1.123 //clean up resources
1.124 - clientse.stream.Close();
1.125 - clientse.handle.Close();
1.126 -
1.127 + iStreamInbound.Close();
1.128 + iPipeInbound.Close();
1.129 }
1.130 - public void SendMessage(string message, Client client)
1.131 +
1.132 + /**
1.133 + * Send a message to our client.
1.134 + */
1.135 + public void SendMessage(string message)
1.136 {
1.137
1.138 ASCIIEncoding encoder = new ASCIIEncoding();
1.139 byte[] messageBuffer = encoder.GetBytes(message);
1.140
1.141 - if (client.stream.CanWrite)
1.142 + if (iStreamOutbound.CanWrite)
1.143 {
1.144 - client.stream.Write(messageBuffer, 0, messageBuffer.Length);
1.145 - client.stream.Flush();
1.146 + iStreamOutbound.Write(messageBuffer, 0, messageBuffer.Length);
1.147 + iStreamOutbound.Flush();
1.148 }
1.149
1.150
1.151 }
1.152 - public void StopServer()
1.153 +
1.154 + /**
1.155 + *
1.156 + */
1.157 + public void Stop()
1.158 {
1.159 //clean up resources
1.160
1.161 - DisconnectNamedPipe(this.clientHandle);
1.162 + DisconnectNamedPipe(this.iPipeOutbound);
1.163 +
1.164 + //TODO: more cleanup
1.165
1.166
1.167 - this.listenThread.Abort();
1.168 + this.iThreadOutbound.Abort();
1.169 }
1.170
1.171 }
2.1 --- a/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 01:10:03 2013 +0100
2.2 +++ b/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 15:56:06 2013 +0100
2.3 @@ -8,24 +8,23 @@
2.4 {
2.5 static void Main(string[] args)
2.6 {
2.7 - NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0);
2.8 + //NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0);
2.9 //NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1);
2.10 - NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1);
2.11 + NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1);
2.12
2.13
2.14 //PServer1.Start();
2.15 - PServer2.Start();
2.16 + server.Start();
2.17
2.18 string Ms="Start";
2.19 do
2.20 {
2.21 Console.WriteLine("Enter the message");
2.22 Ms = Console.ReadLine();
2.23 - PServer2.SendMessage(Ms, PServer2.clientse);
2.24 + server.SendMessage(Ms);
2.25 } while (Ms != "quit");
2.26
2.27 - PServer1.StopServer();
2.28 - PServer2.StopServer();
2.29 + server.Stop();
2.30 }
2.31
2.32 }