示例#1
0
 static void Main(string [] args)
 {
     while (true)
     {
         if (Console.ReadKey().Key == ConsoleKey.Enter)
         {
             Task.Run(() => EmulatedUserConnection.Init());
         }
     }
 }
        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.Ready:
                    messageJSON = incomingPacket.ReadString();
                    GameserverInstance gameserverReady = JsonConvert.DeserializeObject <GameserverInstance>(messageJSON);
                    EmulatedUserConnection.ReceiveServerInfo(gameserverReady);
                    break;

                case MessageType.LiveServers:
                    messageJSON = incomingPacket.ReadString();
                    List <GameserverInstance> servers = JsonConvert.DeserializeObject <List <GameserverInstance> >(messageJSON);
                    foreach (var item in servers)
                    {
                        Console.WriteLine(item.ServerName);
                    }
                    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);
        }