Exemplo n.º 1
0
        private static void OnUDPReceive(IAsyncResult result)
        {
            try
            {
                IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data           = UDPlistener.EndReceive(result, ref clientEndPoint);
                UDPlistener.BeginReceive(OnUDPReceive, null);
                if (data.Length < 4)
                {
                    // todo this could potentially could cause problems under high traffic conditions
                    return;
                }
                using (Packet packet = new Packet(data))
                {
                    int clientId = packet.ReadInt();
                    if (clientId == 0)
                    {
                        return;
                    }

                    // if this connection is new, start session
                    if (clients[clientId].udp.endPoint == null)
                    {
                        clients[clientId].udp.Connect(clientEndPoint);
                        return;
                    }

                    // todo additional security might be needed to prevent ip spoofing attack
                    if (clients[clientId].udp.endPoint.ToString() == clientEndPoint.ToString())
                    {
                        clients[clientId].udp.HandleData(packet);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error receiving UDP data: {e}");
            }
        }
Exemplo n.º 2
0
            private bool HandleData(byte[] data)
            {
                int packetLength = 0;

                recievedData.SetBytes(data);
                if (recievedData.UnreadLength() >= 4)
                {
                    packetLength = recievedData.ReadInt();
                    if (packetLength <= 0)
                    {
                        return(true);
                    }
                }

                while (packetLength > 0 && packetLength <= recievedData.UnreadLength())
                {
                    byte[] packetBytes = recievedData.ReadBytes(packetLength);
                    ThreadManager.ExecuteOnMainThread(() =>
                    {
                        using (Packet p = new Packet(packetBytes))
                        {
                            int packetId = p.ReadInt();
                            Server.packetHandlers[packetId](id, p);
                        }
                    });

                    packetLength = 0;
                    if (recievedData.UnreadLength() >= 4)
                    {
                        packetLength = recievedData.ReadInt();
                        if (packetLength <= 0)
                        {
                            return(true);
                        }
                    }
                }

                return(packetLength <= 1);
            }
Exemplo n.º 3
0
        private static void UDPReceiveCallback(IAsyncResult _result)
        {
            try
            {
                IPEndPoint _clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     _data           = udpListener.EndReceive(_result, ref _clientEndPoint);
                udpListener.BeginReceive(UDPReceiveCallback, null);

                if (_data.Length < 4)
                {
                    return;
                }

                using (Packet _packet = new Packet(_data))
                {
                    int _clientId = _packet.ReadInt();

                    if (_clientId == 0)
                    {
                        return;
                    }

                    if (clients[_clientId].udp.endPoint == null)
                    {
                        clients[_clientId].udp.Connect(_clientEndPoint);
                        return;
                    }

                    if (clients[_clientId].udp.endPoint.ToString() == _clientEndPoint.ToString())
                    {
                        clients[_clientId].udp.HandleData(_packet);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error receiving UDP data: {e}");
            }
        }
Exemplo n.º 4
0
            private bool HandleData(byte[] _data)
            {
                int _packetLength = 0;

                receiveData.SetBytes(_data);
                if (receiveData.UnreadLength() >= 4)
                {
                    _packetLength = receiveData.ReadInt();
                    if (_packetLength <= 0)
                    {
                        return(true);
                    }
                }
                while (_packetLength > 0 && _packetLength <= receiveData.UnreadLength())
                {
                    byte[] _packetBytes = receiveData.ReadBytes(_packetLength);
                    ThreadManager.ExecuteOnMainThread(() => {
                        using (Packet _packet = new Packet(_packetBytes)){
                            int _packetId = _packet.ReadInt();
                            MainServer.packetHandlers[_packetId](id, _packet);
                        }
                    });
                    _packetLength = 0;
                    if (receiveData.UnreadLength() >= 4)
                    {
                        _packetLength = receiveData.ReadInt();
                        if (_packetLength <= 0)
                        {
                            return(true);
                        }
                    }
                }
                if (_packetLength <= 1)
                {
                    return(true);
                }
                return(false);
            }
Exemplo n.º 5
0
        private static void UdpReceiveCallback(IAsyncResult ar)
        {
            try
            {
                IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data           = udpListener.EndReceive(ar, ref clientEndPoint);
                udpListener.BeginReceive(UdpReceiveCallback, null);

                if (data.Length >= 4)
                {
                    using (Packet p = new Packet(data))
                    {
                        int clientId = p.ReadInt();
                        if (!clients.ContainsKey(clientId))
                        {
                            return;
                        }

                        if (clients[clientId].udp.endPoint == null)
                        {
                            clients[clientId].udp.Connect(clientEndPoint);
                            return;
                        }

                        if (clients[clientId].udp.endPoint.ToString() == clientEndPoint.ToString())
                        {
                            clients[clientId].udp.HandleData(p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error recieving data via udp: {ex}");
            }
        }
Exemplo n.º 6
0
        public static void PlayerLeave(Packet packet)
        {
            int id = packet.ReadInt();

            GameManager.players.Remove(id);
        }
Exemplo n.º 7
0
        /// <summary>Receives incoming UDP data.</summary>
        public static void UDPReceiveCallback(IAsyncResult _result)
        {
            try
            {
                IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     data           = udpListener.EndReceive(_result, ref clientEndPoint);
                udpListener.BeginReceive(UDPReceiveCallback, null);

                if (data.Length < 4)
                {
                    return;
                }

                //Handle received Data
                using (Packet _packet = new Packet(data))
                {
                    int clientId = _packet.ReadInt();

                    //// If this is a new connection
                    if (clientId == 0)
                    {
                        for (int i = 1; i < users.Count; i++)
                        {
                            if (users[i].udp.endPoint == null)
                            {
                                users[i].udp.Connect(clientEndPoint);
                                users[i].name = _packet.ReadString();
                                clientId      = i;
                                break;
                            }
                        }

                        //using (Packet _pack = new Packet((int)ServerPackets.welcome))
                        //{
                        //    //you have to catch these info at client on the same order
                        //    _pack.Write(clientId);
                        //    _pack.Write("Welcome");
                        //    _pack.WriteLength();


                        //    UDPServer.SendUDPData(clientEndPoint, _pack);

                        //}

                        //sent to own client a Welcome message
                        ServerSend.Welcome(clientId);

                        return;
                    }


                    if (users[clientId].udp.endPoint.ToString() == clientEndPoint.ToString())
                    {
                        users[clientId].udp.HandleData(_packet);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error receiving UDP data: {ex}");
            }
        }