AddIncomingPacket() private method

private AddIncomingPacket ( NetPacket p ) : void
p NetPacket
return void
Ejemplo n.º 1
0
 public void ProcessPacket(NetPacket packet)
 {
     if (NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence) > 0)
     {
         _remoteSequence = packet.Sequence;
         _peer.AddIncomingPacket(packet);
     }
 }
Ejemplo n.º 2
0
 public bool ProcessPacket(NetPacket packet)
 {
     if (NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence) > 0)
     {
         _remoteSequence = packet.Sequence;
         _peer.AddIncomingPacket(packet);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
        public void ProcessPacket(NetPacket packet)
        {
            int relative = NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence);

            if (packet.Sequence < NetConstants.MaxSequence && relative > 0)
            {
                _peer.Statistics.PacketLoss += (ulong)(relative - 1);
                _remoteSequence              = packet.Sequence;
                _peer.AddIncomingPacket(packet);
            }
        }
Ejemplo n.º 4
0
        internal void ProcessPacket(NetPacket packet)
        {
            if (packet.IsFragmented)
            {
                lock (buffers) {
                    // add to an active buffer if one exists
                    for (int i = 0; i < buffers.Length; ++i)
                    {
                        if (buffers[i].IsActive && buffers[i].FragmentId == packet.FragmentId)
                        {
                            buffers[i].Add(packet);

                            // found the active buffer to add the packet to
                            return;
                        }
                    }

                    // could not add to an active buffer; initialize another one
                    for (int i = 0; i < buffers.Length; ++i)
                    {
                        if (!buffers[i].IsActive)
                        {
                            buffers[i].Initialize(packet.FragmentId, packet.FragmentsTotal);
                            buffers[i].Add(packet);

                            // initialized an active buffer and added the packet to it
                            return;
                        }
                    }

                    // all buffers are active; recycle an old one
                    var maxInactiveTime  = TimeSpan.MinValue;
                    int maxInactiveIndex = -1;
                    for (int i = 0; i < buffers.Length; ++i)
                    {
                        if (buffers[i].InactiveTime > maxInactiveTime)
                        {
                            maxInactiveTime  = buffers[i].InactiveTime;
                            maxInactiveIndex = i;
                        }
                    }

                    buffers[maxInactiveIndex].Abort();
                    buffers[maxInactiveIndex].Initialize(packet.FragmentId, packet.FragmentsTotal);
                    buffers[maxInactiveIndex].Add(packet);
                }
            }
            else
            {
                _peer.AddIncomingPacket(packet);
            }
        }
Ejemplo n.º 5
0
            public void Add(NetPacket packet)
            {
                activeTime = stopwatch.Elapsed;

                packets[packet.FragmentPart] = packet;
                ++receivedCount;
                if (receivedCount == fragmentsTotal)
                {
                    stopwatch.Stop();

                    for (int i = 0; i < fragmentsTotal; ++i)
                    {
                        peer.AddIncomingPacket(packets[i]);
                    }
                }
            }
