Beispiel #1
0
        /// <summary>
        /// Handles incoming data from the server
        /// </summary>
        private void Handle()
        {
            while (true)
            {
                if (_receiveBuffer.Length == 0)
                {
                    break;
                }

                byte    opcode = _receiveBuffer.PeekByte(); // -- Peek doesn't remove the byte. Lets us check if we have enough data yet.
                IPacket packet;

                if (!_packets.TryGetValue(opcode, out packet))
                {
                    Bot.OnErrorMessage("Received invalid packet from the server.");
                    return;
                }

                if (_receiveBuffer.Length >= packet.PacketLength) // -- Check if we have enough data to read this packet.
                {
                    _receiveBuffer.ReadByte();                    // -- Trim off the opcode, the packet is ready to be read.
                    packet.Read(_receiveBuffer);                  // -- Read the data from the buffer
                    packet.Handle(this);                          // -- Handle it.
                }
                else // -- Not enough data, wait for more.
                {
                    break;
                }
            }
        }