Пример #1
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 SwitchQueue <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
            for (int i = 0; i < maxConnections * 3; ++i)
            {
                NetPacket p = NetPacketPool.Get(PacketProperty.Sequenced, 0, NetConstants.MaxPacketSize);
                p.Recycle();
            }
        }
Пример #2
0
 //incoming connection constructor
 internal Player(NetManager netManager, IPEndPoint remoteEndPoint)
 {
     Statistics       = new NetStatistics();
     _packetPool      = netManager.NetPacketPool;
     _netManager      = netManager;
     _remoteEndPoint  = remoteEndPoint;
     _connectionState = ConnectionState.Incoming;
 }
Пример #3
0
 //incoming connection constructor
 internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint)
 {
     Statistics       = new NetStatistics();
     _packetPool      = netManager.NetPacketPool;
     _netManager      = netManager;
     _remoteEndPoint  = remoteEndPoint;
     _connectionState = ConnectionState.Incoming;
     _mergeData       = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize);
     _pongPacket      = new NetPacket(PacketProperty.Pong, 0);
     _pingPacket      = new NetPacket(PacketProperty.Ping, 0);
 }
 /// <summary>
 /// NetManager constructor
 /// </summary>
 /// <param name="listener">Network events listener</param>
 public NetManager(INetEventListener listener)
 {
     _socket                 = new NetSocket(this);
     _netEventListener       = listener;
     _netEventsQueue         = new Queue <NetEvent>();
     _netEventsPool          = new Stack <NetEvent>();
     NetPacketPool           = new NetPacketPool();
     NatPunchModule          = new NatPunchModule(_socket);
     Statistics              = new NetStatistics();
     _peers                  = new NetPeerCollection();
     _connectedPeerListCache = new List <NetPeer>();
 }
Пример #5
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)
 {
     _socket                 = new NetSocket(ReceiveLogic);
     _netEventListener       = listener;
     _netEventsQueue         = new Queue <NetEvent>();
     _netEventsPool          = new Stack <NetEvent>();
     NetPacketPool           = new NetPacketPool();
     NatPunchModule          = new NatPunchModule(_socket);
     Statistics              = new NetStatistics();
     _peers                  = new NetPeerCollection();
     _connectingPeers        = new HashSet <IPEndPoint>(new IPEndPointComparer());
     _maxConnections         = maxConnections;
     _connectedPeerListCache = new List <NetPeer>();
 }
Пример #6
0
        /// <summary>
        /// NetManager constructor
        /// </summary>
        /// <param name="listener">Network events listener</param>
        /// <param name="maxConnections">Maximum connections (incoming and outcoming)</param>
        /// <param name="connectKey">Application key (must be same with remote host for establish connection)</param>
        public NetManager(INetEventListener listener, int maxConnections, string connectKey)
        {
            _logicThread      = new NetThread("LogicThread", DefaultUpdateTime, UpdateLogic);
            _socket           = new NetSocket(ReceiveLogic);
            _netEventListener = listener;
            _netEventsQueue   = new Queue <NetEvent>();
            _netEventsPool    = new Stack <NetEvent>();
            _netPacketPool    = new NetPacketPool();
            NatPunchModule    = new NatPunchModule(this);
            Statistics        = new NetStatistics();

            ConnectKey      = connectKey;
            _peers          = new NetPeerCollection(maxConnections);
            _maxConnections = maxConnections;
            ConnectKey      = connectKey;
        }
Пример #7
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 Queue <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;
 }
Пример #8
0
 //incoming connection constructor
 internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id)
 {
     Id                        = id;
     Statistics                = new NetStatistics();
     _packetPool               = netManager.NetPacketPool;
     _netManager               = netManager;
     _remoteEndPoint           = remoteEndPoint;
     _connectionState          = ConnectionState.Incoming;
     _mergeData                = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize);
     _pongPacket               = new NetPacket(PacketProperty.Pong, 0);
     _pingPacket               = new NetPacket(PacketProperty.Ping, 0);
     _reliableOrderedChannel   = new ReliableChannel(this, true);
     _reliableUnorderedChannel = new ReliableChannel(this, false);
     _sequencedChannel         = new SequencedChannel(this, false);
     _unreliableChannel        = new SimpleChannel(this);
     _reliableSequencedChannel = new SequencedChannel(this, true);
     _holdedFragments          = new Dictionary <ushort, IncomingFragments>();
 }
Пример #9
0
        //incoming connection constructor
        internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id)
        {
            Id               = id;
            Statistics       = new NetStatistics();
            _packetPool      = netManager.NetPacketPool;
            _netManager      = netManager;
            _remoteEndPoint  = remoteEndPoint;
            _connectionState = ConnectionState.Incoming;
            _mergeData       = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize);
            _pongPacket      = new NetPacket(PacketProperty.Pong, 0);
            _pingPacket      = new NetPacket(PacketProperty.Ping, 0)
            {
                Sequence = 1
            };

            _unreliableChannel = new SimpleChannel(this);
            _headChannel       = _unreliableChannel;
            _holdedFragments   = new Dictionary <ushort, IncomingFragments>();

            _channels = new BaseChannel[netManager.ChannelsCount * 4];
        }
