diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs | 520 |
1 files changed, 120 insertions, 400 deletions
diff --git a/OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs b/OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs index 43a68ec..d16837d 100644 --- a/OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs +++ b/OpenSim/Region/ClientStack/LindenUDP/OpenSimUDPBase.cs | |||
@@ -1,419 +1,137 @@ | |||
1 | //--------- Modified Version ------------------- | 1 | /* |
2 | ///* | 2 | * Copyright (c) 2006, Clutch, Inc. |
3 | // * Copyright (c) 2006, Clutch, Inc. | 3 | * Original Author: Jeff Cesnik |
4 | // * Original Author: Jeff Cesnik | 4 | * All rights reserved. |
5 | // * All rights reserved. | 5 | * |
6 | // * | 6 | * - Redistribution and use in source and binary forms, with or without |
7 | // * - Redistribution and use in source and binary forms, with or without | 7 | * modification, are permitted provided that the following conditions are met: |
8 | // * modification, are permitted provided that the following conditions are met: | 8 | * |
9 | // * | 9 | * - Redistributions of source code must retain the above copyright notice, this |
10 | // * - Redistributions of source code must retain the above copyright notice, this | 10 | * list of conditions and the following disclaimer. |
11 | // * list of conditions and the following disclaimer. | 11 | * - Neither the name of the openmetaverse.org nor the names |
12 | // * - Neither the name of the openmetaverse.org nor the names | 12 | * of its contributors may be used to endorse or promote products derived from |
13 | // * of its contributors may be used to endorse or promote products derived from | 13 | * this software without specific prior written permission. |
14 | // * this software without specific prior written permission. | 14 | * |
15 | // * | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
16 | // * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | // * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | // * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | // * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | // * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | // * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | // * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | // * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | // * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 | // * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 25 | * POSSIBILITY OF SUCH DAMAGE. |
26 | // * POSSIBILITY OF SUCH DAMAGE. | 26 | */ |
27 | // */ | ||
28 | 27 | ||
29 | using System; | 28 | using System; |
30 | using System.Net; | 29 | using System.Net; |
31 | using System.Net.Sockets; | 30 | using System.Net.Sockets; |
32 | using System.Threading; | 31 | using System.Threading; |
33 | using OpenMetaverse; | 32 | using log4net; |
34 | 33 | ||
35 | //namespace OpenSim.Region.ClientStack.LindenUDP | 34 | namespace OpenMetaverse |
36 | //{ | ||
37 | // /// <summary> | ||
38 | // /// | ||
39 | // /// </summary> | ||
40 | // public abstract class OpenSimUDPBase | ||
41 | // { | ||
42 | // // these abstract methods must be implemented in a derived class to actually do | ||
43 | // // something with the packets that are sent and received. | ||
44 | // protected abstract void PacketReceived(UDPPacketBuffer buffer); | ||
45 | // protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent); | ||
46 | |||
47 | // // the port to listen on | ||
48 | // internal int udpPort; | ||
49 | |||
50 | // // the UDP socket | ||
51 | // private Socket udpSocket; | ||
52 | |||
53 | // // the ReaderWriterLock is used solely for the purposes of shutdown (Stop()). | ||
54 | // // since there are potentially many "reader" threads in the internal .NET IOCP | ||
55 | // // thread pool, this is a cheaper synchronization primitive than using | ||
56 | // // a Mutex object. This allows many UDP socket "reads" concurrently - when | ||
57 | // // Stop() is called, it attempts to obtain a writer lock which will then | ||
58 | // // wait until all outstanding operations are completed before shutting down. | ||
59 | // // this avoids the problem of closing the socket with outstanding operations | ||
60 | // // and trying to catch the inevitable ObjectDisposedException. | ||
61 | // private ReaderWriterLock rwLock = new ReaderWriterLock(); | ||
62 | |||
63 | // // number of outstanding operations. This is a reference count | ||
64 | // // which we use to ensure that the threads exit cleanly. Note that | ||
65 | // // we need this because the threads will potentially still need to process | ||
66 | // // data even after the socket is closed. | ||
67 | // private int rwOperationCount = 0; | ||
68 | |||
69 | // // the all important shutdownFlag. This is synchronized through the ReaderWriterLock. | ||
70 | // private volatile bool shutdownFlag = true; | ||
71 | |||
72 | // // the remote endpoint to communicate with | ||
73 | // protected IPEndPoint remoteEndPoint = null; | ||
74 | |||
75 | |||
76 | // /// <summary> | ||
77 | // /// Initialize the UDP packet handler in server mode | ||
78 | // /// </summary> | ||
79 | // /// <param name="port">Port to listening for incoming UDP packets on</param> | ||
80 | // public OpenSimUDPBase(int port) | ||
81 | // { | ||
82 | // udpPort = port; | ||
83 | // } | ||
84 | |||
85 | // /// <summary> | ||
86 | // /// Initialize the UDP packet handler in client mode | ||
87 | // /// </summary> | ||
88 | // /// <param name="endPoint">Remote UDP server to connect to</param> | ||
89 | // public OpenSimUDPBase(IPEndPoint endPoint) | ||
90 | // { | ||
91 | // remoteEndPoint = endPoint; | ||
92 | // udpPort = 0; | ||
93 | // } | ||
94 | |||
95 | // /// <summary> | ||
96 | // /// | ||
97 | // /// </summary> | ||
98 | // public void Start() | ||
99 | // { | ||
100 | // if (shutdownFlag) | ||
101 | // { | ||
102 | // if (remoteEndPoint == null) | ||
103 | // { | ||
104 | // // Server mode | ||
105 | |||
106 | // // create and bind the socket | ||
107 | // IPEndPoint ipep = new IPEndPoint(Settings.BIND_ADDR, udpPort); | ||
108 | // udpSocket = new Socket( | ||
109 | // AddressFamily.InterNetwork, | ||
110 | // SocketType.Dgram, | ||
111 | // ProtocolType.Udp); | ||
112 | // udpSocket.Bind(ipep); | ||
113 | // } | ||
114 | // else | ||
115 | // { | ||
116 | // // Client mode | ||
117 | // IPEndPoint ipep = new IPEndPoint(Settings.BIND_ADDR, udpPort); | ||
118 | // udpSocket = new Socket( | ||
119 | // AddressFamily.InterNetwork, | ||
120 | // SocketType.Dgram, | ||
121 | // ProtocolType.Udp); | ||
122 | // udpSocket.Bind(ipep); | ||
123 | // //udpSocket.Connect(remoteEndPoint); | ||
124 | // } | ||
125 | |||
126 | // // we're not shutting down, we're starting up | ||
127 | // shutdownFlag = false; | ||
128 | |||
129 | // // kick off an async receive. The Start() method will return, the | ||
130 | // // actual receives will occur asynchronously and will be caught in | ||
131 | // // AsyncEndRecieve(). | ||
132 | // AsyncBeginReceive(); | ||
133 | // } | ||
134 | // } | ||
135 | |||
136 | // /// <summary> | ||
137 | // /// | ||
138 | // /// </summary> | ||
139 | // public void Stop() | ||
140 | // { | ||
141 | // if (!shutdownFlag) | ||
142 | // { | ||
143 | // // wait indefinitely for a writer lock. Once this is called, the .NET runtime | ||
144 | // // will deny any more reader locks, in effect blocking all other send/receive | ||
145 | // // threads. Once we have the lock, we set shutdownFlag to inform the other | ||
146 | // // threads that the socket is closed. | ||
147 | // rwLock.AcquireWriterLock(-1); | ||
148 | // shutdownFlag = true; | ||
149 | // udpSocket.Close(); | ||
150 | // rwLock.ReleaseWriterLock(); | ||
151 | |||
152 | // // wait for any pending operations to complete on other | ||
153 | // // threads before exiting. | ||
154 | // const int FORCE_STOP = 100; | ||
155 | // int i = 0; | ||
156 | // while (rwOperationCount > 0 && i < FORCE_STOP) | ||
157 | // { | ||
158 | // Thread.Sleep(10); | ||
159 | // ++i; | ||
160 | // } | ||
161 | |||
162 | // if (i >= FORCE_STOP) | ||
163 | // { | ||
164 | // Logger.Log("UDPBase.Stop() forced shutdown while waiting on pending operations", | ||
165 | // Helpers.LogLevel.Warning); | ||
166 | // } | ||
167 | // } | ||
168 | // } | ||
169 | |||
170 | // /// <summary> | ||
171 | // /// | ||
172 | // /// </summary> | ||
173 | // public bool IsRunning | ||
174 | // { | ||
175 | // get { return !shutdownFlag; } | ||
176 | // } | ||
177 | |||
178 | // private void AsyncBeginReceive() | ||
179 | // { | ||
180 | // // this method actually kicks off the async read on the socket. | ||
181 | // // we aquire a reader lock here to ensure that no other thread | ||
182 | // // is trying to set shutdownFlag and close the socket. | ||
183 | // rwLock.AcquireReaderLock(-1); | ||
184 | |||
185 | // if (!shutdownFlag) | ||
186 | // { | ||
187 | // // increment the count of pending operations | ||
188 | // Interlocked.Increment(ref rwOperationCount); | ||
189 | |||
190 | // // allocate a packet buffer | ||
191 | // //WrappedObject<UDPPacketBuffer> wrappedBuffer = Pool.CheckOut(); | ||
192 | // UDPPacketBuffer buf = new UDPPacketBuffer(); | ||
193 | |||
194 | // try | ||
195 | // { | ||
196 | // // kick off an async read | ||
197 | // udpSocket.BeginReceiveFrom( | ||
198 | // //wrappedBuffer.Instance.Data, | ||
199 | // buf.Data, | ||
200 | // 0, | ||
201 | // UDPPacketBuffer.BUFFER_SIZE, | ||
202 | // SocketFlags.None, | ||
203 | // //ref wrappedBuffer.Instance.RemoteEndPoint, | ||
204 | // ref buf.RemoteEndPoint, | ||
205 | // new AsyncCallback(AsyncEndReceive), | ||
206 | // //wrappedBuffer); | ||
207 | // buf); | ||
208 | // } | ||
209 | // catch (SocketException) | ||
210 | // { | ||
211 | // // something bad happened | ||
212 | // //Logger.Log( | ||
213 | // // "A SocketException occurred in UDPServer.AsyncBeginReceive()", | ||
214 | // // Helpers.LogLevel.Error, se); | ||
215 | |||
216 | // // an error occurred, therefore the operation is void. Decrement the reference count. | ||
217 | // Interlocked.Decrement(ref rwOperationCount); | ||
218 | // } | ||
219 | // } | ||
220 | |||
221 | // // we're done with the socket for now, release the reader lock. | ||
222 | // rwLock.ReleaseReaderLock(); | ||
223 | // } | ||
224 | |||
225 | // private void AsyncEndReceive(IAsyncResult iar) | ||
226 | // { | ||
227 | // // Asynchronous receive operations will complete here through the call | ||
228 | // // to AsyncBeginReceive | ||
229 | |||
230 | // // aquire a reader lock | ||
231 | // rwLock.AcquireReaderLock(-1); | ||
232 | |||
233 | // if (!shutdownFlag) | ||
234 | // { | ||
235 | // // get the buffer that was created in AsyncBeginReceive | ||
236 | // // this is the received data | ||
237 | // //WrappedObject<UDPPacketBuffer> wrappedBuffer = (WrappedObject<UDPPacketBuffer>)iar.AsyncState; | ||
238 | // //UDPPacketBuffer buffer = wrappedBuffer.Instance; | ||
239 | // UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState; | ||
240 | |||
241 | // try | ||
242 | // { | ||
243 | // // get the length of data actually read from the socket, store it with the | ||
244 | // // buffer | ||
245 | // buffer.DataLength = udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint); | ||
246 | |||
247 | // // this operation is now complete, decrement the reference count | ||
248 | // Interlocked.Decrement(ref rwOperationCount); | ||
249 | |||
250 | // // we're done with the socket, release the reader lock | ||
251 | // rwLock.ReleaseReaderLock(); | ||
252 | |||
253 | // // call the abstract method PacketReceived(), passing the buffer that | ||
254 | // // has just been filled from the socket read. | ||
255 | // PacketReceived(buffer); | ||
256 | // } | ||
257 | // catch (SocketException) | ||
258 | // { | ||
259 | // // an error occurred, therefore the operation is void. Decrement the reference count. | ||
260 | // Interlocked.Decrement(ref rwOperationCount); | ||
261 | |||
262 | // // we're done with the socket for now, release the reader lock. | ||
263 | // rwLock.ReleaseReaderLock(); | ||
264 | // } | ||
265 | // finally | ||
266 | // { | ||
267 | // // start another receive - this keeps the server going! | ||
268 | // AsyncBeginReceive(); | ||
269 | |||
270 | // //wrappedBuffer.Dispose(); | ||
271 | // } | ||
272 | // } | ||
273 | // else | ||
274 | // { | ||
275 | // // nothing bad happened, but we are done with the operation | ||
276 | // // decrement the reference count and release the reader lock | ||
277 | // Interlocked.Decrement(ref rwOperationCount); | ||
278 | // rwLock.ReleaseReaderLock(); | ||
279 | // } | ||
280 | // } | ||
281 | |||
282 | // public void AsyncBeginSend(UDPPacketBuffer buf) | ||
283 | // { | ||
284 | // rwLock.AcquireReaderLock(-1); | ||
285 | |||
286 | // if (!shutdownFlag) | ||
287 | // { | ||
288 | // try | ||
289 | // { | ||
290 | // Interlocked.Increment(ref rwOperationCount); | ||
291 | // udpSocket.BeginSendTo( | ||
292 | // buf.Data, | ||
293 | // 0, | ||
294 | // buf.DataLength, | ||
295 | // SocketFlags.None, | ||
296 | // buf.RemoteEndPoint, | ||
297 | // new AsyncCallback(AsyncEndSend), | ||
298 | // buf); | ||
299 | // } | ||
300 | // catch (SocketException) | ||
301 | // { | ||
302 | // //Logger.Log( | ||
303 | // // "A SocketException occurred in UDPServer.AsyncBeginSend()", | ||
304 | // // Helpers.LogLevel.Error, se); | ||
305 | // } | ||
306 | // } | ||
307 | |||
308 | // rwLock.ReleaseReaderLock(); | ||
309 | // } | ||
310 | |||
311 | // private void AsyncEndSend(IAsyncResult iar) | ||
312 | // { | ||
313 | // rwLock.AcquireReaderLock(-1); | ||
314 | |||
315 | // if (!shutdownFlag) | ||
316 | // { | ||
317 | // UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState; | ||
318 | |||
319 | // try | ||
320 | // { | ||
321 | // int bytesSent = udpSocket.EndSendTo(iar); | ||
322 | |||
323 | // // note that call to the abstract PacketSent() method - we are passing the number | ||
324 | // // of bytes sent in a separate parameter, since we can't use buffer.DataLength which | ||
325 | // // is the number of bytes to send (or bytes received depending upon whether this | ||
326 | // // buffer was part of a send or a receive). | ||
327 | // PacketSent(buffer, bytesSent); | ||
328 | // } | ||
329 | // catch (SocketException) | ||
330 | // { | ||
331 | // //Logger.Log( | ||
332 | // // "A SocketException occurred in UDPServer.AsyncEndSend()", | ||
333 | // // Helpers.LogLevel.Error, se); | ||
334 | // } | ||
335 | // } | ||
336 | |||
337 | // Interlocked.Decrement(ref rwOperationCount); | ||
338 | // rwLock.ReleaseReaderLock(); | ||
339 | // } | ||
340 | // } | ||
341 | //} | ||
342 | |||
343 | //--------- Original Version ------------------- | ||
344 | |||
345 | |||
346 | namespace OpenSim.Region.ClientStack.LindenUDP | ||
347 | { | 35 | { |
348 | /// <summary> | 36 | /// <summary> |
349 | /// | 37 | /// Base UDP server |
350 | /// </summary> | 38 | /// </summary> |
351 | public abstract class OpenSimUDPBase | 39 | public abstract class OpenSimUDPBase |
352 | { | 40 | { |
353 | // these abstract methods must be implemented in a derived class to actually do | 41 | private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
354 | // something with the packets that are sent and received. | 42 | |
43 | /// <summary> | ||
44 | /// This method is called when an incoming packet is received | ||
45 | /// </summary> | ||
46 | /// <param name="buffer">Incoming packet buffer</param> | ||
355 | protected abstract void PacketReceived(UDPPacketBuffer buffer); | 47 | protected abstract void PacketReceived(UDPPacketBuffer buffer); |
48 | |||
49 | /// <summary> | ||
50 | /// This method is called when an outgoing packet is sent | ||
51 | /// </summary> | ||
52 | /// <param name="buffer">Outgoing packet buffer</param> | ||
53 | /// <param name="bytesSent">Number of bytes written to the wire</param> | ||
356 | protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent); | 54 | protected abstract void PacketSent(UDPPacketBuffer buffer, int bytesSent); |
357 | 55 | ||
358 | // the port to listen on | 56 | /// <summary>UDP port to bind to in server mode</summary> |
359 | internal int udpPort; | 57 | protected int m_udpPort; |
58 | |||
59 | /// <summary>Local IP address to bind to in server mode</summary> | ||
60 | protected IPAddress m_localBindAddress; | ||
360 | 61 | ||
361 | // the UDP socket | 62 | /// <summary>UDP socket, used in either client or server mode</summary> |
362 | private Socket udpSocket; | 63 | private Socket m_udpSocket; |
363 | 64 | ||
364 | // the all important shutdownFlag. | 65 | /// <summary>Flag to process packets asynchronously or synchronously</summary> |
365 | private volatile bool shutdownFlag = true; | 66 | private bool m_asyncPacketHandling; |
366 | 67 | ||
367 | // the remote endpoint to communicate with | 68 | /// <summary>The all important shutdown flag</summary> |
368 | protected IPEndPoint remoteEndPoint = null; | 69 | private volatile bool m_shutdownFlag = true; |
70 | |||
71 | /// <summary>Returns true if the server is currently listening, otherwise false</summary> | ||
72 | public bool IsRunning { get { return !m_shutdownFlag; } } | ||
369 | 73 | ||
370 | /// <summary> | 74 | /// <summary> |
371 | /// Initialize the UDP packet handler in server mode | 75 | /// Default constructor |
372 | /// </summary> | 76 | /// </summary> |
77 | /// <param name="bindAddress">Local IP address to bind the server to</param> | ||
373 | /// <param name="port">Port to listening for incoming UDP packets on</param> | 78 | /// <param name="port">Port to listening for incoming UDP packets on</param> |
374 | public OpenSimUDPBase(int port) | 79 | public OpenSimUDPBase(IPAddress bindAddress, int port) |
375 | { | 80 | { |
376 | udpPort = port; | 81 | m_localBindAddress = bindAddress; |
82 | m_udpPort = port; | ||
377 | } | 83 | } |
378 | 84 | ||
379 | /// <summary> | 85 | /// <summary> |
380 | /// Initialize the UDP packet handler in client mode | 86 | /// Start the UDP server |
381 | /// </summary> | 87 | /// </summary> |
382 | /// <param name="endPoint">Remote UDP server to connect to</param> | 88 | /// <param name="recvBufferSize">The size of the receive buffer for |
383 | public OpenSimUDPBase(IPEndPoint endPoint) | 89 | /// the UDP socket. This value is passed up to the operating system |
90 | /// and used in the system networking stack. Use zero to leave this | ||
91 | /// value as the default</param> | ||
92 | /// <param name="asyncPacketHandling">Set this to true to start | ||
93 | /// receiving more packets while current packet handler callbacks are | ||
94 | /// still running. Setting this to false will complete each packet | ||
95 | /// callback before the next packet is processed</param> | ||
96 | /// <remarks>This method will attempt to set the SIO_UDP_CONNRESET flag | ||
97 | /// on the socket to get newer versions of Windows to behave in a sane | ||
98 | /// manner (not throwing an exception when the remote side resets the | ||
99 | /// connection). This call is ignored on Mono where the flag is not | ||
100 | /// necessary</remarks> | ||
101 | public void Start(int recvBufferSize, bool asyncPacketHandling) | ||
384 | { | 102 | { |
385 | remoteEndPoint = endPoint; | 103 | m_asyncPacketHandling = asyncPacketHandling; |
386 | udpPort = 0; | ||
387 | } | ||
388 | 104 | ||
389 | /// <summary> | 105 | if (m_shutdownFlag) |
390 | /// | ||
391 | /// </summary> | ||
392 | public void Start() | ||
393 | { | ||
394 | if (shutdownFlag) | ||
395 | { | 106 | { |
396 | const int SIO_UDP_CONNRESET = -1744830452; | 107 | const int SIO_UDP_CONNRESET = -1744830452; |
397 | 108 | ||
398 | IPEndPoint ipep = new IPEndPoint(Settings.BIND_ADDR, udpPort); | 109 | IPEndPoint ipep = new IPEndPoint(m_localBindAddress, m_udpPort); |
399 | udpSocket = new Socket( | 110 | |
111 | m_udpSocket = new Socket( | ||
400 | AddressFamily.InterNetwork, | 112 | AddressFamily.InterNetwork, |
401 | SocketType.Dgram, | 113 | SocketType.Dgram, |
402 | ProtocolType.Udp); | 114 | ProtocolType.Udp); |
115 | |||
403 | try | 116 | try |
404 | { | 117 | { |
405 | // this udp socket flag is not supported under mono, | 118 | // This udp socket flag is not supported under mono, |
406 | // so we'll catch the exception and continue | 119 | // so we'll catch the exception and continue |
407 | udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null); | 120 | m_udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null); |
121 | m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag set"); | ||
408 | } | 122 | } |
409 | catch (SocketException) | 123 | catch (SocketException) |
410 | { | 124 | { |
411 | Logger.DebugLog("UDP SIO_UDP_CONNRESET flag not supported on this platform"); | 125 | m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring"); |
412 | } | 126 | } |
413 | udpSocket.Bind(ipep); | 127 | |
128 | if (recvBufferSize != 0) | ||
129 | m_udpSocket.ReceiveBufferSize = recvBufferSize; | ||
130 | |||
131 | m_udpSocket.Bind(ipep); | ||
414 | 132 | ||
415 | // we're not shutting down, we're starting up | 133 | // we're not shutting down, we're starting up |
416 | shutdownFlag = false; | 134 | m_shutdownFlag = false; |
417 | 135 | ||
418 | // kick off an async receive. The Start() method will return, the | 136 | // kick off an async receive. The Start() method will return, the |
419 | // actual receives will occur asynchronously and will be caught in | 137 | // actual receives will occur asynchronously and will be caught in |
@@ -423,41 +141,33 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
423 | } | 141 | } |
424 | 142 | ||
425 | /// <summary> | 143 | /// <summary> |
426 | /// | 144 | /// Stops the UDP server |
427 | /// </summary> | 145 | /// </summary> |
428 | public void Stop() | 146 | public void Stop() |
429 | { | 147 | { |
430 | if (!shutdownFlag) | 148 | if (!m_shutdownFlag) |
431 | { | 149 | { |
432 | // wait indefinitely for a writer lock. Once this is called, the .NET runtime | 150 | // wait indefinitely for a writer lock. Once this is called, the .NET runtime |
433 | // will deny any more reader locks, in effect blocking all other send/receive | 151 | // will deny any more reader locks, in effect blocking all other send/receive |
434 | // threads. Once we have the lock, we set shutdownFlag to inform the other | 152 | // threads. Once we have the lock, we set shutdownFlag to inform the other |
435 | // threads that the socket is closed. | 153 | // threads that the socket is closed. |
436 | shutdownFlag = true; | 154 | m_shutdownFlag = true; |
437 | udpSocket.Close(); | 155 | m_udpSocket.Close(); |
438 | } | 156 | } |
439 | } | 157 | } |
440 | 158 | ||
441 | /// <summary> | ||
442 | /// | ||
443 | /// </summary> | ||
444 | public bool IsRunning | ||
445 | { | ||
446 | get { return !shutdownFlag; } | ||
447 | } | ||
448 | |||
449 | private void AsyncBeginReceive() | 159 | private void AsyncBeginReceive() |
450 | { | 160 | { |
451 | // allocate a packet buffer | 161 | // allocate a packet buffer |
452 | //WrappedObject<UDPPacketBuffer> wrappedBuffer = Pool.CheckOut(); | 162 | //WrappedObject<UDPPacketBuffer> wrappedBuffer = Pool.CheckOut(); |
453 | UDPPacketBuffer buf = new UDPPacketBuffer(); | 163 | UDPPacketBuffer buf = new UDPPacketBuffer(); |
454 | 164 | ||
455 | if (!shutdownFlag) | 165 | if (!m_shutdownFlag) |
456 | { | 166 | { |
457 | try | 167 | try |
458 | { | 168 | { |
459 | // kick off an async read | 169 | // kick off an async read |
460 | udpSocket.BeginReceiveFrom( | 170 | m_udpSocket.BeginReceiveFrom( |
461 | //wrappedBuffer.Instance.Data, | 171 | //wrappedBuffer.Instance.Data, |
462 | buf.Data, | 172 | buf.Data, |
463 | 0, | 173 | 0, |
@@ -472,13 +182,13 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
472 | { | 182 | { |
473 | if (e.SocketErrorCode == SocketError.ConnectionReset) | 183 | if (e.SocketErrorCode == SocketError.ConnectionReset) |
474 | { | 184 | { |
475 | Logger.Log("SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + udpPort, Helpers.LogLevel.Error); | 185 | m_log.Warn("[UDPBASE]: SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + m_udpPort); |
476 | bool salvaged = false; | 186 | bool salvaged = false; |
477 | while (!salvaged) | 187 | while (!salvaged) |
478 | { | 188 | { |
479 | try | 189 | try |
480 | { | 190 | { |
481 | udpSocket.BeginReceiveFrom( | 191 | m_udpSocket.BeginReceiveFrom( |
482 | //wrappedBuffer.Instance.Data, | 192 | //wrappedBuffer.Instance.Data, |
483 | buf.Data, | 193 | buf.Data, |
484 | 0, | 194 | 0, |
@@ -494,7 +204,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
494 | catch (ObjectDisposedException) { return; } | 204 | catch (ObjectDisposedException) { return; } |
495 | } | 205 | } |
496 | 206 | ||
497 | Logger.Log("Salvaged the UDP listener on port " + udpPort, Helpers.LogLevel.Info); | 207 | m_log.Warn("[UDPBASE]: Salvaged the UDP listener on port " + m_udpPort); |
498 | } | 208 | } |
499 | } | 209 | } |
500 | catch (ObjectDisposedException) { } | 210 | catch (ObjectDisposedException) { } |
@@ -505,10 +215,12 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
505 | { | 215 | { |
506 | // Asynchronous receive operations will complete here through the call | 216 | // Asynchronous receive operations will complete here through the call |
507 | // to AsyncBeginReceive | 217 | // to AsyncBeginReceive |
508 | if (!shutdownFlag) | 218 | if (!m_shutdownFlag) |
509 | { | 219 | { |
510 | // start another receive - this keeps the server going! | 220 | // Asynchronous mode will start another receive before the |
511 | AsyncBeginReceive(); | 221 | // callback for this packet is even fired. Very parallel :-) |
222 | if (m_asyncPacketHandling) | ||
223 | AsyncBeginReceive(); | ||
512 | 224 | ||
513 | // get the buffer that was created in AsyncBeginReceive | 225 | // get the buffer that was created in AsyncBeginReceive |
514 | // this is the received data | 226 | // this is the received data |
@@ -520,7 +232,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
520 | { | 232 | { |
521 | // get the length of data actually read from the socket, store it with the | 233 | // get the length of data actually read from the socket, store it with the |
522 | // buffer | 234 | // buffer |
523 | buffer.DataLength = udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint); | 235 | buffer.DataLength = m_udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint); |
524 | 236 | ||
525 | // call the abstract method PacketReceived(), passing the buffer that | 237 | // call the abstract method PacketReceived(), passing the buffer that |
526 | // has just been filled from the socket read. | 238 | // has just been filled from the socket read. |
@@ -528,17 +240,26 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
528 | } | 240 | } |
529 | catch (SocketException) { } | 241 | catch (SocketException) { } |
530 | catch (ObjectDisposedException) { } | 242 | catch (ObjectDisposedException) { } |
531 | //finally { wrappedBuffer.Dispose(); } | 243 | finally |
244 | { | ||
245 | //wrappedBuffer.Dispose(); | ||
246 | |||
247 | // Synchronous mode waits until the packet callback completes | ||
248 | // before starting the receive to fetch another packet | ||
249 | if (!m_asyncPacketHandling) | ||
250 | AsyncBeginReceive(); | ||
251 | } | ||
252 | |||
532 | } | 253 | } |
533 | } | 254 | } |
534 | 255 | ||
535 | public void AsyncBeginSend(UDPPacketBuffer buf) | 256 | public void AsyncBeginSend(UDPPacketBuffer buf) |
536 | { | 257 | { |
537 | if (!shutdownFlag) | 258 | if (!m_shutdownFlag) |
538 | { | 259 | { |
539 | try | 260 | try |
540 | { | 261 | { |
541 | udpSocket.BeginSendTo( | 262 | m_udpSocket.BeginSendTo( |
542 | buf.Data, | 263 | buf.Data, |
543 | 0, | 264 | 0, |
544 | buf.DataLength, | 265 | buf.DataLength, |
@@ -557,7 +278,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
557 | try | 278 | try |
558 | { | 279 | { |
559 | UDPPacketBuffer buf = (UDPPacketBuffer)result.AsyncState; | 280 | UDPPacketBuffer buf = (UDPPacketBuffer)result.AsyncState; |
560 | int bytesSent = udpSocket.EndSendTo(result); | 281 | int bytesSent = m_udpSocket.EndSendTo(result); |
561 | 282 | ||
562 | PacketSent(buf, bytesSent); | 283 | PacketSent(buf, bytesSent); |
563 | } | 284 | } |
@@ -566,4 +287,3 @@ namespace OpenSim.Region.ClientStack.LindenUDP | |||
566 | } | 287 | } |
567 | } | 288 | } |
568 | } | 289 | } |
569 | |||