//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; } } } }
//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); }
//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") { if (controllerDataDictionary == null) { return(""); } 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; } //on the client we care what both the client and server key is but only client is known at registration time //the server key is discovered when the server responds to the inventory and will be populated then controller.clientKey = name; key = controller.clientKey; if (!controllerDataDictionary.ContainsKey(key)) { //brand new first time here controllerDataDictionary.Add(key, controller); controllerDataDictionaryKeys.Add(key); } 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; } } return(key); }