Пример #1
0
 public static void ShutDown()
 {
     if (info != null)
     {
         info.Close();
         info = null;
         Debug.Log("关闭udp服务器成功");
     }
     else
     {
         Debug.LogWarning("重复udp服务器");
     }
 }
Пример #2
0
        public static void Listen()
        {
            if (!CheckNet())
            {
                return;
            }
            if (info != null)
            {
                Debug.LogWarning("请勿重复开启udp服务器");
            }
            else
            {
#if XRUWP && !UNITY_EDITOR
                info = new UdpInfo(Port.ToString());
#else
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, Port);
                info = new UdpInfo(ip);
#endif
                info.Receive();
                Debug.Log("upd服务器开启,开始监听:" + ip);
            }
        }
Пример #3
0
        /// <summary>
        /// Open the connection to the EV3 over a WiFi connection - this will block
        /// </summary>
        public override void Open()
        {
            bool      hasError          = false;
            bool      failedToLocateEV3 = true;
            int       listenPort        = 3015;
            int       tcpIpPort         = 5555;
            UdpClient listener          = null;
            UdpClient sender            = null;

            try
            {
                listener = new UdpClient(listenPort);
                IPEndPoint groupEP    = new IPEndPoint(IPAddress.Any, listenPort);
                byte[]     bytes      = null;
                var        resetEvent = new ManualResetEvent(false);
                Thread t = new Thread(
                    new ThreadStart(
                        delegate()
                {
                    try{
                        bytes = listener.Receive(ref groupEP);
                        resetEvent.Set();
                    }
                    catch {
                    }
                }
                        ));
                t.IsBackground = true;
                t.Priority     = ThreadPriority.Normal;
                t.Start();
                if (timeOut != 0)
                {
                    if (!resetEvent.WaitOne(timeOut))
                    {
                        listener.Close();
                    }
                }
                else
                {
                    resetEvent.WaitOne();                     //wait forever
                }
                if (bytes != null)
                {
                    failedToLocateEV3 = false;
                    UdpInfo udpInfo = new UdpInfo(System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length));
                    Thread.Sleep(100);
                    sender = new UdpClient();
                    sender.Send(new byte[] { 0x00 }, 1, groupEP);
                    Thread.Sleep(100);
                    tcpClient                = new TcpClient(groupEP.Address.ToString(), tcpIpPort);
                    tcpClient.NoDelay        = true;
                    stream                   = tcpClient.GetStream();
                    tcpClient.SendTimeout    = 3000;
                    tcpClient.ReceiveTimeout = 3000;
                    stream.Write(udpInfo.TcpUnlockData, 0, udpInfo.TcpUnlockData.Length);
                    byte[] unlockReply = new byte[16];
                    stream.ReadAll(unlockReply);
                }
                else
                {
                    hasError = true;
                }
            }
            catch
            {
                hasError = true;
            }
            finally
            {
                if (listener != null)
                {
                    listener.Close();
                }
                if (sender != null)
                {
                    sender.Close();
                }
            }
            if (hasError)
            {
                if (failedToLocateEV3)
                {
                    throw new ConnectionException(ConnectionError.OpenError, new Exception("Failed to find EV3"));
                }
                else
                {
                    throw new ConnectionException(ConnectionError.OpenError);
                }
            }
        }