Exemplo n.º 1
0
 public static void Stop()
 {
     try
     {
         commands.stopPullTimer();
     }
     catch (Exception) { }
     try
     {
         TCPLayerLite.shutdownAll();
     }
     catch (Exception) { }
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Input the current password");
            pass = Console.ReadLine();
            Console.WriteLine();

            TCPLayerLite.localDeviceType              = TCPLayerLite.deviceType.GAME;
            TCPLayerLite.DataReceived                += commands.newDataToDecode;
            TCPLayerLite.FailToConnect               += TCPLayerLite_FailToConnect;
            TCPLayerLite.DirectConnectionEstablished += TCPLayerLite_ConnectionEstablished;
            TCPLayerLite.LastConnectionLost          += TCPLayerLite_LastConnectionLost;
            TCPLayerLite.NoConnectedDevice           += TCPLayerLite_NoConnectedDevice;
            TCPLayerLite.setDefaultSecurityOptions(TCPLayerLite.securityMode.PASS_SHA1, Encoding.UTF8.GetBytes("Anonymous"), Encoding.UTF8.GetBytes(pass), false);
            TCPLayerLite.launchConnection(new IPEndPoint(IPAddress.Loopback, port));

            if (!ARE.WaitOne(10000))
            {
                Console.WriteLine("Fail to get answer from server.");
                Thread.Sleep(5000); // <- for seeing previous message, it has nothing to do with the TCPLayerLite
                TCPLayerLite.shutdownAll();
                return;
            }
            if (!connected)
            {
                Console.WriteLine("Connection closed. Bad port or bad password.");
                Thread.Sleep(5000); // <- for seeing previous message, it has nothing to do with the TCPLayerLite
                TCPLayerLite.shutdownAll();
                return;
            }

            Console.WriteLine("Check if sample game is installed.");

            commands.GameListReceived += commands_GameListReceived;
            commands.sendGetGames();
            if (!ARE.WaitOne(1000))
            {
                Console.WriteLine("Fail to get answer from server.");
                Thread.Sleep(5000); // <- for seeing previous message, it has nothing to do with the TCPLayerLite
                TCPLayerLite.shutdownAll();
                return;
            }
            Console.WriteLine();

            Console.WriteLine("Check if sample project is installed.");

            commands.ProjectListReceived += commands_ProjectListReceived;
            commands.sendGetProjectNames(gameName);
            if (!ARE.WaitOne(1000))
            {
                Console.WriteLine("Fail to get answer from server.");
                Thread.Sleep(5000); // <- for seeing previous message, it has nothing to do with the TCPLayerLite
                TCPLayerLite.shutdownAll();
                return;
            }
            Console.WriteLine();

            commands.ConnectedClientAnswerReceived += commands_ConnectedClientAnswerReceived;
            commands.ConnectedClientLoadedProjectAnswerReceived += commands_ConnectedClientLoadedProjectAnswerReceived;

            Console.WriteLine("Choose the server mode and press enter.");
            Console.WriteLine("  1: push mode. The server update this program when it get any input from clients.");
            Console.WriteLine("  2: pull mode. This program ask the server for updates.");
            bool pushMode = false;
            bool pullMode = false;

            while (!pullMode && !pushMode)
            {
                string input = Console.ReadLine();
                if (input == "1")
                {
                    pushMode = true;
                }
                else if (input == "2")
                {
                    pullMode = true;
                }
            }
            Console.WriteLine();

            bool continuousMode = false;
            bool requestMode    = false;

            if (pushMode)
            {
                Console.WriteLine("Setting server mode to push.");
                commands.sendConfigureServer(commands.serverMode.PUSH); // <- send configuration before setting the EMTag list

                Console.WriteLine("Choose display mode and press enter.");
                Console.WriteLine("  1: continuous. A new incoming input fire a event.");
                Console.WriteLine("  2: on request. You must ask the local cache for change with the \"get EMTagName\" (replace the EMTagName with button1, slider1, etc.) command.");

                while (!continuousMode && !requestMode)
                {
                    string input = Console.ReadLine();
                    if (input == "1")
                    {
                        continuousMode = true;
                    }
                    else if (input == "2")
                    {
                        requestMode = true;
                    }
                }
                if (continuousMode)
                {
                    commands.NewCommands += commands_NewCommands;
                }
            }
            else if (pullMode)
            {
                Console.WriteLine("Setting server mode to pull.");
                commands.sendConfigureServer(commands.serverMode.PULL); // <- send configuration before setting the EMTag list

                Console.WriteLine("Choose display mode and press enter.");
                Console.WriteLine("  1: continuous (on internal timer tick). The program ask every X ms the server for changes and update the local cache, and fire a new event if an input changed.");
                Console.WriteLine("  2: on request. You must ask manually the server for update and fire an event if an input changed.");

                while (!continuousMode && !requestMode)
                {
                    string input = Console.ReadLine();
                    if (input == "1")
                    {
                        continuousMode = true;
                    }
                    else if (input == "2")
                    {
                        requestMode = true;
                    }
                }
                if (continuousMode)
                {
                    Console.WriteLine();
                    Console.WriteLine("Choose the timer interval in ms (integer).");
                    string input = Console.ReadLine();
                    int    value = -1;
                    while (value < 0)
                    {
                        if (!int.TryParse(input, NumberStyles.Integer, usCulture, out value))
                        {
                            Console.WriteLine("Error while parsing value.");
                        }
                    }
                    Console.WriteLine("Configuring timer.");
                    commands.setPullTimerParams(value);
                    commands.NewCommands += commands_NewCommands;
                }
                if (requestMode)
                {
                    Console.WriteLine();
                    Console.WriteLine("Type pull to get data from server.");
                    Console.WriteLine("WARNING! You must check the server at least every 30s. Fail to do that may lead to desynch between clients and game.");
                    commands.NewCommands += commands_NewCommands;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Registering the EMTags: button1, toggle1, slider1, POV1, buttonPOV1, buttonPOV2");
            List <commands.EMData> EMDList = new List <commands.EMData>();

            EMDList.Add(new commands.EMData("button1", "False", commands.EMType.BUTTON));
            EMDList.Add(new commands.EMData("toggle1", "False", commands.EMType.BUTTON));
            EMDList.Add(new commands.EMData("slider1", "0.5;0.5", commands.EMType.AXIS));
            EMDList.Add(new commands.EMData("POV1", "-1", commands.EMType.POV));
            EMDList.Add(new commands.EMData("buttonPOV1", "False", commands.EMType.BUTTON));
            EMDList.Add(new commands.EMData("buttonPOV2", "False", commands.EMType.BUTTON));

            EMDList.Add(new commands.EMData("Inventory", "", commands.EMType.INVENTORY));

            commands.registerCommands(EMDList);

            Console.WriteLine();
            Console.WriteLine("Select game mode.");
            Console.WriteLine("  1 for game mode (joystick remap rules apply).");
            Console.WriteLine("  2 for plugin mode (no joystick remap rules apply).");
            bool gameMode   = false;
            bool pluginMode = false;

            while (!gameMode && !pluginMode)
            {
                string input = Console.ReadLine();
                if (input == "1")
                {
                    gameMode = true;
                }
                else if (input == "2")
                {
                    pluginMode = true;
                }
            }
            if (gameMode)
            {
                commands.sendRealJoyRemapApplyRules(projectName, gameName);
            }

            if (pullMode && continuousMode)
            {
                Console.WriteLine("Starting timer.");
                commands.startPullTimer();
            }

            Console.WriteLine("Resynch.");
            commands.sendResynchData();

            Console.WriteLine("Check if project" + projectName + " is loaded on clients.");
            commands.sendConnectedClientsProjects();

            Console.WriteLine();

            Console.WriteLine("Ready.");

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "exit")
                {
                    break;
                }
                else if (input == "pull")
                {
                    commands.sendPullRequest();
                }
                else if (input == "resynch") // for debug purpose only
                {
                    commands.sendResynchData();
                }
                else if (input == "clients")
                {
                    commands.sendConnectedClientsProjects();
                }
                else
                {
                    string[] splitInput = input.Split(space, StringSplitOptions.RemoveEmptyEntries);
                    if (splitInput.Length > 1)
                    {
                        if (splitInput[0] == "get")
                        {
                            string newValue = commands.getValueOfCommand(splitInput[1]);
                            if (newValue == null)
                            {
                                Console.WriteLine("No command with the specified EMTag is registered.");
                            }
                            else
                            {
                                Console.WriteLine(newValue);
                            }
                        }
                        else if (splitInput[0] == "pull")
                        {
                            List <string> EMTags = new List <string>(splitInput.Length - 1);
                            for (int i = 1; i < splitInput.Length; i++)
                            {
                                EMTags.Add(splitInput[i]);
                            }
                            commands.sendPullRequest(EMTags);
                        }
                        else if (splitInput[0] == "set" && splitInput.Length > 2)
                        {
                            commands.EMType?curType = commands.getTypeOfCommand(splitInput[1]);
                            if (curType == null)
                            {
                                Console.WriteLine("No command with the specified EMTag is registered.");
                            }
                            else
                            {
                                commands.EMData EMD = new commands.EMData();
                                EMD.EMTag   = splitInput[1];
                                EMD.EMValue = splitInput[2];
                                EMD.type    = (commands.EMType)curType;
                                commands.sendPushData(new List <commands.EMData> {
                                    EMD
                                });
                            }
                        }
                    }
                }
            }

            commands.stopPullTimer();
            TCPLayerLite.shutdownAll();
        }
