UDPServerBase.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. namespace OpenSim.Servers
  14. {
  15. public class UDPServerBase
  16. {
  17. public Socket Server;
  18. protected IPEndPoint ServerIncoming;
  19. protected byte[] RecvBuffer = new byte[4096];
  20. protected byte[] ZeroBuffer = new byte[8192];
  21. protected IPEndPoint ipeSender;
  22. protected EndPoint epSender;
  23. protected AsyncCallback ReceivedData;
  24. protected int listenPort;
  25. public UDPServerBase(int port)
  26. {
  27. listenPort = port;
  28. }
  29. protected virtual void OnReceivedData(IAsyncResult result)
  30. {
  31. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  32. epSender = (EndPoint)ipeSender;
  33. Packet packet = null;
  34. int numBytes = Server.EndReceiveFrom(result, ref epSender);
  35. int packetEnd = numBytes - 1;
  36. packet = Packet.BuildPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
  37. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  38. }
  39. protected virtual void AddNewClient(Packet packet)
  40. {
  41. }
  42. public virtual void ServerListener()
  43. {
  44. ServerIncoming = new IPEndPoint(IPAddress.Any, listenPort);
  45. Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  46. Server.Bind(ServerIncoming);
  47. ipeSender = new IPEndPoint(IPAddress.Any, 0);
  48. epSender = (EndPoint)ipeSender;
  49. ReceivedData = new AsyncCallback(this.OnReceivedData);
  50. Server.BeginReceiveFrom(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ref epSender, ReceivedData, null);
  51. }
  52. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  53. {
  54. }
  55. }
  56. }