Example #1
0
        static void Main(string [] args)
        {
            Console.WriteLine("Server started");

            Task.Run(() => Gameserver.Init());

            while (Gameserver.Running)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine("Closing server");
        }
Example #2
0
        private bool HandleIncomingData(byte[] data)
        {
            //Read the length of the next data packet
            int packetLength = 0;

            incomingPacket.SetBytes(data);

            //Check if there is 4 or more bytes in the packet (size of int is 4)
            if (incomingPacket.UnreadLength() >= 4)
            {
                //Read the length of the incoming packet
                packetLength = incomingPacket.ReadInt();
                if (packetLength <= 0)
                {
                    //If the length is 0, return
                    return(true);
                }
            }

            //Keep reading until there is no more data left to read for this specific packet
            //We keep this in a while loop here because one packet might be made up of several packets
            while (packetLength > 0 && packetLength <= incomingPacket.UnreadLength())
            {
                //Handle packet
                //Read the messagetype
                int         messageTypeInt = incomingPacket.ReadInt();
                MessageType messageType    = (MessageType)messageTypeInt;
                string      messageJSON    = "";

                switch (messageType)
                {
                case MessageType.Configure:
                    messageJSON = incomingPacket.ReadString();
                    GameserverInstance configuration = JsonConvert.DeserializeObject <GameserverInstance>(messageJSON);
                    Gameserver.RecieveConfigurationRequest(configuration);
                    break;

                case MessageType.Ready:
                    break;

                default:
                    break;
                }

                packetLength = 0;

                //Check if there 4 or more bytes in the packet (size of int is 4)
                if (incomingPacket.UnreadLength() >= 4)
                {
                    //Read the length of the incoming packet
                    packetLength = incomingPacket.ReadInt();
                    if (packetLength <= 0)
                    {
                        //If the length is 0, return
                        return(true);
                    }
                }
            }

            if (packetLength <= 1)
            {
                return(true);
            }

            return(false);
        }