private async UniTask SendConnectAcceptEvent(UdpConnection udp) { Command command = new Command(); command.P2PEventType = CommandType.RequestAccept; command.Value = null; var p = new Command[1]; p[0] = command; var l2 = new UdpPacketL2 { PacketNumber = 0, ACKNumber = 0, Commands = p, }.Serialize(); var packet = new UdpPacket { PeerID = UniP2PManager.MyPeerID, UdpPacketL2 = l2, UdpPacketL2IV = null, }.Serialize(); await UdpSocket.SendAsync(packet, packet.Length, udp.Peer.IPEndPoint); PacketCapture.Write(udp.Peer.IPEndPoint.ToString(), packet.Length, "ConnectAcceptEvent"); Debugger.Log("[SocketUDP] SendConnectAcceptEvent: dist:" + udp.Peer.IPEndPoint.ToString()); }
public async UniTask SendHeartBeatAsync(UdpConnection udp) { byte[] b = { }; Debugger.Log("[SocketUDP] SendHeartBeatAsync:" + udp.Peer.IPEndPoint.ToString()); await SendAsync(udp, b, CommandType.HeartBeat, SocketQosType.Unreliable); PacketCapture.Write(udp.Peer.IPEndPoint.ToString(), b.Length, "HeartBeat"); }
private async UniTask ReceiveConnectAcceptEvent(UdpConnection udp, IPEndPoint ip) { udp.Peer.IPEndPoint = ip; udp.State = UdpConnectionState.KeyExchange; Debugger.Log("[SocketUDP] ReceiveConnectAcceptEvent: dist:" + udp.Peer.IPEndPoint.ToString()); await udp.RequestKey(UdpSocket); var rtt = await GetRTTAsync(udp.Peer.IPEndPoint); udp.RTT = rtt; Debugger.Log("[SocketUDP] Get RTT: " + rtt); }
private async UniTask ReceiveConnectRequestEvent(Peer peer) { var connection = new UdpConnection(peer); UdpConnections.Add(connection); await SendConnectAcceptEvent(connection); connection.State = UdpConnectionState.KeyExchange; var rtt = await GetRTTAsync(peer.IPEndPoint); connection.RTT = rtt; Debugger.Log("[SocketUDP] Receive Connect Request Event:" + peer.IPEndPoint.Address.ToString() + ":" + peer.IPEndPoint.Port.ToString()); }
public async UniTask SendAsync(UdpConnection udp, byte[] data, CommandType p2PEvent = CommandType.Nothing, SocketQosType qosType = SocketQosType.Unreliable) { Command packetObject = new Command(); packetObject.P2PEventType = p2PEvent; packetObject.Value = data; if (udp != null) { if (qosType == SocketQosType.Reliable) { await udp.AddReliableCommand(packetObject); } else if (qosType == SocketQosType.Unreliable) { udp.AddUnreliableCommand(packetObject); } } }
public async UniTask <Peer> ConnectPeerAsync(IPEndPoint ip, string peerid = "", int localport = 0) { var peer = new Peer { State = PeerState.Connecting, ID = peerid, }; var udp = new UdpConnection(peer); UdpConnections.Add(udp); udp.State = UdpConnectionState.RequestSend; int i = 0; do { await SendConnectRequestEvent(ip); if (i >= 3 && localport != 0) { Debugger.Log("[SocketUDP] LAN Connect Port:" + localport); await SendConnectRequestEvent(ip, true, localport); } if (i >= 5) { Debugger.Error("[SocketUDP] Timeout: " + ip.ToString()); UniP2PManager.RemovePeer(peer); return(null); } await UniTask.Delay(1000); i++; }while (peer.State != PeerState.Connected); return(peer); }