Пример #10
0
        private NetPeer(NetManager netManager, NetEndPoint remoteEndPoint)
        {
            Statistics      = new NetStatistics();
            _packetPool     = netManager.NetPacketPool;
            _netManager     = netManager;
            _remoteEndPoint = remoteEndPoint;

            _avgRtt        = 0;
            _rtt           = 0;
            _pingSendTimer = 0;

            _reliableOrderedChannel   = new ReliableChannel(this, true);
            _reliableUnorderedChannel = new ReliableChannel(this, false);
            _sequencedChannel         = new SequencedChannel(this);
            _simpleChannel            = new SimpleChannel(this);
            _reliableSequencedChannel = new ReliableSequencedChannel(this);

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

            _mergeData = _packetPool.Get(PacketProperty.Merged, NetConstants.MaxPacketSize);
        }
Пример #11
0
        //incoming connection constructor
        internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id)
        {
            Id          = id;
            Statistics  = new NetStatistics();
            _packetPool = netManager.NetPacketPool;
            NetManager  = netManager;

            if (netManager.MtuOverride > 0)
            {
                OverrideMtu(netManager.MtuOverride);
            }
            else if (netManager.UseSafeMtu)
            {
                SetMtu(0);
            }
            else
            {
                SetMtu(1);
            }

            EndPoint         = remoteEndPoint;
            _connectionState = ConnectionState.Connected;
            _mergeData       = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize);
            _pongPacket      = new NetPacket(PacketProperty.Pong, 0);
            _pingPacket      = new NetPacket(PacketProperty.Ping, 0)
            {
                Sequence = 1
            };

            _unreliableChannel  = new Queue <NetPacket>(64);
            _holdedFragments    = new Dictionary <ushort, IncomingFragments>();
            _deliveredFragments = new Dictionary <ushort, ushort>();

            _channels         = new BaseChannel[netManager.ChannelsCount * 4];
            _channelSendQueue = new Queue <BaseChannel>(netManager.ChannelsCount * 4);
        }
Пример #12
0
        internal NetPeer(NetManager peerListener, NetEndPoint remoteEndPoint, long connectId)
        {
            Statistics      = new NetStatistics();
            _packetPool     = peerListener.PacketPool;
            _peerListener   = peerListener;
            _remoteEndPoint = remoteEndPoint;

            _avgRtt        = 0;
            _rtt           = 0;
            _pingSendTimer = 0;

            _reliableOrderedChannel   = new ReliableChannel(this, true);
            _reliableUnorderedChannel = new ReliableChannel(this, false);
            _sequencedChannel         = new SequencedChannel(this);
            _simpleChannel            = new SimpleChannel(this);

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

            _mergeData = _packetPool.Get(PacketProperty.Merged, NetConstants.MaxPacketSize);

            //if ID != 0 then we already connected
            _connectAttempts = 0;
            if (connectId == 0)
            {
                _connectId = DateTime.UtcNow.Ticks;
                SendConnectRequest();
            }
            else
            {
                _connectId       = connectId;
                _connectionState = ConnectionState.Connected;
                SendConnectAccept();
            }

            NetUtils.DebugWrite(ConsoleColor.Cyan, "[CC] ConnectId: {0}", _connectId);
        }
Пример #13
0
        //incoming connection constructor
        internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id)
        {
            Id          = id;
            Statistics  = new NetStatistics();
            _packetPool = netManager.NetPacketPool;
            NetManager  = netManager;
            SetMtu(0);

            EndPoint         = remoteEndPoint;
            _connectionState = ConnectionState.Connected;
            _mergeData       = new NetPacket(PacketProperty.Merged, NetConstants.MaxPacketSize);
            _pongPacket      = new NetPacket(PacketProperty.Pong, 0);
            _pingPacket      = new NetPacket(PacketProperty.Ping, 0)
            {
                Sequence = 1
            };

            _unreliableChannel  = new Queue <NetPacket>(64);
            _headChannel        = null;
            _holdedFragments    = new Dictionary <ushort, IncomingFragments>();
            _deliveredFramgnets = new Dictionary <ushort, ushort>();

            _channels = new BaseChannel[netManager.ChannelsCount * 4];
        }
Пример #14
0
        private NetPeer(NetManager netManager, NetEndPoint remoteEndPoint)
        {
            Statistics      = new NetStatistics();
            _packetPool     = netManager.NetPacketPool;
            _netManager     = netManager;
            _remoteEndPoint = remoteEndPoint;

            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>();

            _mergeData = _packetPool.Get(PacketProperty.Merged, 0, NetConstants.MaxPacketSize);
        }