Ejemplo n.º 6
0
        //Process incoming packet
        public void ProcessPacket(NetPacket packet)
        {
            int seq = packet.Sequence;

            if (seq >= NetConstants.MaxSequence)
            {
                NetUtils.DebugWrite("[RR]Bad sequence");
                return;
            }

            int relate    = NetUtils.RelativeSequenceNumber(seq, _remoteWindowStart);
            int relateSeq = NetUtils.RelativeSequenceNumber(seq, _remoteSequence);

            if (relateSeq > _windowSize)
            {
                NetUtils.DebugWrite("[RR]Bad sequence");
                return;
            }

            //Drop bad packets
            if (relate < 0)
            {
                //Too old packet doesn't ack
                NetUtils.DebugWrite("[RR]ReliableInOrder too old");
                return;
            }
            if (relate >= _windowSize * 2)
            {
                //Some very new packet
                NetUtils.DebugWrite("[RR]ReliableInOrder too new");
                return;
            }

            //If very new - move window
            int ackIdx;
            int ackByte;
            int ackBit;

            Monitor.Enter(_outgoingAcks);
            if (relate >= _windowSize)
            {
                //New window position
                int newWindowStart = (_remoteWindowStart + relate - _windowSize + 1) % NetConstants.MaxSequence;
                _outgoingAcks.Sequence = (ushort)newWindowStart;

                //Clean old data
                while (_remoteWindowStart != newWindowStart)
                {
                    ackIdx  = _remoteWindowStart % _windowSize;
                    ackByte = NetConstants.SequencedHeaderSize + ackIdx / BitsInByte;
                    ackBit  = ackIdx % BitsInByte;
                    _outgoingAcks.RawData[ackByte] &= (byte)~(1 << ackBit);
                    _remoteWindowStart              = (_remoteWindowStart + 1) % NetConstants.MaxSequence;
                }
            }

            //Final stage - process valid packet
            //trigger acks send
            _mustSendAcks = true;
            ackIdx        = seq % _windowSize;
            ackByte       = NetConstants.SequencedHeaderSize + ackIdx / BitsInByte;
            ackBit        = ackIdx % BitsInByte;
            if ((_outgoingAcks.RawData[ackByte] & (1 << ackBit)) != 0)
            {
                NetUtils.DebugWrite("[RR]ReliableInOrder duplicate");
                Monitor.Exit(_outgoingAcks);
                return;
            }

            //save ack
            _outgoingAcks.RawData[ackByte] |= (byte)(1 << ackBit);
            Monitor.Exit(_outgoingAcks);

            //detailed check
            if (seq == _remoteSequence)
            {
                NetUtils.DebugWrite("[RR]ReliableInOrder packet succes");
                _peer.AddIncomingPacket(packet);
                _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;

                if (_ordered)
                {
                    NetPacket p;
                    while ((p = _receivedPackets[_remoteSequence % _windowSize]) != null)
                    {
                        //process holded packet
                        _receivedPackets[_remoteSequence % _windowSize] = null;
                        _peer.AddIncomingPacket(p);
                        _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
                    }
                }
                else
                {
                    while (_earlyReceived[_remoteSequence % _windowSize])
                    {
                        //process early packet
                        _earlyReceived[_remoteSequence % _windowSize] = false;
                        _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
                    }
                }

                return;
            }

            //holded packet
            if (_ordered)
            {
                _receivedPackets[ackIdx] = packet;
            }
            else
            {
                _earlyReceived[ackIdx] = true;
                _peer.AddIncomingPacket(packet);
            }
        }
