OpenSimUDPBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. m_udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null);
  229. }
  230. catch
  231. {
  232. m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
  233. }
  234. // On at least Mono 3.2.8, multiple UDP sockets can bind to the same port by default. At the moment
  235. // we never want two regions to listen on the same port as they cannot demultiplex each other's messages,
  236. // leading to a confusing bug.
  237. // By default, Windows does not allow two sockets to bind to the same port.
  238. //
  239. // Unfortunately, this also causes a crashed sim to leave the socket in a state
  240. // where it appears to be in use but is really just hung from the old process
  241. // crashing rather than closing it. While this protects agains misconfiguration,
  242. // allowing crashed sims to be started up again right away, rather than having to
  243. // wait 2 minutes for the socket to clear is more valuable. Commented 12/13/2016
  244. // m_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
  245. if (recvBufferSize != 0)
  246. m_udpSocket.ReceiveBufferSize = recvBufferSize;
  247. m_udpSocket.Bind(ipep);
  248. if (m_udpPort == 0)
  249. m_udpPort = ((IPEndPoint)m_udpSocket.LocalEndPoint).Port;
  250. IsRunningInbound = true;
  251. // kick off an async receive. The Start() method will return, the
  252. // actual receives will occur asynchronously and will be caught in
  253. // AsyncEndRecieve().
  254. AsyncBeginReceive();
  255. }
  256. }
  257. /// <summary>
  258. /// Start outbound UDP packet handling.
  259. /// </summary>
  260. public virtual void StartOutbound()
  261. {
  262. m_log.DebugFormat("[UDPBASE]: Starting outbound UDP loop");
  263. IsRunningOutbound = true;
  264. }
  265. public virtual void StopInbound()
  266. {
  267. if (IsRunningInbound)
  268. {
  269. m_log.DebugFormat("[UDPBASE]: Stopping inbound UDP loop");
  270. IsRunningInbound = false;
  271. m_udpSocket.Close();
  272. }
  273. }
  274. public virtual void StopOutbound()
  275. {
  276. m_log.DebugFormat("[UDPBASE]: Stopping outbound UDP loop");
  277. IsRunningOutbound = false;
  278. }
  279. private void AsyncBeginReceive()
  280. {
  281. while(IsRunningInbound)
  282. {
  283. UDPPacketBuffer buf = GetNewUDPBuffer(new IPEndPoint(IPAddress.Any, 0)); // we need a fresh one here, for now at least
  284. try
  285. {
  286. // kick off an async read
  287. IAsyncResult iar = m_udpSocket.BeginReceiveFrom(
  288. buf.Data,
  289. 0,
  290. buf.Data.Length,
  291. SocketFlags.None,
  292. ref buf.RemoteEndPoint,
  293. AsyncEndReceive,
  294. buf);
  295. if (!iar.CompletedSynchronously)
  296. return;
  297. }
  298. catch (SocketException e)
  299. {
  300. if (e.SocketErrorCode == SocketError.ConnectionReset)
  301. {
  302. m_log.Warn("[UDPBASE]: SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + m_udpPort);
  303. {
  304. try
  305. {
  306. IAsyncResult iar = m_udpSocket.BeginReceiveFrom(
  307. buf.Data,
  308. 0,
  309. buf.Data.Length,
  310. SocketFlags.None,
  311. ref buf.RemoteEndPoint,
  312. AsyncEndReceive,
  313. buf);
  314. if (!iar.CompletedSynchronously)
  315. return;
  316. }
  317. catch (SocketException) { }
  318. catch (ObjectDisposedException) { return; }
  319. }
  320. m_log.Warn("[UDPBASE]: Salvaged the UDP listener on port " + m_udpPort);
  321. }
  322. }
  323. catch (Exception e)
  324. {
  325. m_log.Error(
  326. string.Format("[UDPBASE]: Error processing UDP begin receive {0}. Exception ", UdpReceives), e);
  327. }
  328. }
  329. }
  330. private void AsyncEndReceive(IAsyncResult iar)
  331. {
  332. if (IsRunningInbound)
  333. {
  334. bool sync = iar.CompletedSynchronously;
  335. try
  336. {
  337. // get the buffer that was created in AsyncBeginReceive
  338. // this is the received data
  339. UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
  340. int startTick = Util.EnvironmentTickCount();
  341. // get the length of data actually read from the socket, store it with the
  342. // buffer
  343. buffer.DataLength = m_udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint);
  344. UdpReceives++;
  345. // call the abstract method PacketReceived(), passing the buffer that
  346. // has just been filled from the socket read.
  347. PacketReceived(buffer);
  348. // If more than one thread can be calling AsyncEndReceive() at once (e.g. if m_asyncPacketHandler)
  349. // then a particular stat may be inaccurate due to a race condition. We won't worry about this
  350. // since this should be rare and won't cause a runtime problem.
  351. if (m_currentReceiveTimeSamples >= s_receiveTimeSamples)
  352. {
  353. AverageReceiveTicksForLastSamplePeriod
  354. = (float)m_receiveTicksInCurrentSamplePeriod / s_receiveTimeSamples;
  355. m_receiveTicksInCurrentSamplePeriod = 0;
  356. m_currentReceiveTimeSamples = 0;
  357. }
  358. else
  359. {
  360. m_receiveTicksInCurrentSamplePeriod += Util.EnvironmentTickCountSubtract(startTick);
  361. m_currentReceiveTimeSamples++;
  362. }
  363. }
  364. catch (SocketException se)
  365. {
  366. m_log.Error(
  367. string.Format(
  368. "[UDPBASE]: Error processing UDP end receive {0}, socket error code {1}. Exception ",
  369. UdpReceives, se.ErrorCode),
  370. se);
  371. }
  372. catch (Exception e)
  373. {
  374. m_log.Error(
  375. string.Format("[UDPBASE]: Error processing UDP end receive {0}. Exception ", UdpReceives), e);
  376. }
  377. finally
  378. {
  379. if (IsRunningInbound && !sync)
  380. AsyncBeginReceive();
  381. }
  382. }
  383. }
  384. public void SyncSend(UDPPacketBuffer buf)
  385. {
  386. if(buf.RemoteEndPoint == null)
  387. return; // already expired
  388. try
  389. {
  390. m_udpSocket.SendTo(
  391. buf.Data,
  392. 0,
  393. buf.DataLength,
  394. SocketFlags.None,
  395. buf.RemoteEndPoint
  396. );
  397. UdpSends++;
  398. }
  399. catch (SocketException e)
  400. {
  401. m_log.WarnFormat("[UDPBASE]: sync send SocketException {0} {1}", buf.RemoteEndPoint, e.Message);
  402. }
  403. catch (ObjectDisposedException) { }
  404. }
  405. }
  406. }