private void _p2pConnect_OnMessage(IUserToken arg1, ISocketProtocal arg2) { if (arg2.Content != null) { OnMessage?.Invoke(arg2.Content); } }
/// <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 static void Client_OnReceive(UDPClient arg1, ISocketProtocal arg2) { lock (_locker) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"udp客户端收到消息:{Encoding.UTF8.GetString(arg2.Content)}"); } }
/// <summary> /// 收到peerA的申请,回复两客户端remote /// </summary> /// <param name="userToken"></param> /// <param name="msg"></param> private void ReplyP2PSRequest(IUserToken userToken, ISocketProtocal msg) { var peerA = userToken.ID; var peerB = Encoding.UTF8.GetString(msg.Content); base.SendAsync(peerA, PSocketMsg.Parse(Encoding.UTF8.GetBytes(peerB), HolePunchingType.P2PSResponse).ToBytes()); base.SendAsync(peerB, PSocketMsg.Parse(Encoding.UTF8.GetBytes(peerA), HolePunchingType.P2PSResponse).ToBytes()); }
private static void Server_OnReceive(UDPServer arg1, string arg2, ISocketProtocal arg3) { lock (_locker) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"udp服务器收到消息:{Encoding.UTF8.GetString(arg3.Content)}"); } var msg2 = BaseSocketProtocal.Parse(Encoding.UTF8.GetBytes($"udpserver reply:{Encoding.UTF8.GetString(arg3.Content)}"), SocketProtocalType.ChatMessage); arg1.SendAsync(arg2, msg2); }
/// <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; }
private static void Client_OnReceive(UDPClient <BaseUnpacker> arg1, ISocketProtocal arg2) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"udp client received a message:{Encoding.UTF8.GetString(arg2.Content)}"); }
private static void Server_OnReceive(UDPServer <BaseUnpacker> arg1, string arg2, ISocketProtocal arg3) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"udp server received a message:{Encoding.UTF8.GetString(arg3.Content)}"); arg1.SendAsync(arg2, Encoding.UTF8.GetBytes($"udpserver reply:{Encoding.UTF8.GetString(arg3.Content)}")); }