Example #1
0
    private void ProcessPacket(Buffer packet)
    {
        //print("packet recieved");
        if (packet.Length < 9)
        {
            return;
        }

        string id = packet.ReadString(0, 4);
        //print(id + " packet recieved");

        byte ack = packet.ReadUInt8(4);
        // print(ack);

        uint packetID = packet.ReadUInt32BE(5);

        //print(packetID);

        if (ack == 1 && id != "FRAG")
        {
            SendPacket(PacketBuilder.AckPacket(packetID));
        }

        //TODO: add a length check in order to make sure we got the whole packet // a check for corruption would also be appropriate

        //TODO: process Header for frag annd ACK packets


        switch (id)
        {
        case "FRAG":

            if (packet.Length < 17)
            {
                return;
            }

            byte fragID = packet.ReadUInt8(9);

            if (frags.ContainsKey(fragID))
            {
                Buffer completePacket = frags[fragID].ProcessFrag(packet);

                if (completePacket.Length > 0)
                {
                    //print("completed packet returntd");
                    frags.Remove(fragID);                          //remove the frag packet in csae th efrag ID gets reused
                    SendPacket(PacketBuilder.AckPacket(packetID)); //send ack for final frag packet
                    ProcessPacket(completePacket);                 //process the complete frag packet
                }
                else
                {
                    SendPacket(PacketBuilder.AckPacket(packetID, true));
                }
            }
            else
            {
                frags.Add(fragID, new Frag(packet));
                SendPacket(PacketBuilder.AckPacket(packetID, true));
                //frags[fragID]
            }

            break;

        case "PAWN":
            // if (packet.Length < 5) return;

            // byte networkID = packet.ReadUInt8(4);

            //NetworkObject obj = NetworkObject.GetObjectByNetworkID(networkID);
            //if (obj) {
            //if (obj.classID != "PAWN") return;
            //   Pawn P = (obj as Pawn);
            // if (P != null) P.canPlayerControl = true;
            // }

            break;

        case "MAPG":    //we are currently using our packet type as our classID, we may want to decouple this in the future
            if (packet.Length < 11)
            {
                return;
            }

            NetworkObject obj = ObjectRegistry.SpawnFrom(id);

            obj.Deserialize(packet.Slice(9));


            print("map recived");


            break;
        }
    }