public async UniTask DisConnectAsync(Peer peer) { if (peer.IPEndPoint != null) { var udp = GetUdpConnection(peer.ID); byte[] b = { }; await SendAsync(udp, b, CommandType.Disconnect, SocketQosType.Unreliable); udp.DisConnect(); UdpConnections.Remove(udp); var e = new P2PEventArgs(peer, CommandType.Disconnect, null); ReceiveSubject.OnNext(e); } }
private async UniTask OnReceiveCompletedAsync(byte[] buffer, IPEndPoint remote) { if (Equals(remote, UniP2PManager.PrivateIPEndPoint) || Equals(remote, UniP2PManager.StunIPEndPoint)) { Debugger.Warning("[UniP2PManager] IPEndPoint is MySelf."); return; } UdpPacket packet = null; try { packet = UdpPacket.Deserialize(buffer); } catch (Exception ex) { //Debugger.Warning(ex.Message + remote.ToString()); return; } if (packet.PeerID != null) { var udp = GetUdpConnection(packet.PeerID); if (udp != null) { if (udp.State == UdpConnectionState.Connected) { if (packet.isEncrypt()) { var decrypt = AES.Decrypt(packet.UdpPacketL2, udp.AESKey, packet.UdpPacketL2IV); var l2 = UdpPacketL2.Deserialize(decrypt); if (l2.ACKNumber != 0) { udp.ReceiveACKPacket(l2.ACKNumber); } //Reliable if (l2.PacketNumber != 0) { //Reply ACK } foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.Disconnect) { Debugger.Log("[SocketUDP] DisConnect PeerID:" + udp.Peer.ID); udp.DisConnect(); UdpConnections.Remove(udp); UniP2PManager.RemovePeer(udp.Peer); return; } else if (command.P2PEventType == CommandType.HeartBeat) { udp.ReceiveHeartBeat(); } else if (command.P2PEventType == CommandType.Nothing || command.P2PEventType == CommandType.DataEvent) { var e = new P2PEventArgs(udp.Peer, command.P2PEventType, command.Value); ReceiveSubject.OnNext(e); } } } else { var l2 = UdpPacketL2.Deserialize(packet.UdpPacketL2); foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.HeartBeat) { udp.ReceiveHeartBeat(); } } } } else if (udp.State == UdpConnectionState.KeyExchange) { var l2 = UdpPacketL2.Deserialize(packet.UdpPacketL2); foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.RequestKey) { await udp.CreateKey(command.Value, UdpSocket, this); break; } else if (command.P2PEventType == CommandType.AcceptKey) { udp.AcceptKey(command.Value, this); break; } } } else if (udp.State == UdpConnectionState.RequestSend) { var l2 = UdpPacketL2.Deserialize(packet.UdpPacketL2); foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.RequestAccept) { await ReceiveConnectAcceptEvent(udp, remote); break; } } } else if (udp.State == UdpConnectionState.DisConnect) { udp.DisConnect(); return; } } else { //Not Found Udp Connection var l2 = UdpPacketL2.Deserialize(packet.UdpPacketL2); foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.ConnectRequest) { var peer = new Peer { ID = packet.PeerID, IPEndPoint = remote, }; await ReceiveConnectRequestEvent(peer); } else if (command.P2PEventType == CommandType.PingRequest) { await SendPingResponsePacketAsync(remote, PingPacket.Deserialize(command.Value).PingID); } else if (command.P2PEventType == CommandType.PingResponse) { ReceivePingResponsePacketAsync(PingPacket.Deserialize(command.Value).PingID); } } } } else { var l2 = UdpPacketL2.Deserialize(packet.UdpPacketL2); foreach (var command in l2.Commands) { if (command.P2PEventType == CommandType.PingRequest) { await SendPingResponsePacketAsync(remote, PingPacket.Deserialize(command.Value).PingID); } else if (command.P2PEventType == CommandType.PingResponse) { ReceivePingResponsePacketAsync(PingPacket.Deserialize(command.Value).PingID); } } } }