Beispiel #1
0
 public NetPacket(NetAddress adr, byte[] data)
     : this()
 {
     this.adr  = adr;
     this.data = data;
     this.time = Game.Time;
 }
Beispiel #2
0
        public static void ProcessPacket(NetAddress from, byte[] packet)
        {
            if (packet.Length < 4)
            {
                return;
            }

            var message     = new BitStream(packet);
            var sequenceNum = message.ReadUInt32();

            if (sequenceNum == 0xFFFFFFFF)
            {
                ProcessOutOfBand(from, packet);
                return;
            }

            if (from != _serverChannel.Address)
            {
                return;
            }

            if (_serverChannel.ProcessPacket(packet))
            {
                message = new BitStream(packet); // reset the bitstream
                ProcessServerMessage(message);
            }
        }
Beispiel #3
0
        public static void Connect_f(string[] args)
        {
            if (ConVar.GetValue <string>("nicknamee") == ("")) // baddd
            {
                Log.Write(LogLevel.Info, "You haven't set a nickname, set one now:)");
                return;
            }
            if (args.Length != 2)
            {
                Log.Write(LogLevel.Info, "usage: connect [ip]");
                return;
            }

            if (args[1] != "localhost")
            {
                ConVar.SetValue <bool>("sv_running", false);
            }

            var server = NetAddress.Resolve(args[1]);

            if (_currentServer != server)
            {
                _currentServer = server;

                State = ClientState.Challenging;

                _lastMessageReceivedAt = _clientTime;
                _lastConnectPacketTime = -999999;
            }
        }
