//This method looks through the data structure and scans for timedout connections public static void checkForClientTimeout(float timeout) { BaseControllerType temp; bool foundDeadConnection = false; if (EasyWiFiController.controllerDataDictionary != null) { foreach (string key in EasyWiFiController.controllerDataDictionary.Keys) { temp = EasyWiFiController.controllerDataDictionary[key]; if (!temp.controllerType.Contains(EasyWiFiConstants.BACKCHANNEL_FILTER) && temp.logicalPlayerNumber != EasyWiFiConstants.PLAYERNUMBER_DISCONNECTED && (DateTime.UtcNow - temp.lastReceivedPacketTime).TotalSeconds > timeout && (DateTime.UtcNow - temp.lastReceivedPacketTime).TotalSeconds < 150d) { temp.previousConnectionPlayerNumber = temp.logicalPlayerNumber; temp.logicalPlayerNumber = EasyWiFiConstants.PLAYERNUMBER_DISCONNECTED; foundDeadConnection = true; EasyWiFiController.lastConnectedPlayerNumber = temp.previousConnectionPlayerNumber; } } } //if we found a dead connection let the callback know if (foundDeadConnection) { EasyWiFiController.isConnect = false; EasyWiFiController.forceConnectionRefresh(); } }
//for our network traffic on both ends we are essentially having strings (converted in/out for byte[] on the send/recieve //these methods essentially will be one line in the string (remember a client will have more than one controller type) //this method is called when a packet is received on a connection that is marked as disconnected due to a timeout //this can occur in game usecases when the player suspends the app and restarts and expects the controller to still work //because the app wasn't executing having the server sending a message stating hey you timed out is useless //instead because our design is player based simply check to see if another controller has been assigned this player number //if not then just change the player number back and proceed as normal //if another controller has then get the next available player number and then notify the callback of a changed connection public void reuseOrGetAnotherConnection(int previousPlayerNumber) { if (EasyWiFiUtilities.isPlayerNumberOccupied(previousPlayerNumber, clientKey)) { logicalPlayerNumber = EasyWiFiController.getNewPlayerNumber(clientKey); } else { logicalPlayerNumber = previousConnectionPlayerNumber; } lastReceivedPacketTime = DateTime.UtcNow; justReconnected = true; //take this time to reactivate all of the controls from this IP address so callback only get fired once BaseControllerType temp; string[] splitter = { clientKey }; String[] clientIP = serverKey.Split(splitter, StringSplitOptions.RemoveEmptyEntries); if (EasyWiFiController.controllerDataDictionary != null) { foreach (string key in EasyWiFiController.controllerDataDictionary.Keys) { temp = EasyWiFiController.controllerDataDictionary[key]; if (temp.serverKey.Contains(clientIP[0])) { temp.justReconnected = true; temp.lastReceivedPacketTime = DateTime.UtcNow; temp.logicalPlayerNumber = logicalPlayerNumber; } } } //send the callback for a connection EasyWiFiController.isConnect = true; EasyWiFiController.lastConnectedPlayerNumber = logicalPlayerNumber; EasyWiFiController.forceConnectionRefresh(); }