protected int BaseDecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead)
        {
            //add read offset for packet type
            //iDataReadHead += sizeof(PacketType);

            return(iDataReadHead);
        }
        public void SendPacket(PacketWrapper packetToSend, Connection conTarget)
        {
            //check if packet is dropped
            if (IsPacketDropped())
            {
                return;
            }

            //loop through list of packets in flight to find one not in use
            for (int i = 0; i < m_lstDataInFlight.Count; i++)
            {
                if (m_lstDataInFlight[i].m_fTimeOfDelivery == 0)
                {
                    m_lstDataInFlight[i] = new TimeStampedWrapper()
                    {
                        m_tWrappedData    = packetToSend,
                        m_conTarget       = conTarget,
                        m_fTimeOfDelivery = CalcuateDeliveryTime()
                    };

                    return;
                }
            }

            //if there is not enough room already then add a new entry to the list
            m_lstDataInFlight.Add(new TimeStampedWrapper()
            {
                m_tWrappedData    = packetToSend,
                m_conTarget       = conTarget,
                m_fTimeOfDelivery = CalcuateDeliveryTime()
            });
        }
        public override void EncodePacket(PacketWrapper pkwPacketWrapper)
        {
            BaseEncodePacket(pkwPacketWrapper);

            //add input byte
            //pkwPacketWrapper.add(m_bInput)
        }
        private Packet DecodePacket(PacketWrapper packetWrapper, ref int iReadHead)
        {
            //get packet type
            Packet.PacketType ptyPacketType = Packet.GetPacketType(packetWrapper, iReadHead);

            Packet pktOutput = null;

            //create Packet
            switch (ptyPacketType)
            {
            case Packet.PacketType.Ping:
                pktOutput = new PingPacket();
                break;

            case Packet.PacketType.Input:
                pktOutput = new InputPacket();
                break;

            case Packet.PacketType.ResetTickCount:
                pktOutput = new ResetTickCountPacket();
                break;

            case Packet.PacketType.StartCountdown:
                pktOutput = new StartCountDownPacket();
                break;
            }

            //decode packet
            iReadHead = pktOutput.DecodePacket(packetWrapper, iReadHead);

            return(pktOutput);
        }
        public void ReceivePacket(PacketWrapper packetWrapper)
        {
            //update the last ack packet sent from this client to the connection target clamped to not be more than the total number of packets sent
            m_iLastAckPacketNumberSent = Mathf.Min(Mathf.Max(m_iLastAckPacketNumberSent, packetWrapper.m_iLastAckPackageFromPerson), m_iPacketsQueuedToSendCount);

            //get the tick of the oldest packet in the packet wrapper
            int iPacketNumberHead = packetWrapper.m_iStartPacketNumber;

            //the packet read head
            int iPacketReadHead = 0;

            //the length of the data
            int iPacketReadTail = packetWrapper.m_Payload.Count;

            //decode the remaining packets
            while (iPacketReadHead < iPacketReadTail)
            {
                //decode packet
                Packet pktDecodedPacket = DecodePacket(packetWrapper, ref iPacketReadHead);

                iPacketNumberHead++;

                //add packet to list of packets to be processed
                QueueReceivedPacket(pktDecodedPacket, iPacketNumberHead);
            }
        }
        public override int DecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead)
        {
            iDataReadHead = BaseDecodePacket(pkwPacketWrapper, iDataReadHead);

            //decode tick offset
            m_bOffset = (pkwPacketWrapper.m_Payload[iDataReadHead] as TickStampedPacket).m_bOffset;

            //offset data read point for offset byte
            //iDataReadHead += sizeof(byte);

            return(iDataReadHead);
        }
        public override int DecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead)
        {
            iDataReadHead = base.DecodePacket(pkwPacketWrapper, iDataReadHead);

            //decode tick offset
            m_bInput = (pkwPacketWrapper.m_Payload[iDataReadHead] as InputPacket).m_bInput;

            //move the read head
            //iDataReadHead += sizeof(byte);

            iDataReadHead++;

            return(iDataReadHead);
        }
        public override int DecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead)
        {
            iDataReadHead = base.BaseDecodePacket(pkwPacketWrapper, iDataReadHead);

            //decode tick offset
            m_lGameStartTime = (pkwPacketWrapper.m_Payload[iDataReadHead] as StartCountDownPacket).m_lGameStartTime;


            iDataReadHead += 1;



            return(iDataReadHead);
        }
        private void SendPackets()
        {
            //check if there is anything to send
            if (m_PacketsInFlight.Count == 0)
            {
                return;
            }

            //calculate the number of packets that dont need to be sent
            int iPacketsToDrop = m_PacketsInFlight.Count - (m_iPacketsQueuedToSendCount - m_iLastAckPacketNumberSent);

            if (iPacketsToDrop >= m_PacketsInFlight.Count)
            {
                m_PacketsInFlight.Clear();

                return;
            }

            //dequeue all the old packets that have already been acknowledged
            for (int i = 0; i < iPacketsToDrop; i++)
            {
                m_PacketsInFlight.Dequeue();
            }

            if (m_PacketsInFlight.Count == 0)
            {
                return;
            }

            //create packet wrapper
            PacketWrapper pkwPacketWrappepr = new PacketWrapper(m_iTotalPacketsReceived, m_iPacketsQueuedToSendCount - m_PacketsInFlight.Count, m_PacketsInFlight.Count);

            //add as many packets as possible without hitting the max send data limit
            for (int i = 0; i < m_PacketsInFlight.Count; i++)
            {
                pkwPacketWrappepr.AddDataPacket(m_PacketsInFlight[i]);
            }

            //send packet through coms
            if (m_icsConnectionSim != null)
            {
                m_icsConnectionSim.SendPacket(pkwPacketWrappepr, m_conConnectionTarget);
            }
        }
 public static PacketType GetPacketType(PacketWrapper pkwPacketWrapper, int iDataReadHead)
 {
     return((PacketType)pkwPacketWrapper.m_Payload[iDataReadHead].m_ptyPacketType);
 }
 public override int DecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead)
 {
     return(BaseDecodePacket(pkwPacketWrapper, iDataReadHead) + 1);
 }
 public override void EncodePacket(PacketWrapper pkwPacketWrapper)
 {
     BaseEncodePacket(pkwPacketWrapper);
 }
 protected void BaseEncodePacket(PacketWrapper pkwPacketWrapper)
 {
     //encode packet type
     //pkwPacketWrapper.AddDataPacket((byte)m_ptyPacketType);
 }
 public abstract void EncodePacket(PacketWrapper pkwPacketWrapper);
 public abstract int DecodePacket(PacketWrapper pkwPacketWrapper, int iDataReadHead);
        public override void EncodePacket(PacketWrapper pkwPacketWrapper)
        {
            BaseEncodePacket(pkwPacketWrapper);

            //pkwPacketWrapper.add(m_bOffset);
        }
 public override void EncodePacket(PacketWrapper pkwPacketWrapper)
 {
     //pkwPacketWrapper.add(tobyte(m_lGameStartTime));
 }