Example #1
0
 public NetPacketPool()
 {
     _pool    = new FastQueue <NetPacket> [9];
     _pool[0] = new FastQueue <NetPacket>(); // 16
     _pool[1] = new FastQueue <NetPacket>(); // 32
     _pool[2] = new FastQueue <NetPacket>(); // 64
     _pool[3] = _pool[2];                    // 64
     _pool[4] = new FastQueue <NetPacket>(); // 128
     _pool[5] = _pool[4];                    // 128
     _pool[6] = _pool[4];                    // 128
     _pool[7] = _pool[4];                    // 128
     _pool[8] = new FastQueue <NetPacket>(); // others
 }
Example #2
0
        /// <summary>
        /// NetManager constructor
        /// </summary>
        /// <param name="listener">Network events listener</param>
        /// <param name="maxConnections">Maximum connections (incoming and outcoming)</param>
        public NetManager(INetEventListener listener, int maxConnections)
        {
            //_logicThread = new Thread(UpdateLogic) { Name = "LogicThread", IsBackground = true };
            _socket           = new NetSocket(ReceiveLogic);
            _netEventListener = listener;
            _netEventsQueue   = new FastQueue <NetEvent>();
            _netEventsPool    = new Stack <NetEvent>();
            NetPacketPool     = new NetPacketPool();
            NatPunchModule    = new NatPunchModule(this);
            Statistics        = new NetStatistics();
            _peers            = new NetPeerCollection(maxConnections);
            _connectingPeers  = new HashSet <NetEndPoint>();
            _maxConnections   = maxConnections;
            _updateTimeFilter = new long[3];

            // Precreate all needed Merge Packets
            NetPacketPool.PoolLimit = maxConnections * 100;
            //NetPacketPool.Prepool(maxConnections * 2, MtuStartIdx >= 0 && MtuStartIdx < NetConstants.PossibleMtu.Length ? NetConstants.PossibleMtu[MtuStartIdx] : (NetConstants.MaxPacketSize - NetConstants.FragmentHeaderSize));
            //NetPacketPool.Prepool(maxConnections * 20, 16);
        }
Example #3
0
        public ReliableChannel(NetPeer peer, bool ordered, int channel)
        {
            _peer    = peer;
            _ordered = ordered;
            _channel = channel;

            _outgoingPackets = new FastQueue <NetPacket>(NetConstants.DefaultWindowSize);
            _pendingPackets  = new PendingPacket[NetConstants.DefaultWindowSize];
            for (int i = 0; i < _pendingPackets.Length; i++)
            {
                _pendingPackets[i]     = new PendingPacket();
                _pendingPackets[i].idx = i;
                if (i != 0)
                {
                    _pendingPackets[i - 1].Next = _pendingPackets[i];
                }
            }
            _pendingPackets[_pendingPackets.Length - 1].Next = _pendingPackets[0];

            _tailPendingPacket = _headPendingPacket = _pendingPackets[0];

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

            _localSequence        = 0;
            _remoteSequence       = NetConstants.MaxSequence;
            _packetsToAcknowledge = new List <ushort>(NetConstants.DefaultWindowSize * 2);

            _mustSendAcksStartTimer = -1;
        }
Example #4
0
 public SimpleChannel(NetPeer peer, int channel)
 {
     _outgoingPackets = new FastQueue <NetPacket>(NetConstants.DefaultWindowSize);
     _peer            = peer;
     _channel         = channel;
 }
Example #5
0
        private NetPeer(NetManager netManager, NetEndPoint remoteEndPoint)
        {
            Statistics      = new NetStatistics();
            _packetPool     = netManager.NetPacketPool;
            _netManager     = netManager;
            _remoteEndPoint = remoteEndPoint;
            _mergedPackets  = new FastQueue <NetPacket>(NetConstants.DefaultWindowSize);

            if (netManager.MtuStartIdx >= 0 && netManager.MtuStartIdx < NetConstants.PossibleMtu.Length)
            {
                _mtuIdx    = netManager.MtuStartIdx;
                _mtu       = NetConstants.PossibleMtu[_mtuIdx];
                _finishMtu = true;
            }

            _avgRtt        = 0;
            _rtt           = 0;
            _pingSendTimer = 0;
            _pingMustSend  = false;

            if (NetManager.EnableReliableOrderedChannel)
            {
                _reliableOrderedChannels = new ReliableChannel[NetConstants.MultiChannelCount];
            }
            if (NetManager.EnableReliableUnorderedChannel)
            {
                _reliableUnorderedChannels = new ReliableChannel[NetConstants.MultiChannelCount];
            }
            if (NetManager.EnableSequencedChannel)
            {
                _sequencedChannels = new SequencedChannel[NetConstants.MultiChannelCount];
            }
            if (NetManager.EnableSimpleChannel)
            {
                _simpleChannels = new SimpleChannel[NetConstants.MultiChannelCount];
            }
            //_reliableSequencedChannels = new ReliableSequencedChannel[NetConstants.MultiChannelCount];

            // Initialise default channel
            for (int i = 0; i < NetConstants.MultiChannelCount; ++i)
            {
                if (NetManager.EnableReliableOrderedChannel)
                {
                    _reliableOrderedChannels[i] = new ReliableChannel(this, true, i);
                }
                if (NetManager.EnableReliableUnorderedChannel)
                {
                    _reliableUnorderedChannels[i] = new ReliableChannel(this, false, i);
                }
                if (NetManager.EnableSequencedChannel)
                {
                    _sequencedChannels[i] = new SequencedChannel(this, i);
                }
                if (NetManager.EnableSimpleChannel)
                {
                    _simpleChannels[i] = new SimpleChannel(this, i);
                }
                //_reliableSequencedChannels[i] = new ReliableSequencedChannel(this, i);
            }

            _holdedFragments = new Dictionary <ushort, IncomingFragments>();
        }