Beispiel #4
0
        public static void ProcessPacket(NetAddress from, byte[] packet)
        {
            if (packet.Length < 4)
            {
                return;
            }

            var message     = new BitStream(packet);
            var sequenceNum = message.ReadUInt32();

            if (sequenceNum == 0xFFFFFFFF)
            {
                ProcessOutOfBand(from, packet);
                return;
            }

            for (int i = 0; i < _clients.Length; i++)
            {
                if (_clients[i] != null)
                {
                    if (_clients[i].Channel.Address == from)
                    {
                        if (_clients[i].Channel.ProcessPacket(packet))
                        {
                            ProcessClientMessage(_clients[i], message);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        private static void ProcessOutOfBand(NetAddress from, byte[] packet)
        {
            var message = Encoding.UTF8.GetString(packet, 4, packet.Length - 4);
            var args    = Command.Tokenize(message);

            if (args.Length == 0)
            {
                return;
            }

            switch (args[0])
            {
            case "getchallenge":
                var rnd = new Random();
                _challenges[from] = (int)(rnd.Next() ^ _serverTime);

                NetManager.SendOOBPacket(NetChannelType.ServerToClient, from, "challengeResponse {0}", _challenges[from]);
                break;

            case "connect":
                if (args.Length != 3)
                {
                    return;
                }

                int challenge = 0;
                int.TryParse(args[1], out challenge);

                if (!_challenges.ContainsKey(from) || _challenges[from] != challenge)
                {
                    return;
                }

                _challenges.Remove(from);

                int i;

                for (i = 0; i < _clients.Length; i++)
                {
                    if (_clients[i] == null)
                    {
                        _clients[i]                       = new ServerClient();
                        _clients[i].Channel               = new NetChannel(NetChannelType.ServerToClient, from);
                        _clients[i].Name                  = args[2];
                        _clients[i].ClientNum             = i;
                        _clients[i].ExternalIP            = from;
                        _clients[i].LastMessageReceivedAt = _serverTime;
                        break;
                    }
                }

                SendReliableCommand(null, "print \"{0} joined\"", args[2]);
                Log.Write(LogLevel.Info, "Client {0} connected (name {1}, from {2})", i, args[2], from);

                NetManager.SendOOBPacket(NetChannelType.ServerToClient, from, "connectResponse {0} {1}", i, _mapName);
                break;
            }
        }
Beispiel #6
0
        public static void SendOOBPacket(NetChannelType type, NetAddress address, string format, params object[] others)
        {
            string message = "????" + string.Format(format, others);

            byte[] data = Encoding.UTF8.GetBytes(message);

            data[0] = 0xFF; data[1] = 0xFF; data[2] = 0xFF; data[3] = 0xFF;

            SendPacket(type, address, data);
        }
Beispiel #7
0
 private static void ProcessPacket(NetAddress from, byte[] buffer)
 {
     if (ConVar.GetValue <bool>("sv_running"))
     {
         Server.ProcessPacket(from, buffer);
     }
     else
     {
         Client.ProcessPacket(from, buffer);
     }
 }
Beispiel #8
0
        private static void ProcessOutOfBand(NetAddress from, byte[] packet)
        {
            var message = Encoding.UTF8.GetString(packet, 4, packet.Length - 4);
            var args    = Command.Tokenize(message);

            if (args.Length == 0)
            {
                return;
            }

            switch (args[0])
            {
            case "challengeResponse":
                if (State != ClientState.Challenging)
                {
                    return;
                }

                _lastConnectPacketTime = -999999;
                _lastMessageReceivedAt = _clientTime;

                _challenge = int.Parse(args[1]);
                State      = ClientState.Connecting;
                break;

            case "connectResponse":
                if (State != ClientState.Connecting)
                {
                    return;
                }

                _clientNum = int.Parse(args[1]);

                if (!ConVar.GetValue <bool>("sv_running"))
                {
                    Server.CreatePhysicsWorld();
                    MapManager.Load(string.Format("Maps/{0}.gmp", args[2]));
                }

                _lastConnectPacketTime = -999999;
                _lastMessageReceivedAt = _clientTime;

                _serverChannel = new NetChannel(NetChannelType.ClientToServer, from);
                InitializeConnection();

                State = ClientState.Connected;
                break;
            }
        }
Beispiel #9
0
 public static void SendPacket(NetChannelType type, NetAddress address, byte[] packet)
 {
     if (address.Type == AddressType.Loopback)
     {
         SendLoopbackPacket(type, packet);
     }
     else
     {
         if (net_forceLatency.GetValue <int>() == 0)
         {
             SendPacketInternal(address, packet);
         }
         else
         {
             _sendQueue.Add(new NetPacket(address, packet));
         }
     }
 }
Beispiel #10
0
        public static void Process()
        {
            byte[]   buffer   = new byte[16384];
            int      bytes    = 0;
            EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                try
                {
                    bytes = _socket.ReceiveFrom(buffer, buffer.Length, SocketFlags.None, ref remoteEP);
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode != SocketError.WouldBlock && e.SocketErrorCode != SocketError.ConnectionReset)
                    {
                        Log.Write(LogLevel.Info, "socket error {0}", e.SocketErrorCode);
                    }
                    else
                    {
                        break;
                    }
                }

                if (bytes > 0)
                {
                    byte[] cutBuffer = new byte[bytes];
                    Array.Copy(buffer, cutBuffer, bytes);

                    NetAddress from = new NetAddress((IPEndPoint)remoteEP);

                    if (net_forceLatency.GetValue <int>() == 0)
                    {
                        ProcessPacket(from, cutBuffer);
                    }
                    else
                    {
                        _receiveQueue.Add(new NetPacket(from, cutBuffer));
                    }
                }
            }

            if (_sendQueue.Count > 0)
            {
                var toSend  = _sendQueue.ToArray();
                var time    = Game.Time;
                var latency = net_forceLatency.GetValue <int>();

                foreach (var item in toSend)
                {
                    if (time >= (item.time + latency))
                    {
                        SendPacketInternal(item.adr, item.data);
                        _sendQueue.Remove(item);
                    }
                }
            }

            if (_receiveQueue.Count > 0)
            {
                var toReceive = _receiveQueue.ToArray();
                var time      = Game.Time;
                var latency   = net_forceLatency.GetValue <int>();

                foreach (var item in toReceive)
                {
                    if (time >= (item.time + latency))
                    {
                        ProcessPacket(item.adr, item.data);
                        _receiveQueue.Remove(item);
                    }
                }
            }
        }
Beispiel #11
0
 private static void SendPacketInternal(NetAddress address, byte[] packet)
 {
     _socket.SendTo(packet, address.EndPoint);
 }
Beispiel #12
0
 public NetChannel(NetChannelType type, NetAddress address)
 {
     ChannelType = type;
     Address     = address;
 }