Exemplo n.º 3
0
        public static void Start(Settings settings)
        {
            LeaConnection.settings                    = settings;
            TCPLayerLite.localDeviceType              = TCPLayerLite.deviceType.GAME;
            TCPLayerLite.DataReceived                += commands.newDataToDecode;
            TCPLayerLite.FailToConnect               += TCPLayerLite_FailToConnect;
            TCPLayerLite.DirectConnectionEstablished += TCPLayerLite_ConnectionEstablished;
            TCPLayerLite.LastConnectionLost          += TCPLayerLite_LastConnectionLost;
            TCPLayerLite.NoConnectedDevice           += TCPLayerLite_NoConnectedDevice;
            TCPLayerLite.setDefaultSecurityOptions(TCPLayerLite.securityMode.PASS_SHA1, Encoding.UTF8.GetBytes("Anonymous"), Encoding.UTF8.GetBytes(settings.LeaConnection.Password), false);
            TCPLayerLite.launchConnection(new IPEndPoint(IPAddress.Loopback, settings.LeaConnection.Port));

            if (!autoResetEvent.WaitOne(10000))
            {
                Console.WriteLine("Fail to get answer from server.");
                return;
            }
            if (!connected)
            {
                Console.WriteLine("Connection closed. Bad port or bad password.");
                return;
            }

            Console.WriteLine("Checking if game is installed.");

            commands.GameListReceived += commands_GameListReceived;
            commands.sendGetGames();
            if (!autoResetEvent.WaitOne(1000))
            {
                Console.WriteLine("Fail to get answer from server.");
                return;
            }
            Console.WriteLine();

            Console.WriteLine("Checking if project is installed.");

            commands.ProjectListReceived += commands_ProjectListReceived;
            commands.sendGetProjectNames(settings.GameProject.GameName);
            if (!autoResetEvent.WaitOne(1000))
            {
                Console.WriteLine("Fail to get answer from server.");
                return;
            }
            Console.WriteLine();

            commands.ConnectedClientAnswerReceived += commands_ConnectedClientAnswerReceived;
            commands.ConnectedClientLoadedProjectAnswerReceived += commands_ConnectedClientLoadedProjectAnswerReceived;

            commands.sendConfigureServer(commands.serverMode.PUSH); // <- send configuration before setting the EMTag list

            commands.NewCommands += commands_NewCommands;

            foreach (SettingsStatusEvent statusEvent in settings.StatusEvents)
            {
                EMDList.Add(new commands.EMData(statusEvent.EMTag, statusEvent.Default, commands.EMType.BUTTON));
                settingsStatusEvents.Add(statusEvent);
            }
            commands.registerCommands(EMDList);
            commands.sendResynchData();

            commands.sendConnectedClientsProjects();

            Console.WriteLine();

            Console.WriteLine("Connection fully established. Processing ...");
            Console.WriteLine("Press ESC to stop");

            while (Console.ReadKey().Key != ConsoleKey.Escape)
            {
            }
        }