protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            //Console.WriteLine("GOT PACKET: " + DateTime.UtcNow.Millisecond, Color.Blue);
            var pos = 0;

            while (pos < size)
            {
                var packet = PacketHandler.HandlePacket(buffer, pos, this);
                pos += packet.Size;

                // TODO: Add config here - if debug
                // Temp - only unknowns
                if (Program.Configuration.Global.LogAllPackets && !(packet is UnknownPacket))
                {
                    $"[C] 0x{packet.Id:x2} {packet.GetType()} <<< {GetUserName()}".Info();
                }

                if (packet is UnknownPacket)
                {
                    $"[C] 0x{packet.Id:x2} {packet.GetType()} <<< {GetUserName()}".Warn();
                }

                // TODO: RUN PACKET ASYNC IF LOW PRIORITY?
                packet.Run();
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.Title = "Game Server";
            Log.Send(PrintState.Information, $"Game Server {_serverVersion} - Started at {DateTime.Now}");
            Library.Initialize(); // Initialize ENet Library

            Address address = new Address {
                Port = _Port
            };                                    // Setting IP & Port

            _server = new Host();                 // Creating UDP Host
            _server.Create(address, _maxPlayers); // Creating Server with Connection Limit

            Log.Send(PrintState.Information, $"Game Server started on Port {_Port}..");

            Event netEvent;

            while (!Console.KeyAvailable)
            {
                bool polled = false;

                while (!polled)
                {
                    if (_server.CheckEvents(out netEvent) <= 0)
                    {
                        if (_server.Service(15, out netEvent) <= 0)
                        {
                            break;
                        }

                        polled = true;
                    }

                    switch (netEvent.Type)
                    {
                    case EventType.None:
                        break;

                    case EventType.Connect:
                        Log.Send(PrintState.Packet, "Connection from ID: " + netEvent.Peer.ID + ", IP: " + netEvent.Peer.IP);
                        netEvent.Peer.Timeout(32, 1000, 4000);
                        break;

                    case EventType.Disconnect:
                        Console.WriteLine("Client disconnected - ID: " + netEvent.Peer.ID + ", IP: " + netEvent.Peer.IP);
                        break;

                    case EventType.Timeout:
                        Log.Send(PrintState.Warning, "Timeout from ID: " + netEvent.Peer.ID + ", IP: " + netEvent.Peer.IP);
                        break;

                    case EventType.Receive:
                        PacketHandler.HandlePacket(ref netEvent);
                        netEvent.Packet.Dispose();
                        break;
                    }
                }

                _server.Flush();
            }
            Library.Deinitialize();
        }