diff Messaging/Server/Listeners/AsyncSocketListener.cs @ 28:4c0dea4760c5

RabbitMq working
author adminsh@apollo
date Wed, 21 Mar 2012 20:29:04 +0000
parents 96fdf58e05b4
children
line wrap: on
line diff
--- a/Messaging/Server/Listeners/AsyncSocketListener.cs	Wed Mar 21 19:00:59 2012 +0000
+++ b/Messaging/Server/Listeners/AsyncSocketListener.cs	Wed Mar 21 20:29:04 2012 +0000
@@ -25,7 +25,12 @@
 
         public void Start()
         {
-            Messenger.Default.Send(new SocketLogMessage() { Body = "Opening connection..." });
+            if (IsListening)
+            {
+                Messenger.Default.Send(new SocketLogMessage() { Body = "Already listening..." });
+                return;
+            }
+            Messenger.Default.Send(new SocketLogMessage() { Body = "Opening listener..." });
 
             // Data buffer for incoming data.
             var bytes = new Byte[1024];
@@ -43,34 +48,48 @@
                 {
 
                 // Bind the socket to the local endpoint and listen for incoming connections.
-                try
-                {
-                    listener.Bind(localEndPoint);
-                    listener.Listen(100);
-                    IsListening = true;
+                    try
+                    {
+                        listener.Bind(localEndPoint);
+                        listener.Listen(100);
+                        IsListening = true;
+
+                        Messenger.Default.Send(new SocketLogMessage() { Body = "Listener opened" });
 
-                    Messenger.Default.Send(new SocketLogMessage() { Body = "Connection opened" });
+                        while (IsListening)
+                        {
+                            // Set the event to nonsignaled state.
+                            AllDone.Reset();
 
-                    while (IsListening)
-                    {
-                        // Set the event to nonsignaled state.
-                        AllDone.Reset();
+                            // Start an asynchronous socket to listen for connections.
+                            Messenger.Default.Send(new SocketLogMessage() { Body = "Waiting for a connection..." });
+
+                            listener.BeginAccept(
+                                AcceptCallback,
+                                listener);
 
-                        // Start an asynchronous socket to listen for connections.
-                        Messenger.Default.Send(new SocketLogMessage() { Body = "Waiting for a connection..." });
-                    
-                        listener.BeginAccept(
-                            AcceptCallback,
-                            listener);
+                            // Wait until a connection is made before continuing.
+                            AllDone.WaitOne();
+                        }
+
+                        if (listener.Connected)
+                        {
+                            Messenger.Default.Send(new SocketLogMessage() { Body = "Shutting down listener..." });
+                            listener.Shutdown(SocketShutdown.Both);
 
-                        // Wait until a connection is made before continuing.
-                        AllDone.WaitOne();
+                            Messenger.Default.Send(new SocketLogMessage() { Body = "Disconnecting listener..." });
+                            listener.Disconnect(true);
+                        }
+
                     }
-                }
-                catch (Exception e)
-                {
-                    Messenger.Default.Send(new SocketLogMessage() { Body = e.Message });
-                }
+                    catch (Exception e)
+                    {
+                        Messenger.Default.Send(new SocketLogMessage() { Body = e.Message });
+                    }
+                    finally
+                    {
+                        Messenger.Default.Send(new SocketLogMessage() { Body = "Connection closed." });
+                    }
             }
         }
 
@@ -191,7 +210,23 @@
 
             Messenger.Default.Send(new SocketLogMessage() { Body = "Closing connection..." });
             IsListening = false;
-            Messenger.Default.Send(new SocketLogMessage() { Body = "Connection closed." });
+            AllDone.Set();
         }
     }
+
+    // State object for reading client data asynchronously
+    public class StateObject
+    {
+        // Client  socket.
+        public Socket WorkSocket;
+
+        // Size of receive buffer.
+        public const int BufferSize = 1024;
+
+        // Receive buffer.
+        public byte[] Buffer = new byte[BufferSize];
+
+        // Received data string.
+        public StringBuilder Sb = new StringBuilder();
+    }
 }
\ No newline at end of file