GetPacketFromPool() private method

private GetPacketFromPool ( PacketProperty property = PacketProperty.Unreliable, int size, bool init = true ) : NetPacket
property PacketProperty
size int
init bool
return NetPacket
Esempio n. 1
0
        public ReliableChannel(NetPeer peer, bool ordered)
        {
            _windowSize = NetConstants.DefaultWindowSize;
            _peer       = peer;
            _ordered    = ordered;

            _outgoingPackets = new Queue <NetPacket>(_windowSize);
            _pendingPackets  = new PendingPacket[_windowSize];
            for (int i = 0; i < _pendingPackets.Length; i++)
            {
                _pendingPackets[i] = new PendingPacket();
            }

            if (_ordered)
            {
                _receivedPackets = new NetPacket[_windowSize];
            }
            else
            {
                _earlyReceived = new bool[_windowSize];
            }

            _localWindowStart  = 0;
            _localSeqence      = 0;
            _remoteSequence    = 0;
            _remoteWindowStart = 0;

            //Init acks packet
            int            bytesCount = (_windowSize - 1) / BitsInByte + 1;
            PacketProperty property   = _ordered ? PacketProperty.AckReliableOrdered : PacketProperty.AckReliable;

            _outgoingAcks = _peer.GetPacketFromPool(property, bytesCount);
        }
Esempio n. 2
0
        public ReliableChannel(NetPeer peer, bool ordered, int channel)
        {
            _windowSize = NetConstants.DefaultWindowSize;
            _peer       = peer;
            _ordered    = ordered;
            _channel    = channel;

            _outgoingPackets = new List <NetPacket>(_windowSize);
            _pendingPackets  = new PendingPacket[_windowSize];
            for (int i = 0; i < _pendingPackets.Length; i++)
            {
                _pendingPackets[i]     = new PendingPacket();
                _pendingPackets[i].idx = i;
            }

            if (_ordered)
            {
                _receivedPackets = new NetPacket[_windowSize];
            }
            else
            {
                _earlyReceived = new bool[_windowSize];
            }

            _localSequence        = 0;
            _remoteSequence       = 0;
            _packetsToAcknowledge = new List <ushort>();

            _mustSendAcksStartTimer = -1;

            //Init acks packet
            PacketProperty property = _ordered ? PacketProperty.AckReliableOrdered : PacketProperty.AckReliable;

            _outgoingAcks = _peer.GetPacketFromPool(property, channel, NetConstants.MinPacketDataSize);
        }
        public void SendAcks()
        {
            if (!_mustSendAcks)
            {
                return;
            }
            _mustSendAcks = false;

            NetUtils.DebugWrite("[RR]SendAcks");

            //Init packet
            int            bytesCount = (_windowSize - 1) / BitsInByte + 1;
            PacketProperty property   = _ordered ? PacketProperty.AckReliableOrdered : PacketProperty.AckReliable;
            var            acksPacket = _peer.GetPacketFromPool(property, bytesCount);

            //For quick access
            byte[] data = acksPacket.RawData; //window start + acks size

            //Put window start
            Monitor.Enter(_outgoingAcks);
            acksPacket.Sequence = (ushort)_remoteWindowStart;

            //Put acks
            int startAckIndex   = _remoteWindowStart % _windowSize;
            int currentAckIndex = startAckIndex;
            int currentBit      = 0;
            int currentByte     = NetConstants.SequencedHeaderSize;

            do
            {
                if (_outgoingAcks[currentAckIndex])
                {
                    data[currentByte] |= (byte)(1 << currentBit);
                }

                currentBit++;
                if (currentBit == BitsInByte)
                {
                    currentByte++;
                    currentBit = 0;
                }
                currentAckIndex = (currentAckIndex + 1) % _windowSize;
            } while (currentAckIndex != startAckIndex);
            Monitor.Exit(_outgoingAcks);

            _peer.SendRawData(acksPacket);
            _peer.Recycle(acksPacket);
        }
Esempio n. 4
0
        protected override void ReceiveFromSocket(byte[] reusableBuffer, int count, NetEndPoint remoteEndPoint)
        {
            //Parse packet
            //Peer null when P2P connection packets
            NetPacket packet = _peer == null ? new NetPacket() : _peer.GetPacketFromPool(init: false);

            if (!packet.FromBytes(reusableBuffer, count))
            {
                if (_peer != null)
                {
                    _peer.Recycle(packet);
                }
                return;
            }

            //Check P2P mode
            if (PeerToPeerMode && packet.Property == PacketProperty.ConnectRequest)
            {
                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Received peer connect request");

                string peerKey = Encoding.UTF8.GetString(packet.RawData, 9, packet.RawData.Length - 9);
                if (peerKey != _connectKey)
                {
                    NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Peer connect reject. Invalid key: " + peerKey);
                    return;
                }

                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Peer connect accepting");

                //Make initial packet and put id from received packet
                var connectPacket = NetPacket.CreateRawPacket(PacketProperty.ConnectAccept, 8);
                Buffer.BlockCopy(packet.RawData, 1, connectPacket, 1, 8);

                //Check our peer and create
                if (_peer == null)
                {
                    //Create connect id for proper connection
                    Connect(remoteEndPoint);
                }

                //Send raw
                _peer.SendRawData(connectPacket);

                //clean incoming packet
                _peer.Recycle(packet);

                //We connected
                ProcessConnectAccept();

                return;
            }

            //Check peer
            if (_peer == null)
            {
                return;
            }

            //Check endpoint
            if (!_peer.EndPoint.Equals(remoteEndPoint))
            {
                NetUtils.DebugWriteForce(ConsoleColor.DarkCyan, "[NC] Bad EndPoint " + remoteEndPoint);
                return;
            }

            if (packet.Property == PacketProperty.Disconnect)
            {
                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Received disconnection");
                CloseConnection(true);
                var disconnectEvent = CreateEvent(NetEventType.Disconnect);
                disconnectEvent.AdditionalInfo = "Received disconnection from server";
                EnqueueEvent(disconnectEvent);
                return;
            }

            if (packet.Property == PacketProperty.ConnectAccept)
            {
                if (_connected)
                {
                    return;
                }

                //check connection id
                if (BitConverter.ToUInt64(packet.RawData, 1) != _connectId)
                {
                    return;
                }

                //connection things
                ProcessConnectAccept();
                return;
            }

            //Process income packet
            _peer.ProcessPacket(packet);
        }