Пример #1
0
        /// <summary>
        /// Request time from NTP server and calls callback (if success)
        /// </summary>
        /// <param name="ntpServerAddress">NTP Server address</param>
        /// <param name="port">port</param>
        /// <param name="onRequestComplete">callback (called from other thread!)</param>
        public static void RequestTimeFromNTP(string ntpServerAddress, int port, Action <DateTime?> onRequestComplete)
        {
            NetSocket socket      = null;
            var       ntpEndPoint = new NetEndPoint(ntpServerAddress, port);

            NetManager.OnMessageReceived onReceive = (data, length, code, point) =>
            {
                if (!point.Equals(ntpEndPoint) || length < 48)
                {
                    return;
                }
                socket.Close();

                ulong intPart      = (ulong)data[40] << 24 | (ulong)data[41] << 16 | (ulong)data[42] << 8 | (ulong)data[43];
                ulong fractPart    = (ulong)data[44] << 24 | (ulong)data[45] << 16 | (ulong)data[46] << 8 | (ulong)data[47];
                var   milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
                onRequestComplete(new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)milliseconds));
            };

            //Create and start socket
            socket = new NetSocket(onReceive);
            socket.Bind(0, false);

            //Send request
            int errorCode = 0;
            var sendData  = new byte[48];

            sendData[0] = 0x1B;
            var sendCount = socket.SendTo(sendData, 0, sendData.Length, ntpEndPoint, ref errorCode);

            if (errorCode != 0 || sendCount != sendData.Length)
            {
                onRequestComplete(null);
            }
        }
Пример #2
0
 internal NatPunchModule(NetBase netBase, NetSocket socket)
 {
     _netBase       = netBase;
     _socket        = socket;
     _requestEvents = new Queue <RequestEventData>();
     _successEvents = new Queue <SuccessEventData>();
 }
Пример #3
0
 internal NatPunchModule(NetSocket socket)
 {
     _socket = socket;
     _netPacketProcessor.SubscribeReusable <NatIntroduceResponsePacket>(OnNatIntroductionResponse);
     _netPacketProcessor.SubscribeReusable <NatIntroduceRequestPacket, IPEndPoint>(OnNatIntroductionRequest);
     _netPacketProcessor.SubscribeReusable <NatPunchPacket, IPEndPoint>(OnNatPunch);
 }
Пример #4
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();
            }
        }
Пример #5
0
 public NtpSyncModule(string ntpServer)
 {
     _ntpEndPoint = new NetEndPoint(ntpServer, 123);
     _socket = new NetSocket(OnMessageReceived);
     _socket.Bind(0);
     SyncedTime = null;
 }
Пример #6
0
 public NtpSyncModule(string ntpServer)
 {
     _ntpEndPoint = new NetEndPoint(ntpServer, 123);
     _socket      = new NetSocket(OnMessageReceived);
     _socket.Bind(0);
     SyncedTime = null;
 }
Пример #7
0
 protected NetBase(INetEventListener listener)
 {
     _socket           = new NetSocket(ReceiveLogic);
     _netEventListener = listener;
     _flowModes        = new List <FlowMode>();
     _netEventsQueue   = new Queue <NetEvent>();
     _netEventsPool    = new Stack <NetEvent>();
     NatPunchModule    = new NatPunchModule(this, _socket);
 }
Пример #8
0
 protected NetBase(INetEventListener listener)
 {
     _logicThread      = new NetThread("LogicThread", DefaultUpdateTime, UpdateLogic);
     _socket           = new NetSocket(ReceiveLogic);
     _netEventListener = listener;
     _flowModes        = new List <FlowMode>();
     _netEventsQueue   = new Queue <NetEvent>();
     _netEventsPool    = new Stack <NetEvent>();
     NatPunchModule    = new NatPunchModule(this, _socket);
 }
Пример #9
0
        public NtpSyncModule(string ntpServer)
        {
            _socket = new NetSocket(ConnectionAddressType.IPv4);
            NetEndPoint ourEndPoint = new NetEndPoint(ConnectionAddressType.IPv4, 0);

            _socket.Bind(ref ourEndPoint);
            _socket.ReceiveTimeout = 3000;
            _ntpEndPoint           = new NetEndPoint(ntpServer, 123);
            SyncedTime             = null;
        }
Пример #10
0
 protected NetBase(INetEventListener listener, ConnectionAddressType addressType)
 {
     _socket           = new NetSocket(addressType);
     _addressType      = addressType;
     _netEventListener = listener;
     _flowModes        = new List <FlowMode>();
     _netEventsQueue   = new Queue <NetEvent>();
     _netEventsPool    = new Stack <NetEvent>();
     _remoteEndPoint   = new NetEndPoint(_addressType, 0);
     NatPunchModule    = new NatPunchModule(this, _socket);
 }
Пример #11
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>();
 }
Пример #12
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>();
 }
Пример #13
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;
            _flowModes        = new List <FlowMode>();
            _netEventsQueue   = new Queue <NetEvent>();
            _netEventsPool    = new Stack <NetEvent>();
            _netPacketPool    = new NetPacketPool();
            NatPunchModule    = new NatPunchModule(this);

            _connectKey     = connectKey;
            _peers          = new NetPeerCollection(maxConnections);
            _maxConnections = maxConnections;
            _connectKey     = connectKey;
        }
Пример #14
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;
 }
Пример #15
0
        private NtpRequest(IPEndPoint endPoint, Action <DateTime?> onRequestComplete)
        {
            _ntpEndPoint       = endPoint;
            _onRequestComplete = onRequestComplete;
            //Create and start socket
            _socket = new NetSocket(this);
            _socket.Bind(IPAddress.Any, IPAddress.IPv6Any, 0, false);

            //Send request
            SocketError errorCode = 0;
            var         sendData  = new byte[48];

            sendData[0] = 0x1B;
            var sendCount = _socket.SendTo(sendData, 0, sendData.Length, _ntpEndPoint, ref errorCode);

            if (errorCode != 0 || sendCount != sendData.Length)
            {
                _onRequestComplete(null);
            }
        }
Пример #16
0
        internal NetPeer(NetBase peerListener, NetSocket socket, NetEndPoint remoteEndPoint)
        {
            _id           = remoteEndPoint.GetId();
            _peerListener = peerListener;

            _socket         = socket;
            _remoteEndPoint = remoteEndPoint;

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

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

            _packetPool      = new Stack <NetPacket>();
            _holdedFragments = new Dictionary <ushort, IncomingFragments>();
        }
Пример #17
0
        /// <summary>
        /// Stop updating thread and listening
        /// </summary>
        public virtual void Stop()
        {
            if (_running)
            {
                _running = false;
#if !WINRT || UNITY_EDITOR
                if (Thread.CurrentThread != _logicThread)
                {
                    _logicThread.Join();
                }
                if (Thread.CurrentThread != _receiveThread)
                {
                    _receiveThread.Join();
                }
                _logicThread   = null;
                _receiveThread = null;
#endif
                _socket.Close();
                _socket = null;
            }
        }
Пример #18
0
 internal NatPunchModule(NetBase netBase, NetSocket socket)
 {
     _netBase = netBase;
     _requestEvents = new Queue<RequestEventData>();
     _successEvents = new Queue<SuccessEventData>();
 }
Пример #19
0
 protected NetBase(INetEventListener listener)
 {
     _socket = new NetSocket(ReceiveLogic);
     _netEventListener = listener;
     _flowModes = new List<FlowMode>();
     _netEventsQueue = new Queue<NetEvent>();
     _netEventsPool = new Stack<NetEvent>();
     NatPunchModule = new NatPunchModule(this, _socket);
 }