コード例 #1
0
 public override void ConnectAsync(IPEndPoint localAddress, IPEndPoint remoteAddress)
 {
     udpRemote = new UdpEndPoint(new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes())), (ushort)remoteAddress.Port);
     remote    = remoteAddress;
     if (!IsConnected() && !IsConnecting())
     {
         socket.Connect(udpRemote);
         object statusLock;
         Monitor.Enter(statusLock = this.statusLock);
         try
         {
             connectionStatus = ConnectionStatus.Connecting;
         }
         finally
         {
             Monitor.Exit(statusLock);
         }
     }
 }
コード例 #2
0
        public override bool ConnectSync(IPEndPoint localAddress, IPEndPoint remoteAddress, int timeout)
        {
            long num = DateTime.Now.ToFileTime() / 10000L;
            bool result;

            if (IsConnected())
            {
                UdpIPv4Address udpIPv4Address = new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes()));
                UdpIPv4Address address        = this.udpRemote.Address;
                if (address.Equals(udpIPv4Address) && (int)udpRemote.Port == remoteAddress.Port)
                {
                    result = true;
                    return(result);
                }
                LoggerManager.Instance.Warn("ConnectSync found already connected new addr {0} old addr {1}", remoteAddress.ToString(), udpRemote.ToString());
            }

            if (!IsConnecting())
            {
                udpRemote = new UdpEndPoint(new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes())), (ushort)remoteAddress.Port);
                remote    = remoteAddress;
                socket.Connect(udpRemote);
            }

            object obj = statusLock;

            Monitor.Enter(obj);
            try
            {
                connectionStatus = ConnectionStatus.Connecting;
            }
            finally
            {
                Monitor.Exit(obj);
            }

            long num2 = DateTime.Now.ToFileTime() / 10000L;

            while (num2 - num < (long)(timeout * 1000))
            {
                UdpEvent ev;
                if (socket.Poll(out ev))
                {
                    UdpEventType eventType = ev.EventType;
                    switch (eventType)
                    {
                    case UdpEventType.ConnectFailed:
                    case UdpEventType.ConnectRefused:
                    case UdpEventType.Connected:
                        ProcessConnect(ev);
                        result = (ev.EventType == UdpEventType.Connected);
                        return(result);

                    case (UdpEventType)5:
                    case (UdpEventType)7:
                        break;

                    default:
                        if (eventType == UdpEventType.ServerForceQuit)
                        {
                            ProcessServerForceQuit(ev);
                            result = false;
                            return(result);
                        }
                        break;
                    }
                }
                Thread.Sleep(10);
                num2 = DateTime.Now.ToFileTime() / 10000L;
            }

            Monitor.Enter(obj = this.statusLock);
            try
            {
                connectionStatus = ConnectionStatus.Disconnected;
            }
            finally
            {
                Monitor.Exit(obj);
            }
            result = false;
            return(result);
        }