Example #1
0
        /// <summary>
        /// 收到服务器P2PSResponse, 向打洞的远程机器发送数据
        /// </summary>
        /// <param name="msg"></param>
        private void HolePunching(ISocketProtocal msg)
        {
            var ipPort = Encoding.UTF8.GetString(msg.Content).GetIPPort();

            OnReceiveP2PTask?.Invoke(ipPort);

            var qm = PSocketMsg.Parse(null, HolePunchingType.P2PRequest);

            SendTo(new System.Net.IPEndPoint(IPAddress.Parse(ipPort.Item1), ipPort.Item2), qm.ToBytes());

            _peerBAddress = ipPort;
        }
Example #2
0
        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);
        }