public void Close(bool suspend) { if (!suspend) { IsRunning = false; #if UNITY_IOS && !UNITY_EDITOR _unitySocketFix.Socket = null; _unitySocketFix = null; #endif } /* * if (_udpSocketv4 != null) * _udpSocketv4.Close(); * if (_udpSocketv6 != null) * _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; */ }
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; }
public void Close(bool suspend) { if (!suspend) { IsRunning = false; #if UNITY _unitySocketFix.Socket = null; _unitySocketFix = null; #endif } if (_udpSocketv4 != null) { _udpSocketv4.Close(); _udpSocketv4 = null; } if (_udpSocketv6 != null) { _udpSocketv6.Close(); _udpSocketv6 = null; } _threadv4 = null; _threadv6 = null; }
public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool reuseAddress, IPv6Mode ipv6Mode, bool manualMode) { if (IsActive()) { return(false); } _useNativeSockets = _listener.UseNativeSockets && NativeSocket.IsSupported; 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; _unitySocketFix.ManualMode = manualMode; } else { _unitySocketFix.Paused = false; } #endif if (dualMode) { _udpSocketv6 = _udpSocketv4; } IsRunning = true; if (!manualMode) { ParameterizedThreadStart ts = ReceiveLogic; if (_useNativeSockets) { ts = NativeReceiveLogic; } _threadv4 = new Thread(ts) { Name = $"SocketThreadv4({LocalPort})", IsBackground = true }; _threadv4.Start(_udpSocketv4); } else { _bufferEndPointv4 = new IPEndPoint(IPAddress.Any, 0); } //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)) { if (manualMode) { _bufferEndPointv6 = new IPEndPoint(IPAddress.IPv6Any, 0); } else { ParameterizedThreadStart ts = ReceiveLogic; if (_useNativeSockets) { ts = NativeReceiveLogic; } _threadv6 = new Thread(ts) { Name = $"SocketThreadv6({LocalPort})", IsBackground = true }; _threadv6.Start(_udpSocketv6); } } return(true); }
public bool Bind(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool reuseAddress, bool ipv6) { if (IsActive()) { return(false); } _udpSocketv4 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if (!BindSocket(_udpSocketv4, new IPEndPoint(addressIPv4, port), reuseAddress)) { return(false); } #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 = port; _unitySocketFix.IPv6 = ipv6; } #endif LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port; IsRunning = true; _threadv4 = new Thread(ReceiveLogic) { Name = "SocketThreadv4(" + LocalPort + ")", IsBackground = true }; _threadv4.Start(_udpSocketv4); //Check IPv6 support if (!IPv6Support || !ipv6) { 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)) { try { #if !UNITY _udpSocketv6.SetSocketOption( SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(MulticastAddressV6)); #endif } catch (Exception) { // Unity3d throws exception - ignored } _threadv6 = new Thread(ReceiveLogic) { Name = "SocketThreadv6(" + LocalPort + ")", IsBackground = true }; _threadv6.Start(_udpSocketv6); } return(true); }
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); } #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 = port; _unitySocketFix.IPv6 = ipv6Mode; } #endif if (dualMode) { _udpSocketv6 = _udpSocketv4; } LocalPort = ((IPEndPoint)_udpSocketv4.LocalEndPoint).Port; 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); }
/// <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> /// <param name="manualMode">mode of library</param> public bool Start(IPAddress addressIPv4, IPAddress addressIPv6, int port, bool manualMode) { if (IsRunning && !IsActive()) { return(false); } _manualMode = manualMode; UseNativeSockets = UseNativeSockets && NativeSocket.IsSupported; //osx doesn't support dual mode if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && IPv6Mode == IPv6Mode.DualMode) { IPv6Mode = IPv6Mode.SeparateSocket; } 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))) { 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.Port = LocalPort; _unitySocketFix.ManualMode = _manualMode; } else { _unitySocketFix.Paused = false; } #endif if (dualMode) { _udpSocketv6 = _udpSocketv4; } IsRunning = true; if (!_manualMode) { ParameterizedThreadStart ts = ReceiveLogic; if (UseNativeSockets) { ts = NativeReceiveLogic; } _threadv4 = new Thread(ts) { Name = $"SocketThreadv4({LocalPort})", IsBackground = true }; _threadv4.Start(_udpSocketv4); _logicThread = new Thread(UpdateLogic) { Name = "LogicThread", IsBackground = true }; _logicThread.Start(); } else { _bufferEndPointv4 = new IPEndPoint(IPAddress.Any, 0); } //Check IPv6 support if (IPv6Support && IPv6Mode == IPv6Mode.SeparateSocket) { _udpSocketv6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); //Use one port for two sockets if (BindSocket(_udpSocketv6, new IPEndPoint(addressIPv6, LocalPort))) { if (_manualMode) { _bufferEndPointv6 = new IPEndPoint(IPAddress.IPv6Any, 0); } else { ParameterizedThreadStart ts = ReceiveLogic; if (UseNativeSockets) { ts = NativeReceiveLogic; } _threadv6 = new Thread(ts) { Name = $"SocketThreadv6({LocalPort})", IsBackground = true }; _threadv6.Start(_udpSocketv6); } } } return(true); }