UDPServer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using libsecondlife.Packets;
  33. using OpenSim.Assets;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Interfaces;
  37. using OpenSim.Region.Caches;
  38. namespace OpenSim.Region.ClientStack
  39. {
  40. public class UDPServer : ClientStackNetworkHandler
  41. {
  42. protected Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>();
  43. public Socket Server;
  44. protected IPEndPoint ServerIncoming;
  45. protected byte[] RecvBuffer = new byte[4096];
  46. protected byte[] ZeroBuffer = new byte[8192];
  47. protected IPEndPoint ipeSender;
  48. protected EndPoint epSender;
  49. protected AsyncCallback ReceivedData;
  50. protected PacketServer _packetServer;
  51. protected int listenPort;
  52. protected IWorld m_localWorld;
  53. protected AssetCache m_assetCache;
  54. protected InventoryCache m_inventoryCache;
  55. protected LogBase m_log;
  56. protected AuthenticateSessionsBase m_authenticateSessionsClass;
  57. public PacketServer PacketServer
  58. {
  59. get
  60. {
  61. return _packetServer;
  62. }
  63. set
  64. {
  65. _packetServer = value;
  66. }
  67. }
  68. public IWorld LocalWorld
  69. {
  70. set
  71. {
  72. this.m_localWorld = value;
  73. this._packetServer.LocalWorld = this.m_localWorld;
  74. }
  75. }
  76. public UDPServer()
  77. {
  78. }
  79. public UDPServer(int port, AssetCache assetCache, InventoryCache inventoryCache, LogBase console, AuthenticateSessionsBase authenticateClass)
  80. {
  81. listenPort = port;
  82. this.m_assetCache = assetCache;
  83. this.m_inventoryCache = inventoryCache;
  84. this.m_log = console;
  85. this.m_authenticateSessionsClass = authenticateClass;
  86. this.CreatePacketServer();
  87. }
  88. protected virtual void CreatePacketServer()
  89. {
  90. PacketServer packetServer = new PacketServer(this);
  91. }
  92. protected virtual void OnReceivedData(IAsyncResult result)
  93. {
  94. ipeSender = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  95. epSender = (EndPoint)ipeSender;
  96. Packet packet = null;
  97. int numBytes = Server.EndReceiveFrom(result, ref epSender);
  98. int packetEnd = numBytes - 1;
  99. packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
  100. // do we already have a circuit for this endpoint
  101. if (this.clientCircuits.ContainsKey(epSender))
  102. {
  103. //if so then send packet to the packetserver
  104. this._packetServer.ClientInPacket(this.clientCircuits[epSender], packet);
  105. }
  106. else if (packet.Type == PacketType.UseCircuitCode)
  107. {
  108. // new client
  109. this.AddNewClient(packet);
  110. }
  111. else
  112. { // invalid client
  113. m_log.Warn("UDPServer.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString());
  114. }
  115. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  116. }
  117. protected virtual void AddNewClient(Packet packet)
  118. {
  119. UseCircuitCodePacket useCircuit = (UseCircuitCodePacket)packet;
  120. this.clientCircuits.Add(epSender, useCircuit.CircuitCode.Code);
  121. this.PacketServer.AddNewClient(epSender, useCircuit, m_assetCache, m_inventoryCache, m_authenticateSessionsClass);
  122. }
  123. public void ServerListener()
  124. {
  125. m_log.Status("UDPServer.cs:ServerListener() - Opening UDP socket on " + listenPort);
  126. ServerIncoming = new IPEndPoint(IPAddress.Parse("0.0.0.0"), listenPort);
  127. Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  128. Server.Bind(ServerIncoming);
  129. m_log.Verbose("UDPServer.cs:ServerListener() - UDP socket bound, getting ready to listen");
  130. ipeSender = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  131. epSender = (EndPoint)ipeSender;
  132. ReceivedData = new AsyncCallback(this.OnReceivedData);
  133. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  134. m_log.Verbose("UDPServer.cs:ServerListener() - Listening...");
  135. }
  136. public virtual void RegisterPacketServer(PacketServer server)
  137. {
  138. this._packetServer = server;
  139. }
  140. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)//EndPoint packetSender)
  141. {
  142. // find the endpoint for this circuit
  143. EndPoint sendto = null;
  144. foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits)
  145. {
  146. if (p.Value == circuitcode)
  147. {
  148. sendto = p.Key;
  149. break;
  150. }
  151. }
  152. if (sendto != null)
  153. {
  154. //we found the endpoint so send the packet to it
  155. this.Server.SendTo(buffer, size, flags, sendto);
  156. }
  157. }
  158. public virtual void RemoveClientCircuit(uint circuitcode)
  159. {
  160. foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits)
  161. {
  162. if (p.Value == circuitcode)
  163. {
  164. this.clientCircuits.Remove(p.Key);
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. }