Example #1
0
        private async UniTask SendPingResponsePacketAsync(IPEndPoint ip, int id)
        {
            try
            {
                var commands = new Command[1];
                commands[0] = new Command
                {
                    P2PEventType = CommandType.PingResponse, Value = new PingPacket {
                        PingID = id
                    }.Serialize()
                };
                var l2 = new UdpPacketL2
                {
                    ACKNumber    = 0,
                    PacketNumber = 0,
                    Commands     = commands,
                }.Serialize();

                var packet = new UdpPacket
                {
                    PeerID        = null,
                    UdpPacketL2   = l2,
                    UdpPacketL2IV = null
                }.Serialize();

                await UdpSocket.SendAsync(packet, packet.Length, ip);

                PacketCapture.Write(ip.ToString(), packet.Length, "PingResponse");
            }
            catch (ObjectDisposedException ex)
            {
                Debugger.Error(ex.Message);
                Debugger.Log("[SocketUDP] Closed");
            }
        }
Example #2
0
        public async UniTask CreateKey(byte[] buffer, UdpClient udpSocket ,SocketUdp socketUdp)
        {
            var publicKey = RSAPublicKey.Deserialize(buffer);
            RSA = new RSA();
            var key = RSA.CreateKey(publicKey.Modules, publicKey.Exponent);
            AESKey = key.aeskey;

            Command obj = new Command
            {
                P2PEventType = CommandType.AcceptKey,
                Value = key.encrypted,
            };
            var array = new Command[1];
            array[0] = obj;

            var l2 = new UdpPacketL2
            {
                Commands = array,
                PacketNumber = 0,
                ACKNumber = 0,
            }.Serialize();

            var buf = new UdpPacket
            {
                PeerID = UniP2PManager.MyPeerID,
                UdpPacketL2 = l2,
                UdpPacketL2IV = null
            }.Serialize();
            
            await udpSocket.SendAsync(buf, buf.Length, Peer.IPEndPoint);
            PacketCapture.Write(Peer.IPEndPoint.ToString(), buf.Length, "CreateKey");
            State = UdpConnectionState.Connected;
            Peer.State = PeerState.Connected;
            InitHeartBeatAsync(socketUdp);
        }
Example #3
0
        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());
        }
Example #4
0
        public async UniTask RequestKey(UdpClient UdpSocket)
        {
            State = UdpConnectionState.KeyExchange;
            RSA = new RSA();
            var publickey = RSA.RequestKey();
            
            Command obj = new Command
            {
                P2PEventType = CommandType.RequestKey,
                Value = publickey.Serialize(),
            };
            var array = new Command[1];
            array[0] = obj;
            var l2 = new UdpPacketL2
            {
                Commands = array,
                PacketNumber = 0,
                ACKNumber = 0,
            }.Serialize();

            var buf = new UdpPacket
            {
                PeerID = UniP2PManager.MyPeerID,
                UdpPacketL2 = l2,
                UdpPacketL2IV = null
            }.Serialize();

            await UdpSocket.SendAsync(buf, buf.Length, Peer.IPEndPoint);
            PacketCapture.Write(Peer.IPEndPoint.ToString(),buf.Length, "RequestKey");
        }
Example #5
0
        public async UniTask SendUnreliable(UdpClient UdpSocket)
        {
            if (UnreliableCommandBuffer.ToArray().Length != 0)
            {
                var l2 = new UdpPacketL2
                {
                    PacketNumber = 0,
                    ACKNumber = 0,
                    Commands = UnreliableCommandBuffer.ToArray()
                }.Serialize();

                var l2Encrypt = AES.Encrypt(l2, AESKey);

                var packet = new UdpPacket
                {
                    PeerID = UniP2PManager.MyPeerID,
                    UdpPacketL2 = l2Encrypt.result,
                    UdpPacketL2IV = l2Encrypt.iv,
                }.Serialize();

                await UdpSocket.SendAsync(packet, packet.Length, Peer.IPEndPoint);
                PacketCapture.Write(Peer.IPEndPoint.ToString(), packet.Length, "Unreliable");
                UnreliableCommandBuffer.Clear();
            }
        }
Example #6
0
        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");
        }
Example #7
0
        public async UniTask SendPacketDataAsync(Peer peer, byte[] data, CommandType p2PEvent = CommandType.Nothing, SocketQosType qosType = SocketQosType.Unreliable)
        {
            foreach (var connection in UdpConnections)
            {
                if (connection.Peer == peer)
                {
                    await SendAsync(GetUdpConnection(peer.ID), data, p2PEvent, qosType);

                    PacketCapture.Write(peer.IPEndPoint.ToString(), data.Length, "PacketData");
                    return;
                }
            }
        }
Example #8
0
        public async UniTask SendEmptyPacketAsync(IPEndPoint ip)
        {
            byte[] buf = { };
            try
            {
                await UdpSocket.SendAsync(buf, buf.Length, ip);

                PacketCapture.Write(ip.ToString(), buf.Length, "EmptyPacket");
            }
            catch (ObjectDisposedException ex)
            {
                //Debugger.Log("[SocketUDP] Closed");
            }
        }
