123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using log4net;
- namespace OpenMetaverse
- {
-
-
-
- public abstract class OpenSimUDPBase
- {
- private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
-
-
-
-
- protected abstract void PacketReceived(UDPPacketBuffer buffer);
-
- protected int m_udpPort;
-
- protected IPAddress m_localBindAddress;
-
- private Socket m_udpSocket;
-
- private bool m_asyncPacketHandling;
-
- private volatile bool m_shutdownFlag = true;
-
- public bool IsRunning { get { return !m_shutdownFlag; } }
-
-
-
-
-
- public OpenSimUDPBase(IPAddress bindAddress, int port)
- {
- m_localBindAddress = bindAddress;
- m_udpPort = port;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Start(int recvBufferSize, bool asyncPacketHandling)
- {
- m_asyncPacketHandling = asyncPacketHandling;
- if (m_shutdownFlag)
- {
- const int SIO_UDP_CONNRESET = -1744830452;
- IPEndPoint ipep = new IPEndPoint(m_localBindAddress, m_udpPort);
-
- m_log.DebugFormat(
- "[UDPBASE]: Binding UDP listener using internal IP address config {0}:{1}",
- ipep.Address, ipep.Port);
- m_udpSocket = new Socket(
- AddressFamily.InterNetwork,
- SocketType.Dgram,
- ProtocolType.Udp);
- try
- {
-
-
- m_udpSocket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0 }, null);
- m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag set");
- }
- catch (SocketException)
- {
- m_log.Debug("[UDPBASE]: SIO_UDP_CONNRESET flag not supported on this platform, ignoring");
- }
- if (recvBufferSize != 0)
- m_udpSocket.ReceiveBufferSize = recvBufferSize;
- m_udpSocket.Bind(ipep);
-
- m_shutdownFlag = false;
-
-
-
- AsyncBeginReceive();
- }
- }
-
-
-
- public void Stop()
- {
- if (!m_shutdownFlag)
- {
-
-
-
-
- m_shutdownFlag = true;
- m_udpSocket.Close();
- }
- }
- private void AsyncBeginReceive()
- {
-
-
- UDPPacketBuffer buf = new UDPPacketBuffer();
- if (!m_shutdownFlag)
- {
- try
- {
-
- m_udpSocket.BeginReceiveFrom(
-
- buf.Data,
- 0,
- UDPPacketBuffer.BUFFER_SIZE,
- SocketFlags.None,
- ref buf.RemoteEndPoint,
- AsyncEndReceive,
-
- buf);
- }
- catch (SocketException e)
- {
- if (e.SocketErrorCode == SocketError.ConnectionReset)
- {
- m_log.Warn("[UDPBASE]: SIO_UDP_CONNRESET was ignored, attempting to salvage the UDP listener on port " + m_udpPort);
- bool salvaged = false;
- while (!salvaged)
- {
- try
- {
- m_udpSocket.BeginReceiveFrom(
-
- buf.Data,
- 0,
- UDPPacketBuffer.BUFFER_SIZE,
- SocketFlags.None,
- ref buf.RemoteEndPoint,
- AsyncEndReceive,
-
- buf);
- salvaged = true;
- }
- catch (SocketException) { }
- catch (ObjectDisposedException) { return; }
- }
- m_log.Warn("[UDPBASE]: Salvaged the UDP listener on port " + m_udpPort);
- }
- }
- catch (ObjectDisposedException) { }
- }
- }
- private void AsyncEndReceive(IAsyncResult iar)
- {
-
-
- if (!m_shutdownFlag)
- {
-
-
- if (m_asyncPacketHandling)
- AsyncBeginReceive();
-
-
-
-
- UDPPacketBuffer buffer = (UDPPacketBuffer)iar.AsyncState;
- try
- {
-
-
- buffer.DataLength = m_udpSocket.EndReceiveFrom(iar, ref buffer.RemoteEndPoint);
-
-
- PacketReceived(buffer);
- }
- catch (SocketException) { }
- catch (ObjectDisposedException) { }
- finally
- {
-
-
-
- if (!m_asyncPacketHandling)
- AsyncBeginReceive();
- }
- }
- }
- public void AsyncBeginSend(UDPPacketBuffer buf)
- {
- if (!m_shutdownFlag)
- {
- try
- {
- m_udpSocket.BeginSendTo(
- buf.Data,
- 0,
- buf.DataLength,
- SocketFlags.None,
- buf.RemoteEndPoint,
- AsyncEndSend,
- buf);
- }
- catch (SocketException) { }
- catch (ObjectDisposedException) { }
- }
- }
- void AsyncEndSend(IAsyncResult result)
- {
- try
- {
- m_udpSocket.EndSendTo(result);
- }
- catch (SocketException) { }
- catch (ObjectDisposedException) { }
- }
- }
- }
|