UDPServer.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Timers;
  8. using System.Reflection;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using libsecondlife;
  12. using libsecondlife.Packets;
  13. using OpenSim.Terrain;
  14. using OpenSim.Framework.Interfaces;
  15. using OpenSim.Framework.Types;
  16. using OpenSim.UserServer;
  17. using OpenSim.Assets;
  18. using OpenSim.CAPS;
  19. using OpenSim.Framework.Console;
  20. using OpenSim.Framework;
  21. using Nwc.XmlRpc;
  22. using OpenSim.Servers;
  23. using OpenSim.GenericConfig;
  24. namespace OpenSim
  25. {
  26. public class UDPServer : OpenSimNetworkHandler
  27. {
  28. protected Dictionary<EndPoint, uint> clientCircuits = new Dictionary<EndPoint, uint>();
  29. public Socket Server;
  30. protected IPEndPoint ServerIncoming;
  31. protected byte[] RecvBuffer = new byte[4096];
  32. protected byte[] ZeroBuffer = new byte[8192];
  33. protected IPEndPoint ipeSender;
  34. protected EndPoint epSender;
  35. protected AsyncCallback ReceivedData;
  36. protected PacketServer _packetServer;
  37. protected int listenPort;
  38. protected IWorld m_localWorld;
  39. protected AssetCache m_assetCache;
  40. protected InventoryCache m_inventoryCache;
  41. protected ConsoleBase m_console;
  42. protected AuthenticateSessionsBase m_authenticateSessionsClass;
  43. public PacketServer PacketServer
  44. {
  45. get
  46. {
  47. return _packetServer;
  48. }
  49. set
  50. {
  51. _packetServer = value;
  52. }
  53. }
  54. public IWorld LocalWorld
  55. {
  56. set
  57. {
  58. this.m_localWorld = value;
  59. this._packetServer.LocalWorld = this.m_localWorld;
  60. }
  61. }
  62. public UDPServer()
  63. {
  64. }
  65. public UDPServer(int port, AssetCache assetCache, InventoryCache inventoryCache, ConsoleBase console, AuthenticateSessionsBase authenticateClass)
  66. {
  67. listenPort = port;
  68. this.m_assetCache = assetCache;
  69. this.m_inventoryCache = inventoryCache;
  70. this.m_console = console;
  71. this.m_authenticateSessionsClass = authenticateClass;
  72. this.CreatePacketServer();
  73. }
  74. protected virtual void CreatePacketServer()
  75. {
  76. PacketServer packetServer = new PacketServer(this, (uint) listenPort);
  77. }
  78. protected virtual void OnReceivedData(IAsyncResult result)
  79. {
  80. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  81. epSender = (EndPoint)ipeSender;
  82. Packet packet = null;
  83. int numBytes = Server.EndReceiveFrom(result, ref epSender);
  84. int packetEnd = numBytes - 1;
  85. packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
  86. // do we already have a circuit for this endpoint
  87. if (this.clientCircuits.ContainsKey(epSender))
  88. {
  89. //if so then send packet to the packetserver
  90. this._packetServer.ClientInPacket(this.clientCircuits[epSender], packet);
  91. }
  92. else if (packet.Type == PacketType.UseCircuitCode)
  93. {
  94. // new client
  95. this.AddNewClient(packet);
  96. }
  97. else
  98. { // invalid client
  99. Console.Error.WriteLine("UDPServer.cs:OnReceivedData() - WARNING: Got a packet from an invalid client - " + epSender.ToString());
  100. }
  101. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  102. }
  103. protected virtual void AddNewClient(Packet packet)
  104. {
  105. UseCircuitCodePacket useCircuit = (UseCircuitCodePacket)packet;
  106. this.clientCircuits.Add(epSender, useCircuit.CircuitCode.Code);
  107. this.PacketServer.AddNewClient(epSender, useCircuit, m_assetCache, m_inventoryCache, m_authenticateSessionsClass);
  108. }
  109. public void ServerListener()
  110. {
  111. m_console.WriteLine("UDPServer.cs:ServerListener() - Opening UDP socket on " + listenPort);
  112. ServerIncoming = new IPEndPoint(IPAddress.Any, listenPort);
  113. Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  114. Server.Bind(ServerIncoming);
  115. m_console.WriteLine("UDPServer.cs:ServerListener() - UDP socket bound, getting ready to listen");
  116. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  117. epSender = (EndPoint)ipeSender;
  118. ReceivedData = new AsyncCallback(this.OnReceivedData);
  119. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  120. m_console.WriteLine("UDPServer.cs:ServerListener() - Listening...");
  121. }
  122. public virtual void RegisterPacketServer(PacketServer server)
  123. {
  124. this._packetServer = server;
  125. }
  126. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)//EndPoint packetSender)
  127. {
  128. // find the endpoint for this circuit
  129. EndPoint sendto = null;
  130. foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits)
  131. {
  132. if (p.Value == circuitcode)
  133. {
  134. sendto = p.Key;
  135. break;
  136. }
  137. }
  138. if (sendto != null)
  139. {
  140. //we found the endpoint so send the packet to it
  141. this.Server.SendTo(buffer, size, flags, sendto);
  142. }
  143. }
  144. public virtual void RemoveClientCircuit(uint circuitcode)
  145. {
  146. foreach (KeyValuePair<EndPoint, uint> p in this.clientCircuits)
  147. {
  148. if (p.Value == circuitcode)
  149. {
  150. this.clientCircuits.Remove(p.Key);
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. }