Exemplo n.º 1
0
        public void Close(bool suspend)
        {
            if (!suspend)
            {
                IsRunning = false;
#if UNITY_IOS && !UNITY_EDITOR
                _unitySocketFix.Socket = null;
                _unitySocketFix        = null;
#endif
            }

            //cleanup dual mode
            if (_udpSocketv4 == _udpSocketv6)
            {
                _udpSocketv6 = null;
            }

            _udpSocketv4?.Close();
            _udpSocketv6?.Close();
            _udpSocketv4 = null;
            _udpSocketv6 = null;

            if (_threadv4 != null && _threadv4 != Thread.CurrentThread)
            {
                _threadv4.Join();
            }
            if (_threadv6 != null && _threadv6 != Thread.CurrentThread)
            {
                _threadv6.Join();
            }
            _threadv4 = null;
            _threadv6 = null;
        }
Exemplo n.º 2
0
        public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool reuseAddress, IPv6Mode ipv6Mode)
        {
            if (IsActive())
            {
                return(false);
            }
            bool dualMode = ipv6Mode == IPv6Mode.DualMode && IPv6Support;

            _udpSocketv4 = new Socket(dualMode ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork,
                                      SocketType.Dgram, ProtocolType.Udp);

            if (!BindSocket(_udpSocketv4, new IPEndPoint(dualMode ? addressIPv6 : addressIPv4, port), reuseAddress,
                            ipv6Mode))
            {
                return(false);
            }

            LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port;

#if UNITY_IOS && !UNITY_EDITOR
            if (_unitySocketFix == null)
            {
                var unityFixObj = new GameObject("LiteNetLib_UnitySocketFix");
                GameObject.DontDestroyOnLoad(unityFixObj);
                _unitySocketFix              = unityFixObj.AddComponent <UnitySocketFix>();
                _unitySocketFix.Socket       = this;
                _unitySocketFix.BindAddrIPv4 = addressIPv4;
                _unitySocketFix.BindAddrIPv6 = addressIPv6;
                _unitySocketFix.Reuse        = reuseAddress;
                _unitySocketFix.Port         = LocalPort;
                _unitySocketFix.IPv6         = ipv6Mode;
            }
            else
            {
                _unitySocketFix.Paused = false;
            }
#endif
            if (dualMode)
            {
                _udpSocketv6 = _udpSocketv4;
            }

            IsRunning = true;
            _threadv4 = new Thread(ReceiveLogic)
            {
                Name = "SocketThreadv4(" + LocalPort + ")", IsBackground = true
            };
            _threadv4.Start(_udpSocketv4);

            //Check IPv6 support
            if (!IPv6Support || ipv6Mode != IPv6Mode.SeparateSocket)
            {
                return(true);
            }

            _udpSocketv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
            //Use one port for two sockets
            if (BindSocket(_udpSocketv6, new IPEndPoint(addressIPv6, LocalPort), reuseAddress, ipv6Mode))
            {
                _threadv6 = new Thread(ReceiveLogic)
                {
                    Name = "SocketThreadv6(" + LocalPort + ")", IsBackground = true
                };
                _threadv6.Start(_udpSocketv6);
            }

            return(true);
        }