annotate Messaging/Server/Listeners/AsyncSocketListener.cs @ 29:9919ee227c93

msmq added
author adminsh@apollo
date Wed, 21 Mar 2012 22:09:18 +0000
parents 4c0dea4760c5
children
rev   line source
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
1 using System;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
2 using System.Net;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
3 using System.Net.Sockets;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
4 using System.Text;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
5 using System.Threading;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
6 using Common.Messages;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
7 using GalaSoft.MvvmLight.Messaging;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
8 using Server.Interfaces;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
9
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
10 namespace Server.Listeners
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
11 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
12 public class AsyncSocketListener : IListener
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
13 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
14 public bool IsListening { get; set; }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
15 private readonly int _portNumber;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
16
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
17 // Thread signal.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
18 public static ManualResetEvent AllDone = new ManualResetEvent(false);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
19
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
20 public AsyncSocketListener(int portNumber)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
21 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
22 IsListening = false;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
23 _portNumber = portNumber;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
24 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
25
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
26 public void Start()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
27 {
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
28 if (IsListening)
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
29 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
30 Messenger.Default.Send(new SocketLogMessage() { Body = "Already listening..." });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
31 return;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
32 }
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
33 Messenger.Default.Send(new SocketLogMessage() { Body = "Opening listener..." });
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
34
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
35 // Data buffer for incoming data.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
36 var bytes = new Byte[1024];
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
37
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
38 // Establish the local endpoint for the socket
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
39 var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
40 var ipAddress = ipHostInfo.AddressList[2];
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
41 var localEndPoint = new IPEndPoint(ipAddress, _portNumber);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
42
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
43 // Create a TCP/IP socket.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
44 using(var listener = new Socket(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
45 AddressFamily.InterNetwork,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
46 SocketType.Stream,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
47 ProtocolType.Tcp))
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
48 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
49
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
50 // Bind the socket to the local endpoint and listen for incoming connections.
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
51 try
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
52 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
53 listener.Bind(localEndPoint);
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
54 listener.Listen(100);
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
55 IsListening = true;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
56
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
57 Messenger.Default.Send(new SocketLogMessage() { Body = "Listener opened" });
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
58
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
59 while (IsListening)
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
60 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
61 // Set the event to nonsignaled state.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
62 AllDone.Reset();
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
63
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
64 // Start an asynchronous socket to listen for connections.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
65 Messenger.Default.Send(new SocketLogMessage() { Body = "Waiting for a connection..." });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
66
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
67 listener.BeginAccept(
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
68 AcceptCallback,
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
69 listener);
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
70
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
71 // Wait until a connection is made before continuing.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
72 AllDone.WaitOne();
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
73 }
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
74
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
75 if (listener.Connected)
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
76 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
77 Messenger.Default.Send(new SocketLogMessage() { Body = "Shutting down listener..." });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
78 listener.Shutdown(SocketShutdown.Both);
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
79
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
80 Messenger.Default.Send(new SocketLogMessage() { Body = "Disconnecting listener..." });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
81 listener.Disconnect(true);
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
82 }
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
83
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
84 }
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
85 catch (Exception e)
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
86 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
87 Messenger.Default.Send(new SocketLogMessage() { Body = e.Message });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
88 }
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
89 finally
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
90 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
91 Messenger.Default.Send(new SocketLogMessage() { Body = "Connection closed." });
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
92 }
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
93 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
94 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
95
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
96 public void AcceptCallback(IAsyncResult ar)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
97 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
98 // Signal the main thread to continue.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
99 AllDone.Set();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
100
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
101 // Get the socket that handles the client request.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
102 var listener = (Socket) ar.AsyncState;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
103 var handler = listener.EndAccept(ar);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
104
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
105 // Create the state object.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
106 var state = new StateObject {WorkSocket = handler};
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
107
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
108 handler.BeginReceive(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
109 state.Buffer,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
110 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
111 StateObject.BufferSize,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
112 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
113 ReadCallback,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
114 state);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
115 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
116
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
117 public void ReadCallback(IAsyncResult ar)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
118 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
119 var content = String.Empty;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
120
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
121 // Retrieve the state object and the handler socket
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
122 // from the asynchronous state object.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
123 var state = (StateObject)ar.AsyncState;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
124 var handler = state.WorkSocket;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
125
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
126 // Read data from the client socket.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
127 var bytesRead = handler.EndReceive(ar);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
128
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
129 if (bytesRead <= 0) return;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
130
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
131 // There might be more data, so store the data received so far.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
132 state.Sb.Append(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
133 Encoding.ASCII.GetString(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
134 state.Buffer,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
135 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
136 bytesRead));
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
137
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
138 // Check for end-of-file tag. If it is not there, read
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
139 // more data.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
140 content = state.Sb.ToString();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
141 if (content.IndexOf("<EOF>") > -1)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
142 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
143 // All the data has been read from the
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
144 // client. Display it on the console.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
145
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
146 Messenger.Default.Send(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
147 new SocketLogMessage()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
148 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
149 Body = string.Format(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
150 "Read {0} bytes from socket. \n Data : {1}",
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
151 content.Length,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
152 content)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
153 });
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
154
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
155 // Echo the data back to the client.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
156 Send(handler, "The following message was received: " + content);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
157 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
158 else
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
159 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
160 // Not all data received. Get more.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
161 handler.BeginReceive(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
162 state.Buffer,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
163 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
164 StateObject.BufferSize,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
165 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
166 ReadCallback,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
167 state);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
168 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
169 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
170
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
171 private void Send(Socket handler, String data)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
172 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
173 // Convert the string data to byte data using ASCII encoding.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
174 var byteData = Encoding.ASCII.GetBytes(data);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
175
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
176 // Begin sending the data to the remote device.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
177 handler.BeginSend(
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
178 byteData,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
179 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
180 byteData.Length,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
181 0,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
182 SendCallback,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
183 handler);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
184 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
185
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
186 private void SendCallback(IAsyncResult ar)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
187 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
188 try
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
189 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
190 // Retrieve the socket from the state object.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
191 var handler = (Socket)ar.AsyncState;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
192
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
193 // Complete sending the data to the remote device.
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
194 var bytesSent = handler.EndSend(ar);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
195
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
196 Messenger.Default.Send(new SocketLogMessage() { Body = string.Format("Sent {0} bytes to client.", bytesSent) });
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
197
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
198 handler.Shutdown(SocketShutdown.Both);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
199 handler.Close();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
200 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
201 catch (Exception e)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
202 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
203 Messenger.Default.Send(new SocketLogMessage() { Body = e.Message });
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
204 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
205 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
206
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
207 public void Stop()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
208 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
209 if (IsListening == false) return;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
210
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
211 Messenger.Default.Send(new SocketLogMessage() { Body = "Closing connection..." });
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
212 IsListening = false;
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
213 AllDone.Set();
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
214 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
215 }
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
216
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
217 // State object for reading client data asynchronously
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
218 public class StateObject
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
219 {
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
220 // Client socket.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
221 public Socket WorkSocket;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
222
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
223 // Size of receive buffer.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
224 public const int BufferSize = 1024;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
225
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
226 // Receive buffer.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
227 public byte[] Buffer = new byte[BufferSize];
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
228
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
229 // Received data string.
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
230 public StringBuilder Sb = new StringBuilder();
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
231 }
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
232 }