public void OnDisconnect(UdpPeer peer) { if (!Peers.Remove(peer.IpEndPoint)) { Logger.Error("rm peer failed !"); } }
public void OnDisconnected(UdpPeer peer) { IsConnected = false; if (null != OnDisconnectListener) { OnDisconnectListener(peer); } }
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); } }
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; }