Пример #1
0
    //Reads in a packet sent from the server and passes it onto whatever handle function that packet type is mapped onto
    public void ReadServerPacket(string PacketData)
    {
        //Store the total set of packet data into a new PacketData object for easier reading
        NetworkPacket TotalPacket = new NetworkPacket(PacketData);

        //Iterate over all the packet data until we finish reading and handling it all
        while (!TotalPacket.FinishedReading())
        {
            //Read the packets order number and packet type enum values
            int OrderNumber             = TotalPacket.ReadInt();
            ServerPacketType PacketType = TotalPacket.ReadType();

            //Get the rest of the values for this set based on the packet type, then put the order number back to the front
            NetworkPacket SectionPacket = ReadPacketValues(PacketType, TotalPacket);

            //Compare this packets order number to see if its arrived in the order we were expecting
            int  ExpectedOrderNumber = ConnectionManager.Instance.PacketQueue.LastPacketNumberRecieved + 1;
            bool InOrder             = OrderNumber == ExpectedOrderNumber;

            //If the packet arrived in order then it gets processed normally
            if (InOrder)
            {
                //Reset the packets data before we start handling it
                SectionPacket.ResetRemainingData();

                //Read away the packet type value as its not needed when processing packets immediately
                SectionPacket.ReadType();

                //Pass the section packet on to its handler function
                if (PacketHandlers.TryGetValue(PacketType, out Packet Packet))
                {
                    Packet.Invoke(ref SectionPacket);
                }

                //Store this as the last packet that was processed
                ConnectionManager.Instance.PacketQueue.LastPacketNumberRecieved = OrderNumber;
            }
            //If packets arrive out of order tell the server the order number that we were expecting to recieve next so everything since that packet can be resent
            else
            {
                //Tell the server what we need resent and disregard everything else in this packet
                SystemPacketSender.Instance.SendMissedPacketsRequest(ExpectedOrderNumber);
                return;
            }
        }
    }