# HG changeset patch # User lenclud@WUE-9BW295J.ad.garmin.com # Date 1360248966 -3600 # Node ID f9c5cad93786b92ee5cdf833bcec0496171c2d7d # Parent a77691c4006693d65f8e42fc448bd0a7345cdbad Starting the refactor our server. diff -r a77691c40066 -r f9c5cad93786 CSNamedPipe/CSNamedPipe/NamedPipeServer.cs --- a/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 01:10:03 2013 +0100 +++ b/CSNamedPipe/CSNamedPipe/NamedPipeServer.cs Thu Feb 07 15:56:06 2013 +0100 @@ -34,61 +34,71 @@ public const uint PIPE_ACCESS_OUTBOUND = (0x00000002); public const uint PIPE_ACCESS_INBOUND = (0x00000001); - public class Client - { - public SafeFileHandle handle; - public FileStream stream; - } public const int BUFFER_SIZE = 256; - public Client clientse =null; - public string pipeName; - Thread listenThread; - SafeFileHandle clientHandle; + // + public string iPipeNameOutbound; + Thread iThreadOutbound; + SafeFileHandle iPipeOutbound; + public FileStream iStreamOutbound; + // + public string iPipeNameInbound; + Thread iThreadInbound; + SafeFileHandle iPipeInbound; + public FileStream iStreamInbound; + public int ClientType; public NamedPipeServer(string PName,int Mode) { - pipeName = PName; + iPipeNameOutbound = PName; ClientType = Mode;//0 Reading Pipe, 1 Writing Pipe } + /** + * Start our services. + */ public void Start() { - this.listenThread = new Thread(new ThreadStart(ListenForClients)); - this.listenThread.Start(); + //Start outbound thread to send messages + this.iThreadOutbound = new Thread(new ThreadStart(ThreadOutbound)); + this.iThreadOutbound.Start(); + //Start inbound thread to receive messages + //this.iThreadInbound = new Thread(new ThreadStart(ThreadInbound)); + //this.iThreadInbound.Start(); } - private void ListenForClients() + + /** + * Outbound thread is sending messages to our client. + */ + private void ThreadOutbound() { + //TODO: Check that 255 + iPipeOutbound = CreateNamedPipe(this.iPipeNameOutbound, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); + while (true) { - - clientHandle = CreateNamedPipe(this.pipeName, PIPE_ACCESS_OUTBOUND /*| FILE_FLAG_OVERLAPPED*/, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); - //could not create named pipe - if (clientHandle.IsInvalid) + if (iPipeOutbound.IsInvalid) return; - int success = ConnectNamedPipe(clientHandle, IntPtr.Zero); + //Will complete once a client connects + int success = ConnectNamedPipe(iPipeOutbound, IntPtr.Zero); //could not connect client if (success == 0) return; - clientse = new Client(); - clientse.handle = clientHandle; - clientse.stream = new FileStream(clientse.handle, FileAccess.Write, BUFFER_SIZE, false); - - if (ClientType == 0) - { - Thread readThread = new Thread(new ThreadStart(Read)); - readThread.Start(); - } + iStreamOutbound = new FileStream(iPipeOutbound, FileAccess.Write, BUFFER_SIZE, false); } } - private void Read() + + /** + * Inbound thread is receiving messages from our client + */ + private void ThreadInbound() { //Client client = (Client)clientObj; //clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true); @@ -96,14 +106,13 @@ ASCIIEncoding encoder = new ASCIIEncoding(); while (true) - { - + { int bytesRead = 0; try { buffer = new byte[BUFFER_SIZE]; - bytesRead = clientse.stream.Read(buffer, 0, BUFFER_SIZE); + bytesRead = iStreamInbound.Read(buffer, 0, BUFFER_SIZE); } catch { @@ -141,32 +150,41 @@ } //clean up resources - clientse.stream.Close(); - clientse.handle.Close(); - + iStreamInbound.Close(); + iPipeInbound.Close(); } - public void SendMessage(string message, Client client) + + /** + * Send a message to our client. + */ + public void SendMessage(string message) { ASCIIEncoding encoder = new ASCIIEncoding(); byte[] messageBuffer = encoder.GetBytes(message); - if (client.stream.CanWrite) + if (iStreamOutbound.CanWrite) { - client.stream.Write(messageBuffer, 0, messageBuffer.Length); - client.stream.Flush(); + iStreamOutbound.Write(messageBuffer, 0, messageBuffer.Length); + iStreamOutbound.Flush(); } } - public void StopServer() + + /** + * + */ + public void Stop() { //clean up resources - DisconnectNamedPipe(this.clientHandle); + DisconnectNamedPipe(this.iPipeOutbound); + + //TODO: more cleanup - this.listenThread.Abort(); + this.iThreadOutbound.Abort(); } } diff -r a77691c40066 -r f9c5cad93786 CSNamedPipe/CSNamedPipe/Program.cs --- a/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 01:10:03 2013 +0100 +++ b/CSNamedPipe/CSNamedPipe/Program.cs Thu Feb 07 15:56:06 2013 +0100 @@ -8,24 +8,23 @@ { static void Main(string[] args) { - NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0); + //NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0); //NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1); - NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1); + NamedPipeServer server = new NamedPipeServer(@"\\.\pipe\sga-receiver", 1); //PServer1.Start(); - PServer2.Start(); + server.Start(); string Ms="Start"; do { Console.WriteLine("Enter the message"); Ms = Console.ReadLine(); - PServer2.SendMessage(Ms, PServer2.clientse); + server.SendMessage(Ms); } while (Ms != "quit"); - PServer1.StopServer(); - PServer2.StopServer(); + server.Stop(); } }