//used by the client listening to a server responding to a broadcast message public static void listenServerBroadcastResponse(string message) { if (clientState == CURRENT_CLIENT_STATE.Broadcasting) { string[] splitMessage; if (isVerbose) { sendClientLog(createClientLogMessage("Recieved Server Broadcast Response... " + message)); } splitMessage = message.Split(WiFiInputConstants.SPLITARRAY_COLON, StringSplitOptions.RemoveEmptyEntries); serverIPAddress = splitMessage[1]; //we've received the server response so add endpoint to send directly to server clientSend = new UdpClient() { EnableBroadcast = true }; clientSendEndPoint = new IPEndPoint(IPAddress.Parse(serverIPAddress), serverSocketListenPort); clientState = CURRENT_CLIENT_STATE.ServerFound; if (readyToTransmitInventory) { //if we aren't ready the wifi manager will send it when ready sendControllerInventory(createInventoryMessage()); } } }
//this method is called upon init of the client automatically (if set to) to look for the server //this can also be invoked in gamecode to essentially relook for server (if we want to reconnect or find a new one) public static void checkForServer() { //reset the connection variables clientState = CURRENT_CLIENT_STATE.Broadcasting; serverIPAddress = String.Empty; broadcastThread = new Thread(new ThreadStart(broadcastLoop)) { IsBackground = true }; broadcastThread.Start(); }
public static void Run(string name, int serverPort, int clientPort, bool verbose, bool clientConnectAutomatically) { appName = name; serverSocketListenPort = serverPort; clientScoketListenPort = clientPort; isVerbose = verbose; myIPAddress = "127.0.0.1"; if (isVerbose) { Debug.Log("Initializing Easy WiFi Controller..."); } //if we're initing with connect automatically this will be changed below when checking for server clientState = 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(); } }
//used by the client listening to a server responding to a inventory message public static void listenServerInventoryResponse(string message) { string[] splitMessage, splitMessage2; if (isVerbose) { sendClientLog(createClientLogMessage("Received Server's Inventory Response... " + message)); } splitMessage = message.Split(WiFiInputConstants.SPLITARRAY_COLON, StringSplitOptions.RemoveEmptyEntries); splitMessage = splitMessage[1].Split(WiFiInputConstants.SPLITARRAY_COMMA, StringSplitOptions.RemoveEmptyEntries); //map the server keys to the data structure for (int i = 0; i < splitMessage.Length; i++) { splitMessage2 = splitMessage[i].Split(WiFiInputConstants.SPLITARRAY_POUND, StringSplitOptions.RemoveEmptyEntries); controllerDataDictionary[splitMessage2[1]].serverKey = splitMessage2[0]; } //we now have the server keys so set the flag to start the controller data stream clientState = CURRENT_CLIENT_STATE.SendingControllerData; }
//used by the client to tell the server we are disconnecting public static void sendDisconnect(string message) { if (isVerbose) { sendClientLog(createClientLogMessage("Sending Disconnect message... " + message)); } byte[] bytes = Encoding.UTF8.GetBytes(message); if (clientState != CURRENT_CLIENT_STATE.Broadcasting && clientState != CURRENT_CLIENT_STATE.NotConnected) { try { //if we're broadcasting we don't yet know the server clientSend.Send(bytes, (int)bytes.Length, clientSendEndPoint); } catch (Exception ex) { //the most common exception here is if you forgot to turn on your wifi (network unreachable) //log the error and move on (try again later) Debug.Log(ex.Message); } clientState = CURRENT_CLIENT_STATE.NotConnected; } }