OpenSimUDPBase.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2006, Clutch, Inc.
  3. * Original Author: Jeff Cesnik
  4. * All rights reserved.
  5. *
  6. * - Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * - 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. * this software without specific prior written permission.
  14. *
  15. * 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. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * 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. * 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. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Concurrent;
  29. using System.Net;
  30. using System.Net.Sockets;
  31. using System.Threading;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Monitoring;
  35. namespace OpenMetaverse
  36. {
  37. /// <summary>
  38. /// Base UDP server
  39. /// </summary>
  40. public abstract class OpenSimUDPBase
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// This method is called when an incoming packet is received
  45. /// </summary>
  46. /// <param name="buffer">Incoming packet buffer</param>
  47. public abstract void PacketReceived(UDPPacketBuffer buffer);
  48. /// <summary>UDP port to bind to in server mode</summary>
  49. protected int m_udpPort;
  50. /// <summary>Local IP address to bind to in server mode</summary>
  51. protected IPAddress m_localBindAddress;
  52. /// <summary>UDP socket, used in either client or server mode</summary>
  53. private Socket m_udpSocket;
  54. public static Object m_udpBuffersPoolLock = new Object();
  55. public static UDPPacketBuffer[] m_udpBuffersPool = new UDPPacketBuffer[1000];
  56. public static int m_udpBuffersPoolPtr = -1;
  57. /// <summary>Returns true if the server is currently listening for inbound packets, otherwise false</summary>
  58. public bool IsRunningInbound { get; private set; }
  59. /// <summary>Returns true if the server is currently sending outbound packets, otherwise false</summary>
  60. /// <remarks>If IsRunningOut = false, then any request to send a packet is simply dropped.</remarks>
  61. public bool IsRunningOutbound { get; private set; }
  62. /// <summary>
  63. /// Number of UDP receives.
  64. /// </summary>
  65. public int UdpReceives { get; private set; }
  66. /// <summary>
  67. /// Number of UDP sends
  68. /// </summary>
  69. public int UdpSends { get; private set; }
  70. /// <summary>
  71. /// Number of receives over which to establish a receive time average.
  72. /// </summary>
  73. private readonly static int s_receiveTimeSamples = 500;
  74. /// <summary>
  75. /// Current number of samples taken to establish a receive time average.
  76. /// </summary>
  77. private int m_currentReceiveTimeSamples;
  78. /// <summary>
  79. /// Cumulative receive time for the sample so far.
  80. /// </summary>
  81. private int m_receiveTicksInCurrentSamplePeriod;
  82. /// <summary>
  83. /// The average time taken for each require receive in the last sample.
  84. /// </summary>
  85. public float AverageReceiveTicksForLastSamplePeriod { get; private set; }
  86. public int Port
  87. {
  88. get { return m_udpPort; }
  89. }
  90. #region PacketDropDebugging
  91. /// <summary>
  92. /// For debugging purposes only... random number generator for dropping
  93. /// outbound packets.
  94. /// </summary>
  95. private Random m_dropRandomGenerator = new Random();
  96. /// <summary>
  97. /// For debugging purposes only... parameters for a simplified
  98. /// model of packet loss with bursts, overall drop rate should
  99. /// be roughly 1 - m_dropLengthProbability / (m_dropProbabiliy + m_dropLengthProbability)
  100. /// which is about 1% for parameters 0.0015 and 0.15
  101. /// </summary>
  102. private double m_dropProbability = 0.0030;
  103. private double m_dropLengthProbability = 0.15;
  104. private bool m_dropState = false;
  105. /// <summary>
  106. /// For debugging purposes only... parameters to control the time
  107. /// duration over which packet loss bursts can occur, if no packets
  108. /// have been sent for m_dropResetTicks milliseconds, then reset the
  109. /// state of the packet dropper to its default.
  110. /// </summary>
  111. private int m_dropLastTick = 0;
  112. private int m_dropResetTicks = 500;
  113. /// <summary>
  114. /// Debugging code used to simulate dropped packets with bursts
  115. /// </summary>
  116. private bool DropOutgoingPacket()
  117. {
  118. double rnum = m_dropRandomGenerator.NextDouble();
  119. // if the connection has been idle for awhile (more than m_dropResetTicks) then
  120. // reset the state to the default state, don't continue a burst
  121. int curtick = Util.EnvironmentTickCount();
  122. if (Util.EnvironmentTickCountSubtract(curtick, m_dropLastTick) > m_dropResetTicks)
  123. m_dropState = false;
  124. m_dropLastTick = curtick;
  125. // if we are dropping packets, then the probability of dropping
  126. // this packet is the probability that we stay in the burst
  127. if (m_dropState)
  128. {
  129. m_dropState = (rnum < (1.0 - m_dropLengthProbability)) ? true : false;
  130. }
  131. else
  132. {
  133. m_dropState = (rnum < m_dropProbability) ? true : false;
  134. }
  135. return m_dropState;
  136. }
  137. #endregion PacketDropDebugging
  138. /// <summary>
  139. /// Default constructor
  140. /// </summary>
  141. /// <param name="bindAddress">Local IP address to bind the server to</param>
  142. /// <param name="port">Port to listening for incoming UDP packets on</param>
  143. /// /// <param name="usePool">Are we to use an object pool to get objects for handing inbound data?</param>
  144. public OpenSimUDPBase(IPAddress bindAddress, int port)
  145. {
  146. m_localBindAddress = bindAddress;
  147. m_udpPort = port;
  148. // for debugging purposes only, initializes the random number generator
  149. // used for simulating packet loss
  150. // m_dropRandomGenerator = new Random();
  151. }
  152. ~OpenSimUDPBase()
  153. {
  154. if(m_udpSocket !=null)
  155. try { m_udpSocket.Close(); } catch { }
  156. }
  157. public UDPPacketBuffer GetNewUDPBuffer(IPEndPoint remoteEndpoint)
  158. {
  159. lock (m_udpBuffersPoolLock)
  160. {
  161. if (m_udpBuffersPoolPtr >= 0)
  162. {
  163. UDPPacketBuffer buf = m_udpBuffersPool[m_udpBuffersPoolPtr];
  164. m_udpBuffersPool[m_udpBuffersPoolPtr] = null;
  165. m_udpBuffersPoolPtr--;
  166. buf.RemoteEndPoint = remoteEndpoint;
  167. return buf;
  168. }
  169. }
  170. return new UDPPacketBuffer(remoteEndpoint);
  171. }
  172. public void FreeUDPBuffer(UDPPacketBuffer buf)
  173. {
  174. lock (m_udpBuffersPoolLock)
  175. {
  176. if (m_udpBuffersPoolPtr < 999)
  177. {
  178. buf.RemoteEndPoint = null;
  179. buf.DataLength = 0;
  180. m_udpBuffersPoolPtr++;
  181. m_udpBuffersPool[m_udpBuffersPoolPtr] = buf;
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Start inbound UDP packet handling.
  187. /// </summary>
  188. /// <param name="recvBufferSize">The size of the receive buffer for
  189. /// the UDP socket. This value is passed up to the operating system
  190. /// and used in the system networking stack. Use zero to leave this
  191. /// value as the default</param>
  192. /// <param name="asyncPacketHandling">Set this to true to start
  193. /// receiving more packets while current packet handler callbacks are
  194. /// still running. Setting this to false will complete each packet
  195. /// callback before the next packet is processed</param>
  196. /// <remarks>This method will attempt to set the SIO_UDP_CONNRESET flag
  197. /// on the socket to get newer versions of Windows to behave in a sane
  198. /// manner (not throwing an exception when the remote side resets the
  199. /// connection). This call is ignored on Mono where the flag is not
  200. /// necessary</remarks>
  201. public virtual void StartInbound(int recvBufferSize)
  202. {
  203. if (!IsRunningInbound)
  204. {
  205. m_log.DebugFormat("[UDPBASE]: Starting inbound UDP loop");
  206. const int SIO_UDP_CONNRESET = -1744830452;
  207. IPEndPoint ipep = new IPEndPoint(m_localBindAddress, m_udpPort);
  208. m_udpSocket = new Socket(
  209. AddressFamily.InterNetwork,
  210. SocketType.Dgram,
  211. ProtocolType.Udp);
  212. try
  213. {
  214. if (m_udpSocket.Ttl < 128)
  215. {
  216. m_udpSocket.Ttl = 128;
  217. }
  218. }
  219. catch (SocketException)
  220. {
  221. m_log.Debug("[UDPBASE]: Failed to increase default TTL");
  222. }
  223. try
  224. {
  225. // This udp socket flag is not supported under mono,
  226. // so we'll catch the exception and continue
  227. // Try does not protect some mono versions on mac
  228. if(Util.IsWindows())
  229. {
  230. m_udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null);
  231. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag set");
  232. }
  233. else
  234. {
  235. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
  236. }
  237. }
  238. catch
  239. {
  240. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
  241. }
  242. // On at least Mono 3.2.8, multiple UDP sockets can bind to the same port by default. At the moment
  243. // we never want two regions to listen on the same port as they cannot demultiplex each other's messages,
  244. // leading to a confusing bug.
  245. // By default, Windows does not allow two sockets to bind to the same port.
  246. //
  247. // Unfortunately, this also causes a crashed sim to leave the socket in a state
  248. // where it appears to be in use but is really just hung from the old process
  249. // crashing rather than closing it. While this protects agains misconfiguration,
  250. // allowing crashed sims to be started up again right away, rather than having to
  251. // wait 2 minutes for the socket to clear is more valuable. Commented 12/13/2016
  252. // m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
  253. if (recvBufferSize != 0)
  254. m_udpSocket.ReceiveBufferSize = recvBufferSize;
  255. m_udpSocket.Bind(ipep);
  256. if (m_udpPort == 0)
  257. m_udpPort = ((IPEndPoint)m_udpSocket.LocalEndPoint).Port;
  258. IsRunningInbound = true;
  259. // kick off an async receive. The Start() method will return, the
  260. // actual receives will occur asynchronously and will be caught in
  261. // AsyncEndRecieve().
  262. AsyncBeginReceive();
  263. }
  264. }
  265. /// <summary>
  266. /// Start outbound UDP packet handling.
  267. /// </summary>
  268. public virtual void StartOutbound()
  269. {
  270. m_log.DebugFormat("[UDPBASE]: Starting outbound UDP loop");
  271. IsRunningOutbound = true;
  272. }
  273. public virtual void StopInbound()
  274. {
  275. if (IsRunningInbound)
  276. {
  277. m_log.DebugFormat("[UDPBASE]: Stopping inbound UDP loop");
  278. IsRunningInbound = false;
  279. m_udpSocket.Close();
  280. }
  281. }
  282. public virtual void StopOutbound()
  283. {
  284. m_log.DebugFormat("[UDPBASE]: Stopping outbound UDP loop");
  285. IsRunningOutbound = false;
  286. }
  287. private void AsyncBeginReceive()
  288. {
  289. if (!IsRunningInbound)
  290. return;
  291. UDPPacketBuffer buf = GetNewUDPBuffer(new IPEndPoint(IPAddress.Any, 0)); // we need a fresh one here, for now at least
  292. try
  293. {
  294. // kick off an async read
  295. m_udpSocket.BeginReceiveFrom(
  296. buf.Data,
  297. 0,
  298. buf.Data.Length,
  299. SocketFlags.None,
  300. ref buf.RemoteEndPoint,
  301. AsyncEndReceive,
  302. buf);
  303. }
  304. catch (SocketException e)
  305. {
  306. if (e.SocketErrorCode == SocketError.ConnectionReset)
  307. {
  308. m_log.Warn("[UDPBASE]: SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + m_udpPort);
  309. bool salvaged = false;
  310. while (!salvaged)
  311. {
  312. try
  313. {
  314. m_udpSocket.BeginReceiveFrom(
  315. buf.Data,
  316. 0,
  317. buf.Data.Length,
  318. SocketFlags.None,
  319. ref buf.RemoteEndPoint,
  320. AsyncEndReceive,
  321. buf);
  322. salvaged = true;
  323. }
  324. catch (SocketException) { }
  325. catch (ObjectDisposedException) { return; }
  326. }
  327. m_log.Warn("[UDPBASE]: Salvaged the UDP listener on port " + m_udpPort);
  328. }
  329. }
  330. catch (Exception e)
  331. {
  332. m_log.Error(
  333. string.Format("[UDPBASE]: Error processing UDP begin receive {0}. Exception ", UdpReceives), e);
  334. }
  335. }
  336. private void AsyncEndReceive(IAsyncResult iar)
  337. {
  338. // Asynchronous receive operations will complete here through the call
  339. // to AsyncBeginReceive
  340. if (IsRunningInbound)
  341. {
  342. UdpReceives++;
  343. try
  344. {
  345. // get the buffer that was created in AsyncBeginReceive
  346. // this is the received data
  347. UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
  348. int startTick = Util.EnvironmentTickCount();
  349. // get the length of data actually read from the socket, store it with the
  350. // buffer
  351. buffer.DataLength = m_udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint);
  352. // call the abstract method PacketReceived(), passing the buffer that
  353. // has just been filled from the socket read.
  354. PacketReceived(buffer);
  355. // If more than one thread can be calling AsyncEndReceive() at once (e.g. if m_asyncPacketHandler)
  356. // then a particular stat may be inaccurate due to a race condition. We won't worry about this
  357. // since this should be rare and won't cause a runtime problem.
  358. if (m_currentReceiveTimeSamples >= s_receiveTimeSamples)
  359. {
  360. AverageReceiveTicksForLastSamplePeriod
  361. = (float)m_receiveTicksInCurrentSamplePeriod / s_receiveTimeSamples;
  362. m_receiveTicksInCurrentSamplePeriod = 0;
  363. m_currentReceiveTimeSamples = 0;
  364. }
  365. else
  366. {
  367. m_receiveTicksInCurrentSamplePeriod += Util.EnvironmentTickCountSubtract(startTick);
  368. m_currentReceiveTimeSamples++;
  369. }
  370. }
  371. catch (SocketException se)
  372. {
  373. m_log.Error(
  374. string.Format(
  375. "[UDPBASE]: Error processing UDP end receive {0}, socket error code {1}. Exception ",
  376. UdpReceives, se.ErrorCode),
  377. se);
  378. }
  379. catch (Exception e)
  380. {
  381. m_log.Error(
  382. string.Format("[UDPBASE]: Error processing UDP end receive {0}. Exception ", UdpReceives), e);
  383. }
  384. finally
  385. {
  386. AsyncBeginReceive();
  387. }
  388. }
  389. }
  390. /* not in use
  391. public void AsyncBeginSend(UDPPacketBuffer buf)
  392. {
  393. // if (IsRunningOutbound)
  394. // {
  395. // This is strictly for debugging purposes to simulate dropped
  396. // packets when testing throttles & retransmission code
  397. // if (DropOutgoingPacket())
  398. // return;
  399. try
  400. {
  401. m_udpSocket.BeginSendTo(
  402. buf.Data,
  403. 0,
  404. buf.DataLength,
  405. SocketFlags.None,
  406. buf.RemoteEndPoint,
  407. AsyncEndSend,
  408. buf);
  409. }
  410. catch (SocketException) { }
  411. catch (ObjectDisposedException) { }
  412. // }
  413. }
  414. void AsyncEndSend(IAsyncResult result)
  415. {
  416. try
  417. {
  418. // UDPPacketBuffer buf = (UDPPacketBuffer)result.AsyncState;
  419. m_udpSocket.EndSendTo(result);
  420. UdpSends++;
  421. }
  422. catch (SocketException) { }
  423. catch (ObjectDisposedException) { }
  424. }
  425. */
  426. public void SyncSend(UDPPacketBuffer buf)
  427. {
  428. if(buf.RemoteEndPoint == null)
  429. return; // already expired
  430. try
  431. {
  432. m_udpSocket.SendTo(
  433. buf.Data,
  434. 0,
  435. buf.DataLength,
  436. SocketFlags.None,
  437. buf.RemoteEndPoint
  438. );
  439. UdpSends++;
  440. }
  441. catch (SocketException e)
  442. {
  443. m_log.WarnFormat("[UDPBASE]: sync send SocketException {0} {1}", buf.RemoteEndPoint, e.Message);
  444. }
  445. catch (ObjectDisposedException) { }
  446. }
  447. }
  448. }