Ejemplo n.º 7
0
        //Process incoming packet
        public void ProcessPacket(NetPacket packet)
        {
            if (packet.Sequence >= NetConstants.MaxSequence)
            {
                _peer.DebugWrite("[RR]Bad sequence");
                return;
            }

            int relate    = NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteWindowStart);
            int relateSeq = NetUtils.RelativeSequenceNumber(packet.Sequence, _remoteSequence);

            if (relateSeq > _windowSize)
            {
                _peer.DebugWrite("[RR]Bad sequence");
                return;
            }

            //Drop bad packets
            if (relate < 0)
            {
                //Too old packet doesn't ack
                _peer.DebugWrite("[RR]ReliableInOrder too old");
                return;
            }
            if (relate >= _windowSize * 2)
            {
                //Some very new packet
                _peer.DebugWrite("[RR]ReliableInOrder too new");
                return;
            }

            //If very new - move window
            Monitor.Enter(_outgoingAcksAccess);
            if (relate >= _windowSize)
            {
                //New window position
                int newWindowStart = (_remoteWindowStart + relate - _windowSize + 1) % NetConstants.MaxSequence;

                //Clean old data
                while (_remoteWindowStart != newWindowStart)
                {
                    _outgoingAcks[_remoteWindowStart % _windowSize] = false;
                    _remoteWindowStart = (_remoteWindowStart + 1) % NetConstants.MaxSequence;
                }
            }

            //Final stage - process valid packet
            //trigger acks send
            _mustSendAcks = true;

            if (_outgoingAcks[packet.Sequence % _windowSize])
            {
                _peer.DebugWrite("[RR]ReliableInOrder duplicate");
                Monitor.Exit(_outgoingAcksAccess);
                return;
            }

            //save ack
            _outgoingAcks[packet.Sequence % _windowSize] = true;
            Monitor.Exit(_outgoingAcksAccess);

            //detailed check
            if (packet.Sequence == _remoteSequence)
            {
                _peer.DebugWrite("[RR]ReliableInOrder packet succes");
                _peer.AddIncomingPacket(packet);
                _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;

                if (_ordered)
                {
                    NetPacket p;
                    while ((p = _receivedPackets[_remoteSequence % _windowSize]) != null)
                    {
                        //process holded packet
                        _receivedPackets[_remoteSequence % _windowSize] = null;
                        _peer.AddIncomingPacket(p);
                        _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
                    }
                }
                else
                {
                    while (_earlyReceived[_remoteSequence % _windowSize])
                    {
                        //process early packet
                        _earlyReceived[_remoteSequence % _windowSize] = false;
                        _remoteSequence = (_remoteSequence + 1) % NetConstants.MaxSequence;
                    }
                }

                return;
            }

            //holded packet
            if (_ordered)
            {
                _receivedPackets[packet.Sequence % _windowSize] = packet;
            }
            else
            {
                _earlyReceived[packet.Sequence % _windowSize] = true;
                _peer.AddIncomingPacket(packet);
            }
        }
Ejemplo n.º 8
0
        //Process incoming packet
        public bool ProcessPacket(NetPacket packet)
        {
            if (_mustSendAcksStartTimer <= 0)
            {
                _mustSendAcksStartTimer = NetTime.NowMs;
            }

            if (packet.Sequence >= NetConstants.MaxSequence)
            {
                NetUtils.DebugWrite("[RR]Bad sequence");
                return(false);
            }

            _packetsToAcknowledge.Add(packet.Sequence);

            // Check if its a duplicated packet
            if (_ordered && RelativeSequenceDiff(packet.Sequence, _remoteSequence) < 0)
            {
                // Its a duplicated packet
                return(false);
            }

            //detailed check
            if (packet.Sequence == _remoteSequence)
            {
                NetUtils.DebugWrite("[RR]ReliableInOrder packet succes");
                _peer.AddIncomingPacket(packet);
                _remoteSequence = NetUtils.IncrementSequenceNumber(_remoteSequence, 1);

                if (_ordered)
                {
                    NetPacket p;
                    while ((p = _receivedPackets[_remoteSequence % _windowSize]) != null)
                    {
                        //process holded packet
                        _receivedPackets[_remoteSequence % _windowSize] = null;
                        _peer.AddIncomingPacket(p);
                        _remoteSequence = NetUtils.IncrementSequenceNumber(_remoteSequence, 1);
                    }
                }
                else
                {
                    while (_earlyReceived[_remoteSequence % _windowSize])
                    {
                        //process early packet
                        _earlyReceived[_remoteSequence % _windowSize] = false;
                        _remoteSequence = NetUtils.IncrementSequenceNumber(_remoteSequence, 1);
                    }
                }

                return(true);
            }

            //holded packet
            if (_ordered)
            {
                // Doesnt matter if it overwrites multiple time the same packet
                _receivedPackets[packet.Sequence % _windowSize] = packet;
            }
            else
            {
                if (_earlyReceived[packet.Sequence % _windowSize] == false)
                {
                    _earlyReceived[packet.Sequence % _windowSize] = true;
                    _peer.AddIncomingPacket(packet);
                }
            }

            return(true);
        }