Example #1
0
 public void RemoveUdpAgent(UDPAgent agent)
 {
     lock (udpAgents)
     {
         udpAgents.Remove(agent.remoteEnd);
     }
 }
Example #2
0
        private void OnReceiveUDP(IAsyncResult result)        //接收到UDP消息
        {
            if (udpClient == null)
            {
                return;
            }
            try
            {
                var udpPort = new IPEndPoint(IPAddress.Any, 0);
                var bytes   = udpClient.EndReceive(result, ref udpPort);
                if (bytes.Length > 0)
                {
                    UDPAgent ag1 = null;
                    lock (udpAgents)
                    {
                        //远程客户端不支持UDP连接 网络无法连接上 UDP穿透失败
                        if (!udpAgents.ContainsKey(udpPort))
                        {
                            var ag = new UDPAgent(udpPort, this, udpClient);
                            udpAgents.Add(udpPort, ag);
                        }
                        ag1 = udpAgents[udpPort];
                    }
                    if (ag1 != null)
                    {
                        ag1.ReceiveData(bytes);
                    }
                }
                else
                {
                    LogHelper.Log("UDP", "Error Receive 0");
                }

                udpClient.BeginReceive(OnReceiveUDP, null);                  //开启下一次UDP消息连接
            }
            catch (Exception exp)
            {
                LogHelper.Log("Error", exp.ToString());
            }
        }
Example #3
0
 public void SetUDPAgent(UDPAgent ud)
 {
     udpAgent = ud;
 }