Ejemplo n.º 1
0
        //used by the client listening to a server responding to a broadcast message
        public static void listenServerBroadcastResponse(string message)
        {
            if (clientState == EasyWiFiConstants.CURRENT_CLIENT_STATE.Broadcasting)
            {
                string[] splitMessage;

                if (isVerbose)
                {
                    sendClientLog(createClientLogMessage("Recieved Server Broadcast Response... " + message));
                }
                splitMessage    = message.Split(EasyWiFiConstants.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 = EasyWiFiConstants.CURRENT_CLIENT_STATE.ServerFound;
                if (readyToTransmitInventory)
                {
                    //if we aren't ready the wifi manager will send it when ready
                    sendControllerInventory(createInventoryMessage());
                }
            }
        }
Ejemplo n.º 2
0
        //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     = EasyWiFiConstants.CURRENT_CLIENT_STATE.Broadcasting;
            serverIPAddress = String.Empty;


            broadcastThread = new Thread(new ThreadStart(broadcastLoop))
            {
                IsBackground = true
            };
            broadcastThread.Start();
        }
Ejemplo n.º 3
0
        //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(EasyWiFiConstants.SPLITARRAY_COLON, StringSplitOptions.RemoveEmptyEntries);
            splitMessage = splitMessage[1].Split(EasyWiFiConstants.SPLITARRAY_COMMA, StringSplitOptions.RemoveEmptyEntries);

            //map the server keys to the data structure
            for (int i = 0; i < splitMessage.Length; i++)
            {
                splitMessage2 = splitMessage[i].Split(EasyWiFiConstants.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 = EasyWiFiConstants.CURRENT_CLIENT_STATE.SendingControllerData;
        }
Ejemplo n.º 4
0
        //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 != EasyWiFiConstants.CURRENT_CLIENT_STATE.Broadcasting && clientState != EasyWiFiConstants.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 = EasyWiFiConstants.CURRENT_CLIENT_STATE.NotConnected;
            }
        }
Ejemplo n.º 5
0
        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();
                }
            }
        }