OpenSimUDPBase.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. buf.DataLength = 0;
  168. return buf;
  169. }
  170. }
  171. return new UDPPacketBuffer(remoteEndpoint);
  172. }
  173. public void FreeUDPBuffer(UDPPacketBuffer buf)
  174. {
  175. lock (m_udpBuffersPoolLock)
  176. {
  177. if(buf.DataLength < 0)
  178. return; // avoid duplicated free that may still happen
  179. if (m_udpBuffersPoolPtr < 999)
  180. {
  181. buf.RemoteEndPoint = null;
  182. buf.DataLength = -1;
  183. m_udpBuffersPoolPtr++;
  184. m_udpBuffersPool[m_udpBuffersPoolPtr] = buf;
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// Start inbound UDP packet handling.
  190. /// </summary>
  191. /// <param name="recvBufferSize">The size of the receive buffer for
  192. /// the UDP socket. This value is passed up to the operating system
  193. /// and used in the system networking stack. Use zero to leave this
  194. /// value as the default</param>
  195. /// <param name="asyncPacketHandling">Set this to true to start
  196. /// receiving more packets while current packet handler callbacks are
  197. /// still running. Setting this to false will complete each packet
  198. /// callback before the next packet is processed</param>
  199. /// <remarks>This method will attempt to set the SIO_UDP_CONNRESET flag
  200. /// on the socket to get newer versions of Windows to behave in a sane
  201. /// manner (not throwing an exception when the remote side resets the
  202. /// connection). This call is ignored on Mono where the flag is not
  203. /// necessary</remarks>
  204. public virtual void StartInbound(int recvBufferSize)
  205. {
  206. if (!IsRunningInbound)
  207. {
  208. m_log.DebugFormat("[UDPBASE]: Starting inbound UDP loop");
  209. const int SIO_UDP_CONNRESET = -1744830452;
  210. IPEndPoint ipep = new IPEndPoint(m_localBindAddress, m_udpPort);
  211. m_udpSocket = new Socket(
  212. AddressFamily.InterNetwork,
  213. SocketType.Dgram,
  214. ProtocolType.Udp);
  215. try
  216. {
  217. if (m_udpSocket.Ttl < 128)
  218. {
  219. m_udpSocket.Ttl = 128;
  220. }
  221. }
  222. catch (SocketException)
  223. {
  224. m_log.Debug("[UDPBASE]: Failed to increase default TTL");
  225. }
  226. try
  227. {
  228. // This udp socket flag is not supported under mono,
  229. // so we'll catch the exception and continue
  230. // Try does not protect some mono versions on mac
  231. // this may work now on all platforms
  232. //if(Util.IsWindows())
  233. //{
  234. m_udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null);
  235. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag set");
  236. //}
  237. //else
  238. //{
  239. // m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
  240. //}
  241. }
  242. catch
  243. {
  244. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
  245. }
  246. // On at least Mono 3.2.8, multiple UDP sockets can bind to the same port by default. At the moment
  247. // we never want two regions to listen on the same port as they cannot demultiplex each other's messages,
  248. // leading to a confusing bug.
  249. // By default, Windows does not allow two sockets to bind to the same port.
  250. //
  251. // Unfortunately, this also causes a crashed sim to leave the socket in a state
  252. // where it appears to be in use but is really just hung from the old process
  253. // crashing rather than closing it. While this protects agains misconfiguration,
  254. // allowing crashed sims to be started up again right away, rather than having to
  255. // wait 2 minutes for the socket to clear is more valuable. Commented 12/13/2016
  256. // m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
  257. if (recvBufferSize != 0)
  258. m_udpSocket.ReceiveBufferSize = recvBufferSize;
  259. m_udpSocket.Bind(ipep);
  260. if (m_udpPort == 0)
  261. m_udpPort = ((IPEndPoint)m_udpSocket.LocalEndPoint).Port;
  262. IsRunningInbound = true;
  263. // kick off an async receive. The Start() method will return, the
  264. // actual receives will occur asynchronously and will be caught in
  265. // AsyncEndRecieve().
  266. AsyncBeginReceive();
  267. }
  268. }
  269. /// <summary>
  270. /// Start outbound UDP packet handling.
  271. /// </summary>
  272. public virtual void StartOutbound()
  273. {
  274. m_log.DebugFormat("[UDPBASE]: Starting outbound UDP loop");
  275. IsRunningOutbound = true;
  276. }
  277. public virtual void StopInbound()
  278. {
  279. if (IsRunningInbound)
  280. {
  281. m_log.DebugFormat("[UDPBASE]: Stopping inbound UDP loop");
  282. IsRunningInbound = false;
  283. m_udpSocket.Close();
  284. }
  285. }
  286. public virtual void StopOutbound()
  287. {
  288. m_log.DebugFormat("[UDPBASE]: Stopping outbound UDP loop");
  289. IsRunningOutbound = false;
  290. }
  291. private void AsyncBeginReceive()
  292. {
  293. if (!IsRunningInbound)
  294. return;
  295. UDPPacketBuffer buf = GetNewUDPBuffer(new IPEndPoint(IPAddress.Any, 0)); // we need a fresh one here, for now at least
  296. try
  297. {
  298. // kick off an async read
  299. m_udpSocket.BeginReceiveFrom(
  300. buf.Data,
  301. 0,
  302. buf.Data.Length,
  303. SocketFlags.None,
  304. ref buf.RemoteEndPoint,
  305. AsyncEndReceive,
  306. buf);
  307. }
  308. catch (SocketException e)
  309. {
  310. if (e.SocketErrorCode == SocketError.ConnectionReset)
  311. {
  312. m_log.Warn("[UDPBASE]: SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + m_udpPort);
  313. bool salvaged = false;
  314. while (!salvaged)
  315. {
  316. try
  317. {
  318. m_udpSocket.BeginReceiveFrom(
  319. buf.Data,
  320. 0,
  321. buf.Data.Length,
  322. SocketFlags.None,
  323. ref buf.RemoteEndPoint,
  324. AsyncEndReceive,
  325. buf);
  326. salvaged = true;
  327. }
  328. catch (SocketException) { }
  329. catch (ObjectDisposedException) { return; }
  330. }
  331. m_log.Warn("[UDPBASE]: Salvaged the UDP listener on port " + m_udpPort);
  332. }
  333. }
  334. catch (Exception e)
  335. {
  336. m_log.Error(
  337. string.Format("[UDPBASE]: Error processing UDP begin receive {0}. Exception ", UdpReceives), e);
  338. }
  339. }
  340. private void AsyncEndReceive(IAsyncResult iar)
  341. {
  342. // Asynchronous receive operations will complete here through the call
  343. // to AsyncBeginReceive
  344. if (IsRunningInbound)
  345. {
  346. UdpReceives++;
  347. try
  348. {
  349. // get the buffer that was created in AsyncBeginReceive
  350. // this is the received data
  351. UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
  352. int startTick = Util.EnvironmentTickCount();
  353. // get the length of data actually read from the socket, store it with the
  354. // buffer
  355. buffer.DataLength = m_udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint);
  356. // call the abstract method PacketReceived(), passing the buffer that
  357. // has just been filled from the socket read.
  358. PacketReceived(buffer);
  359. // If more than one thread can be calling AsyncEndReceive() at once (e.g. if m_asyncPacketHandler)
  360. // then a particular stat may be inaccurate due to a race condition. We won't worry about this
  361. // since this should be rare and won't cause a runtime problem.
  362. if (m_currentReceiveTimeSamples >= s_receiveTimeSamples)
  363. {
  364. AverageReceiveTicksForLastSamplePeriod
  365. = (float)m_receiveTicksInCurrentSamplePeriod / s_receiveTimeSamples;
  366. m_receiveTicksInCurrentSamplePeriod = 0;
  367. m_currentReceiveTimeSamples = 0;
  368. }
  369. else
  370. {
  371. m_receiveTicksInCurrentSamplePeriod += Util.EnvironmentTickCountSubtract(startTick);
  372. m_currentReceiveTimeSamples++;
  373. }
  374. }
  375. catch (SocketException se)
  376. {
  377. m_log.Error(
  378. string.Format(
  379. "[UDPBASE]: Error processing UDP end receive {0}, socket error code {1}. Exception ",
  380. UdpReceives, se.ErrorCode),
  381. se);
  382. }
  383. catch (Exception e)
  384. {
  385. m_log.Error(
  386. string.Format("[UDPBASE]: Error processing UDP end receive {0}. Exception ", UdpReceives), e);
  387. }
  388. finally
  389. {
  390. AsyncBeginReceive();
  391. }
  392. }
  393. }
  394. /* not in use
  395. public void AsyncBeginSend(UDPPacketBuffer buf)
  396. {
  397. // if (IsRunningOutbound)
  398. // {
  399. // This is strictly for debugging purposes to simulate dropped
  400. // packets when testing throttles & retransmission code
  401. // if (DropOutgoingPacket())
  402. // return;
  403. try
  404. {
  405. m_udpSocket.BeginSendTo(
  406. buf.Data,
  407. 0,
  408. buf.DataLength,
  409. SocketFlags.None,
  410. buf.RemoteEndPoint,
  411. AsyncEndSend,
  412. buf);
  413. }
  414. catch (SocketException) { }
  415. catch (ObjectDisposedException) { }
  416. // }
  417. }
  418. void AsyncEndSend(IAsyncResult result)
  419. {
  420. try
  421. {
  422. // UDPPacketBuffer buf = (UDPPacketBuffer)result.AsyncState;
  423. m_udpSocket.EndSendTo(result);
  424. UdpSends++;
  425. }
  426. catch (SocketException) { }
  427. catch (ObjectDisposedException) { }
  428. }
  429. */
  430. public void SyncSend(UDPPacketBuffer buf)
  431. {
  432. if(buf.RemoteEndPoint == null)
  433. return; // already expired
  434. try
  435. {
  436. m_udpSocket.SendTo(
  437. buf.Data,
  438. 0,
  439. buf.DataLength,
  440. SocketFlags.None,
  441. buf.RemoteEndPoint
  442. );
  443. UdpSends++;
  444. }
  445. catch (SocketException e)
  446. {
  447. m_log.WarnFormat("[UDPBASE]: sync send SocketException {0} {1}", buf.RemoteEndPoint, e.Message);
  448. }
  449. catch (ObjectDisposedException) { }
  450. }
  451. }
  452. }