Bind() public method

public Bind ( int port ) : bool
port int
return bool
Exemplo n.º 1
0
 public NtpSyncModule(string ntpServer)
 {
     _ntpEndPoint = new NetEndPoint(ntpServer, 123);
     _socket = new NetSocket(OnMessageReceived);
     _socket.Bind(0);
     SyncedTime = null;
 }
Exemplo n.º 2
0
 public NtpSyncModule(string ntpServer)
 {
     _ntpEndPoint = new NetEndPoint(ntpServer, 123);
     _socket      = new NetSocket(OnMessageReceived);
     _socket.Bind(0);
     SyncedTime = null;
 }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Start logic thread and listening on selected port
        /// </summary>
        /// <param name="port">port to listen</param>
        public virtual bool Start(int port)
        {
            if (_running)
            {
                return(false);
            }

            _netEventsQueue.Clear();
            if (!_socket.Bind(port))
            {
                return(false);
            }

            _running = true;
#if WINRT && !UNITY_EDITOR
            ThreadPool.RunAsync(
                a => UpdateLogic(),
                WorkItemPriority.Normal,
                WorkItemOptions.TimeSliced).AsTask();
#else
            _logicThread              = new Thread(UpdateLogic);
            _logicThread.Name         = "LogicThread(" + port + ")";
            _logicThread.IsBackground = true;
            _logicThread.Start();
#endif
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Start logic thread and listening on selected port
        /// </summary>
        /// <param name="port">port to listen</param>
        public virtual bool Start(int port)
        {
            if (_running)
            {
                return(false);
            }

            _netEventsQueue.Clear();
            _localEndPoint = new NetEndPoint(_addressType, port);
            if (!_socket.Bind(ref _localEndPoint))
            {
                return(false);
            }

            _running = true;
#if WINRT && !UNITY_EDITOR
            ThreadPool.RunAsync(
                a => UpdateLogic(),
                WorkItemPriority.Normal,
                WorkItemOptions.TimeSliced).AsTask();

            ThreadPool.RunAsync(
                a => ReceiveLogic(),
                WorkItemPriority.Normal,
                WorkItemOptions.TimeSliced).AsTask();
#else
            _logicThread   = new Thread(UpdateLogic);
            _receiveThread = new Thread(ReceiveLogic);
            _logicThread.Start();
            _receiveThread.Start();
#endif
            return(true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Start logic thread and listening on selected port
        /// </summary>
        /// <param name="addressIPv4">bind to specific ipv4 address</param>
        /// <param name="addressIPv6">bind to specific ipv6 address</param>
        /// <param name="port">port to listen</param>
        public bool Start(string addressIPv4, string addressIPv6, int port)
        {
            if (IsRunning)
            {
                return(false);
            }
            _netEventsQueue.Clear();
            IPAddress ipv4 = NetEndPoint.GetFromString(addressIPv4);
            IPAddress ipv6 = NetEndPoint.GetFromString(addressIPv6);

            if (!_socket.Bind(ipv4, ipv6, port, ReuseAddress))
            {
                return(false);
            }
            IsRunning = true;
            _logicThread.Start();
            return(true);
        }
Exemplo n.º 7
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;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Start logic thread and listening on selected port
        /// </summary>
        /// <param name="addressIPv4">bind to specific ipv4 address</param>
        /// <param name="addressIPv6">bind to specific ipv6 address</param>
        /// <param name="port">port to listen</param>
        public bool Start(string addressIPv4, string addressIPv6, int port)
        {
            if (IsRunning)
            {
                return(false);
            }
            _netEventsQueue.Clear();
            IPAddress ipv4 = NetUtils.ResolveAddress(addressIPv4);
            IPAddress ipv6 = NetUtils.ResolveAddress(addressIPv6);

            if (!_socket.Bind(ipv4, ipv6, port, ReuseAddress))
            {
                return(false);
            }
            IsRunning    = true;
            _logicThread = new Thread(UpdateLogic)
            {
                Name = "LogicThread", IsBackground = true
            };
            _logicThread.Start();
            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Start logic thread and listening on selected port
        /// </summary>
        /// <param name="port">port to listen</param>
        public bool Start(int port)
        {
            if (IsRunning)
            {
                return(false);
            }

            _netEventsQueue.Clear();
            if (!_socket.Bind(port, ReuseAddress))
            {
                return(false);
            }

            _logicThread.Start();
            return(true);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Start logic thread and listening on selected port
 /// </summary>
 /// <param name="addressIPv4">bind to specific ipv4 address</param>
 /// <param name="addressIPv6">bind to specific ipv6 address</param>
 /// <param name="port">port to listen</param>
 public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port)
 {
     if (IsRunning)
     {
         return(false);
     }
     if (!_socket.Bind(addressIPv4, addressIPv6, port, ReuseAddress))
     {
         return(false);
     }
     IsRunning    = true;
     _logicThread = new Thread(UpdateLogic)
     {
         Name = "LogicThread", IsBackground = true
     };
     _logicThread.Start();
     return(true);
 }
Exemplo n.º 11
0
 private void OnApplicationPause(bool pause)
 {
     if (Socket == null)
     {
         return;
     }
     if (pause)
     {
         Paused = true;
         Socket.Close(true);
     }
     else if (Paused)
     {
         if (!Socket.Bind(BindAddrIPv4, BindAddrIPv6, Port, Reuse, IPv6, ManualMode))
         {
             NetDebug.WriteError("[S] Cannot restore connection \"{0}\",\"{1}\" port {2}", BindAddrIPv4, BindAddrIPv6, Port);
             Socket.OnErrorRestore();
         }
     }
 }
Exemplo n.º 12
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);
            }
        }