Пример #1
0
 public void OnDisconnect(UdpPeer peer)
 {
     if (!Peers.Remove(peer.IpEndPoint))
     {
         Logger.Error("rm peer failed !");
     }
 }
Пример #2
0
 public void OnDisconnected(UdpPeer peer)
 {
     IsConnected = false;
     if (null != OnDisconnectListener)
     {
         OnDisconnectListener(peer);
     }
 }
Пример #3
0
        protected void OnReceive(SocketAsyncEventArgs args)
        {
            try
            {
                if (args.BytesTransferred > 0)
                {
                    var ipEndPoint = args.RemoteEndPoint as IPEndPoint;
                    if (null == ipEndPoint)
                    {
                        Logger.Error("IPEndPoint is null");
                        return;
                    }

                    if (!Peers.ContainsKey(ipEndPoint))
                    {
#if NET_4_6 && UNITY_2017
                        var peer = new UdpPeer(ipEndPoint, args.ConnectSocket);
#else
                        var peer = new UdpPeer(ipEndPoint, RealTimeSocket);
#endif
                        Peers[ipEndPoint]          = peer;
                        peer.OnDisconnectListener += OnDisconnect;
                        peer.OnConnected();
                    }

                    _receiveStream.Write(args.Buffer, args.Offset, args.BytesTransferred);
                    if (null != OnReceiveListener)
                    {
                        OnReceiveListener(Peers[ipEndPoint], _receiveStream);
                    }

                    _receiveStream.Position = 0;
                    _receiveStream.SetLength(0);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw;
            }
            finally
            {
                StartReceive(args);
            }
        }
Пример #4
0
        public void Connect(string ip, int port, long connId)
        {
            var address = IPAddress.Parse(ip);

            _endPoint      = new IPEndPoint(address, port);
            RealTimeSocket = new Socket(_endPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
            var peer = new UdpPeer(_endPoint, RealTimeSocket);

            peer.OnConnected();
            peer.OnDisconnectListener += OnDisconnected;
            Peers.Add(_endPoint, peer);
            if (null != OnConnectListener)
            {
                OnConnectListener(peer);
            }
            StartReceive();
            IsConnected = true;
        }