Example #9
0
        public async UniTask <int> SendPingPacketAsync(IPEndPoint ip)
        {
            int ms = 0;

            try
            {
                var objs = new Command[1];
                objs[0] = new Command {
                    P2PEventType = CommandType.PingRequest, Value = new PingPacket {
                        PingID = pingid
                    }.Serialize()
                };
                var u = new UdpPacketL2
                {
                    ACKNumber    = 0,
                    PacketNumber = 0,
                    Commands     = objs
                }.Serialize();

                var packet = new UdpPacket
                {
                    PeerID        = null,
                    UdpPacketL2   = u,
                    UdpPacketL2IV = null
                }.Serialize();
                await UdpSocket.SendAsync(packet, packet.Length, ip);

                PacketCapture.Write(ip.ToString(), packet.Length, "Ping");
                var id = pingid;
                PingTemp.Add(id, false);
                pingid++;
                do
                {
                    await UniTask.Delay(1);

                    ms++;
                }while (PingTemp[id]);
                PingTemp.Remove(id);
                return(ms);
            }
            catch (ObjectDisposedException ex)
            {
                Debugger.Error(ex.Message);
                Debugger.Log("[SocketUDP] Closed");
            }

            return(-1);
        }
Example #10
0
        public async UniTask TickReliablePacket(UdpClient UdpSocket)
        {
            while (isTickReliablePacket)
            {
                foreach (var value in SentReliableBuffer.Values)
                {
                    value.WaitTime++;
                    if (RTT <= value.WaitTime)
                    {
                        await UdpSocket.SendAsync(value.Buffer, value.Buffer.Length, Peer.IPEndPoint);
                        PacketCapture.Write(Peer.IPEndPoint.ToString(), value.Buffer.Length, "TickReliablePacket");
                    }
                }

                await UniTask.Delay(1);
            }
        }
Example #11
0
        public async UniTask SendReliable(UdpClient UdpSocket)
        {
            if (State == UdpConnectionState.Connected)
            {
                /*if (!isTickReliablePacket)
                 * {
                 *  isTickReliablePacket = true;
                 *  await TickReliablePacket(UdpSocket);
                 * }*/

                if (ReliableCommandBuffer.Count != 0)
                {
                    var l2 = new UdpPacketL2
                    {
                        PacketNumber = MyPacketCount,
                        ACKNumber    = 0,
                        Commands     = ReliableCommandBuffer.ToArray(),
                    }.Serialize();

                    var l2Encrypt = AES.Encrypt(l2, AESKey);

                    var packet = new UdpPacket
                    {
                        PeerID        = UniP2PManager.MyPeerID,
                        UdpPacketL2   = l2Encrypt.result,
                        UdpPacketL2IV = l2Encrypt.iv,
                    }.Serialize();

                    await UdpSocket.SendAsync(packet, packet.Length, Peer.IPEndPoint);

                    PacketCapture.Write(Peer.IPEndPoint.ToString(), packet.Length, "Reliable");
                    //SentReliableBuffer.Add(MyPacketCount, new ReliablePacketInfo { Buffer = packet, WaitTime = 0 });
                    MyPacketCount++;
                }

                /*if (SentReliableBuffer.ContainsKey(PeerWaitPacketNumber))
                 * {
                 *  await UdpSocket.SendAsync(SentReliableBuffer[PeerWaitPacketNumber].Buffer, SentReliableBuffer[PeerWaitPacketNumber].Buffer.Length, Peer.IPEndPoint);
                 *  PacketCapture.Write(Peer.IPEndPoint.ToString(), SentReliableBuffer[PeerWaitPacketNumber].Buffer.Length, "SentReliableBuffer");
                 * }*/
            }
        }
Example #12
0
        private async UniTask SendConnectRequestEvent(IPEndPoint ip, bool isBroadcast = false, int localport = 0)
        {
            Command command = new Command();

            command.P2PEventType = CommandType.ConnectRequest;
            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();

            if (isBroadcast && localport != 0)
            {
                UdpSocket.EnableBroadcast = true;
                await UdpSocket.SendAsync(packet, packet.Length, new IPEndPoint(IPAddress.Broadcast, localport));

                PacketCapture.Write("Broadcast:" + localport, packet.Length, "ConnectRequestEvent");
                UdpSocket.EnableBroadcast = false;
                await UdpSocket.SendAsync(packet, packet.Length, new IPEndPoint(UniP2PManager.PrivateIPEndPoint.Address, localport));

                PacketCapture.Write(UniP2PManager.PrivateIPEndPoint.Address.ToString() + ":" + localport, packet.Length, "ConnectRequestEvent");
            }
            else
            {
                await UdpSocket.SendAsync(packet, packet.Length, ip);
            }
        }