/// <summary> /// 收到peerA的申请,回复两客户端remote /// </summary> /// <param name="userToken"></param> /// <param name="msg"></param> private void ReplyP2PSRequest(IUserToken userToken, ISocketProtocal msg) { var peerA = userToken.ID.ToIPPort(); var natInfoA = new NatInfo() { IP = peerA.Item1, Port = peerA.Item2, IsMe = false }; var IDB = Encoding.UTF8.GetString(msg.Content); var peerB = IDB.ToIPPort(); var natInfoB = new NatInfo() { IP = peerB.Item1, Port = peerB.Item2, IsMe = false }; base.SendAsync(IDB, PSocketMsg.Parse(SerializeHelper.ByteSerialize(natInfoA), TcpP2pType.P2PSResponse).ToBytes()); base.SendAsync(userToken.ID, PSocketMsg.Parse(SerializeHelper.ByteSerialize(natInfoB), TcpP2pType.P2PSResponse).ToBytes()); }
private void ReplyPeerListRequest(IUserToken userToken) { var ipPort = userToken.ID.ToIPPort(); var natInfo = new NatInfo() { IP = ipPort.Item1, Port = ipPort.Item2, IsMe = true }; var data = SerializeHelper.ByteSerialize(natInfo); ReplyBase(userToken, PSocketMsg.Parse(data, TcpP2pType.PublicNatInfoResponse)); }
protected override void OnReceiveBytes(IUserToken userToken, byte[] data) { userToken.Unpacker.Unpack(data, (msg) => { switch (msg.Type) { case (byte)TcpP2pType.Heart: ReplyHeart(userToken); break; case (byte)TcpP2pType.PublicNatInfoRequest: ReplyPeerListRequest(userToken); break; case (byte)TcpP2pType.P2PSRequest: ReplyP2PSRequest(userToken, msg); break; case (byte)TcpP2pType.P2PResponse: var ipPort = userToken.Socket.RemoteEndPoint.ToString().ToIPPort(); var natInfo = new NatInfo() { IP = ipPort.Item1, Port = ipPort.Item2, IsMe = false }; OnP2PSucess?.Invoke(natInfo); ReplyP2PResponse(userToken); break; case (byte)TcpP2pType.Message: OnMessage?.Invoke(userToken, msg); break; case (byte)TcpP2pType.Logout: case (byte)TcpP2pType.Close: default: base.Disconnect(userToken, new Exception("收到来自客户端的关闭请求")); break; } }, null, null); }
protected override void OnReceived(byte[] data) { Actived = DateTime.Now; base.UserToken.Unpacker.Unpack(data, (msg) => { switch (msg.Type) { case (byte)TcpP2pType.Heart: break; case (byte)TcpP2pType.PublicNatInfoRequest: break; case (byte)TcpP2pType.PublicNatInfoResponse: _me = SerializeHelper.ByteDeserialize <NatInfo>(msg.Content); OnPublicNatInfoResponse.Invoke(_me); break; case (byte)TcpP2pType.Message: OnMessage?.Invoke(msg); break; case (byte)TcpP2pType.P2PSResponse: OnReceiveP2PTask?.Invoke(SerializeHelper.ByteDeserialize <NatInfo>(msg.Content)); break; case (byte)TcpP2pType.P2PResponse: OnP2PSucess?.Invoke(_me); break; case (byte)TcpP2pType.Logout: case (byte)TcpP2pType.Close: default: SendClose(); base.Disconnect(); break; } }, null, null); }