private void CheckForClientTimeout() { //check for any clients that may have timed out (if we just had a reconnection skip this time around if (!(WiFiInputController.isConnect == true && (DateTime.UtcNow - WiFiInputController.lastCallbackTime).TotalSeconds < clientTimeout && (DateTime.UtcNow - WiFiInputController.lastCallbackTime).TotalSeconds > 0d)) { WiFiInputUtilities.CheckForClientTimeout(clientTimeout); } }
//this function is responsible for looping through all the keys and giving out the NEW player number //remember this instance has not been added yet public static int getNewPlayerNumber(string controlName) { int player = WiFiInputUtilities.getHighestPlayerNumber() + 1; int returnIndex = 0; bool foundControl = false; for (int i = 0; i <= player; i++) { foundControl = WiFiInputUtilities.isPlayerNumberOccupied(i, controlName); if (foundControl == false) { //we found no control with this player number (lowest yet so return it) returnIndex = i; break; } } return(returnIndex); }
//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 (WiFiInputUtilities.isPlayerNumberOccupied(previousPlayerNumber, clientKey)) { logicalPlayerNumber = WiFiInputController.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 (WiFiInputController.controllerDataDictionary != null) { foreach (string key in WiFiInputController.controllerDataDictionary.Keys) { temp = WiFiInputController.controllerDataDictionary[key]; if (temp.serverKey.Contains(clientIP[0])) { temp.justReconnected = true; temp.lastReceivedPacketTime = DateTime.UtcNow; temp.logicalPlayerNumber = logicalPlayerNumber; } } } //send the callback for a connection WiFiInputController.isConnect = true; WiFiInputController.lastConnectedPlayerNumber = logicalPlayerNumber; WiFiInputController.forceConnectionRefresh(); }
// WiFiServerController's interface public override void OnConnectionsChanged() { m_Controller = WiFiInputUtilities.checkForClient <ButtonControllerType>(controlName, (int)playerNumber); }
// LOGIC public void Initialize() { m_Controller = WiFiInputUtilities.checkForClient <ButtonControllerType>(controlName, (int)playerNumber); }
//this method is responsible for registering a control which will instance a specific controller type //add it to the data structure and set its key and return it //There are two ways this method is called, one on the client the UI script in its startup registers it client side //The second way is the server when a client sends over its inventory public static string registerControl(string type, string name, string IP = "0") { BaseControllerType controller; string key = ""; switch (type) { case WiFiInputConstants.CONTROLLERTYPE_AXIS: controller = new AxisControllerType(); controller.controllerType = WiFiInputConstants.CONTROLLERTYPE_AXIS; break; case WiFiInputConstants.CONTROLLERTYPE_BUTTON: controller = new ButtonControllerType(); controller.controllerType = WiFiInputConstants.CONTROLLERTYPE_BUTTON; break; default: //should not occur controller = null; Debug.Log("Error: a controller type that isn't defined was registered"); break; } controller.serverKey = IP + name; controller.clientKey = name; controller.clientIP = IP; controller.logicalPlayerNumber = getNewPlayerNumber(name); controller.lastReceivedPacketTime = DateTime.UtcNow; lastConnectedPlayerNumber = controller.logicalPlayerNumber; isConnect = true; key = controller.serverKey; if (!controllerDataDictionary.ContainsKey(key)) { //brand new first time here controllerDataDictionary.Add(key, controller); controllerDataDictionaryKeys.Add(key); isNew = true; } else if (controllerDataDictionary[key].logicalPlayerNumber == WiFiInputConstants.PLAYERNUMBER_DISCONNECTED) { //already here just reassign (original disconnected and new one is here) //we will only change the player number if it's been occupied in between controllerDataDictionary[key].lastReceivedPacketTime = DateTime.UtcNow; controllerDataDictionary[key].justReconnected = true; if (!WiFiInputUtilities.isPlayerNumberOccupied(controllerDataDictionary[key].previousConnectionPlayerNumber, controllerDataDictionary[key].clientKey)) { //not occupied assign it's previous number controllerDataDictionary[key].logicalPlayerNumber = controllerDataDictionary[key].previousConnectionPlayerNumber; } else { //occupied give it the lowest available number controllerDataDictionary[key].logicalPlayerNumber = controller.logicalPlayerNumber; } isNew = true; } else { //was already here and isn't currently marked as disconnected (most likely a delayed packet) isNew = false; } if (isNew) { if (OnControllerRegisteredEvent != null) { OnControllerRegisteredEvent(key); } } return(key); }