//closes out all the UDPClients and Threads public static void endUDPClientAndThread() { serverState = EasyWiFiConstants.CURRENT_SERVER_STATE.Disconnecting; //close connections if (clientListen != null) { clientListen.Close(); clientListen = null; } if (clientBroadcast != null) { clientBroadcast.Close(); clientBroadcast = null; } if (serverListen != null) { serverListen.Close(); serverListen = null; } if (clientSend != null) { clientSend.Close(); clientSend = null; } if (serverSendKeys != null) { foreach (string key in serverSendKeys) { if (serverSendDictionary[key] != null) { serverSendDictionary[key].Close(); serverSendDictionary[key] = null; } } } //abort threads if (listenThread != null) { #if !UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS) listenThread.Interrupt(); #else listenThread.Abort();//not support in il2cpp #endif listenThread = null; } if (broadcastThread != null) { #if !UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS) broadcastThread.Interrupt(); #else broadcastThread.Abort();//not support in il2cpp #endif broadcastThread = null; } }
public static void initialize(string name, string type, int serverPort, int clientPort, bool verbose, bool clientConnectAutomatically) { appName = name; peerType = type; serverSocketListenPort = serverPort; clientScoketListenPort = clientPort; isVerbose = verbose; myIPAddress = Network.player.ipAddress; controllerDataDictionary = new Dictionary <string, BaseControllerType>(); serverSendKeys = new List <string>(); if (isVerbose) { Debug.Log("Initializing Easy WiFi Controller..."); } if (peerType.Equals(EasyWiFiConstants.PEERTYPE_SERVER)) { serverIPAddress = Network.player.ipAddress; serverSendDictionary = new Dictionary <string, UdpClient>(); serverSendEndPointDictionary = new Dictionary <string, IPEndPoint>(); //setup for server listening (setup for server send happens later) serverListen = new UdpClient() { EnableBroadcast = true }; serverListen.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); serverListenEndPoint = new IPEndPoint(IPAddress.Any, serverSocketListenPort); serverListen.Client.Bind(serverListenEndPoint); listenThread = new Thread(new ThreadStart(listen)) { IsBackground = true }; listenThread.Start(); serverState = EasyWiFiConstants.CURRENT_SERVER_STATE.Listening; } else if (peerType.Equals(EasyWiFiConstants.PEERTYPE_CLIENT)) { //if we're initing with connect automatically this will be changed below when checking for server clientState = EasyWiFiConstants.CURRENT_CLIENT_STATE.NotConnected; //setup for client broadcast (setup for client send to server happens later) clientBroadcast = new UdpClient() { EnableBroadcast = true }; clientBroadcastEndPoint = new IPEndPoint(IPAddress.Broadcast, serverSocketListenPort); //setup for client listening clientListen = new UdpClient() { EnableBroadcast = true }; clientListen.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); clientListenEndPoint = new IPEndPoint(IPAddress.Any, clientScoketListenPort); clientListen.Client.Bind(clientListenEndPoint); listenThread = new Thread(new ThreadStart(listen)) { IsBackground = true }; listenThread.Start(); if (clientConnectAutomatically) { checkForServer(); } } }