// Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 1
        //   List<AvailableOverlaySetting> remoteSettings
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary<string, object> resultData = new Dictionary<string,object>();
            List<AvailableOverlaySetting> remoteSettings = new List<AvailableOverlaySetting>();

            try
            {
                // Read length of the list
                byte[] settingsListSizeBytes = new byte[4];
                StreamHelper.ForceReadAll(ns, settingsListSizeBytes, 0, 4);
                int settingsListSize = BitConverter.ToInt32(settingsListSizeBytes, 0);

                // For each element, read name and path and create an AvailableOverlaySetting for each
                for (int x = 0; x < settingsListSize; ++x)
                {
                    AvailableOverlaySetting aos = new AvailableOverlaySetting();

                    aos.Name = SerializedString.FromNetworkBytes(ns);
                    aos.Path = SerializedString.FromNetworkBytes(ns);
                    aos.Local = false;
                    //aos.IsCurrent = (OverlaySettings.Instance.Location.FullName == Path.Combine(OverlaySettings.OverlaysTempBasePath.FullName, aos.Path));

                    remoteSettings.Add(aos);
                }
            } catch (Exception) {
                return new CommandResult(false);
            }

            resultData["remoteSettings"] = remoteSettings;
            return new CommandResult(true, resultData);
        }
示例#2
0
        // Returns CommandResult (see below) that holds success/failure and implementation specific data.
        //  Return Data: 1
        //   List<AvailableOverlaySetting> remoteSettings
        public override CommandResult HandleCommand(Stream ns)
        {
            Dictionary <string, object>    resultData     = new Dictionary <string, object>();
            List <AvailableOverlaySetting> remoteSettings = new List <AvailableOverlaySetting>();

            try
            {
                // Read length of the list
                byte[] settingsListSizeBytes = new byte[4];
                StreamHelper.ForceReadAll(ns, settingsListSizeBytes, 0, 4);
                int settingsListSize = BitConverter.ToInt32(settingsListSizeBytes, 0);

                // For each element, read name and path and create an AvailableOverlaySetting for each
                for (int x = 0; x < settingsListSize; ++x)
                {
                    AvailableOverlaySetting aos = new AvailableOverlaySetting();

                    aos.Name  = SerializedString.FromNetworkBytes(ns);
                    aos.Path  = SerializedString.FromNetworkBytes(ns);
                    aos.Local = false;
                    //aos.IsCurrent = (OverlaySettings.Instance.Location.FullName == Path.Combine(OverlaySettings.OverlaysTempBasePath.FullName, aos.Path));

                    remoteSettings.Add(aos);
                }
            } catch (Exception) {
                return(new CommandResult(false));
            }


            resultData["remoteSettings"] = remoteSettings;
            return(new CommandResult(true, resultData));
        }
示例#3
0
        private void OverlaySettingSelected(AvailableOverlaySetting selectedSetting)
        {
            // If our selection wasn't local, request the setting and load it
            if (selectedSetting.Local == false)
            {
                Dictionary <string, object> downloadRequestParameters = new Dictionary <string, object>();
                downloadRequestParameters["settingPath"] = selectedSetting.Path;

                // Open when download completes
                remoteSettingToDownload = selectedSetting;
                SettingsDownloaded     += new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);

                DownloadSettingRequestCommand.Instance.SendCommand(EncryptedStream, downloadRequestParameters);
            }
            else
            {
                try
                {
                    OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, selectedSetting.Path)));
                } catch (OverlayLoadingException ole) {
                    MessageBox.Show(ole.Message);
                }
            }

            // Send our selection
            Dictionary <string, object> selectSettingParameters = new Dictionary <string, object>();

            selectSettingParameters["selectedSetting"] = selectedSetting;
            OverlaySettingSelectCommand.Instance.SendCommand(EncryptedStream, selectSettingParameters);
        }
示例#4
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   AvailableOverlaySetting selectedSetting
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.MissingParameter);
            }

            AvailableOverlaySetting selectedSetting = null;

            // Load parameters into local variable
            try
            {
                selectedSetting = (AvailableOverlaySetting)parameters["selectedSetting"];
            } catch (KeyNotFoundException) {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("OverlaySettingSelect", true, CommandException.Reason.InvalidParameterType);
            }


            byte[] serverRemoteByte = BitConverter.GetBytes(selectedSetting.Local);
            byte[] settingPath      = SerializedString.ToNetworkBytes(selectedSetting.Path);

            byte[] bufferBytes = new byte[1 + 1 + settingPath.Length];
            bufferBytes[0] = (byte)ControlCommand.Command.OverlaySettingSelect;
            bufferBytes[1] = serverRemoteByte[0];
            Array.Copy(settingPath, 0, bufferBytes, 2, settingPath.Length);

            ns.Write(bufferBytes, 0, bufferBytes.Length);

            return(true);
        }
示例#5
0
        private void SettingsDownloaded_OpenOnComplete(DirectoryInfo tempDirectory)
        {
            try
            {
                OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysTempBasePath.FullName, remoteSettingToDownload.Path)));
            } catch (OverlayLoadingException ole) {
                MessageBox.Show(ole.Message);
            }

            remoteSettingToDownload = null;

            // Remove self from event
            SettingsDownloaded -= new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);
        }
示例#6
0
        void SelectOverlaySettings_OverlaySettingSelected(AvailableOverlaySetting selectedSetting)
        {
            if (selectedSetting.Local != true)
            {
                throw new ArgumentException();
            }

            try
            {
                OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, selectedSetting.Path)));
            } catch (OverlayLoadingException ole) {
                MessageBox.Show("Overlay Loading Exception: " + ole.Message);
            } catch (Exception ex) {
                MessageBox.Show("Unhandled exception: " + ex.Message);
            }
        }
示例#7
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   List<AvailableOverlaySetting> availableSettings
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.MissingParameter);
            }

            List <AvailableOverlaySetting> availableSettings = null;

            // Load parameters into local variable
            try
            {
                availableSettings = (List <AvailableOverlaySetting>)parameters["availableSettings"];
            } catch (KeyNotFoundException) {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("AvailableSettingsResponseCommand", true, CommandException.Reason.InvalidParameterType);
            }

            // List size
            byte[] settingsListSize = new byte[4];
            settingsListSize = BitConverter.GetBytes(availableSettings.Count);

            // Bytes for each of the availableSettings
            byte[][] settingsList       = new byte[availableSettings.Count][];
            int      totalListBytesSize = 0;

            // Serialize each AvailableOverlaySetting to its bytes
            for (int x = 0; x < availableSettings.Count; ++x)
            {
                AvailableOverlaySetting aos = availableSettings[x];
                byte[] serializedName       = SerializedString.ToNetworkBytes(aos.Name);
                byte[] serializedPath       = SerializedString.ToNetworkBytes(aos.Path);

                settingsList[x] = new byte[serializedName.Length + serializedPath.Length];
                Array.Copy(serializedName, 0, settingsList[x], 0, serializedName.Length);
                Array.Copy(serializedPath, 0, settingsList[x], serializedName.Length, serializedPath.Length);

                // Keep track of the total length of the bytes put together are.
                totalListBytesSize += settingsList[x].Length;
            }

            // Create final buffer
            byte[] bufferBytes = new byte[1 + 4 + totalListBytesSize];

            bufferBytes[0] = (byte)ControlCommand.Command.AvailableSettingsResponse;
            Array.Copy(settingsListSize, 0, bufferBytes, 1, 4);

            // Concatenate all the AvailableOverlaySetting bytes together
            int curOffset = 5;

            foreach (byte[] setting in settingsList)
            {
                Array.Copy(setting, 0, bufferBytes, curOffset, setting.Length);
                curOffset += setting.Length;
            }

            // Write out the bytes to the network socket
            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            return(true);
        }
        private void SettingsDownloaded_OpenOnComplete(DirectoryInfo tempDirectory)
        {
            try
                {
                    OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysTempBasePath.FullName, remoteSettingToDownload.Path)));
                } catch (OverlayLoadingException ole) {
                    MessageBox.Show(ole.Message);
                }

                remoteSettingToDownload = null;

                // Remove self from event
                SettingsDownloaded -= new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);
        }
        private void OverlaySettingSelected(AvailableOverlaySetting selectedSetting)
        {
            // If our selection wasn't local, request the setting and load it
                if (selectedSetting.Local == false)
                {
                    Dictionary<string, object> downloadRequestParameters = new Dictionary<string, object>();
                    downloadRequestParameters["settingPath"] = selectedSetting.Path;

                    // Open when download completes
                    remoteSettingToDownload = selectedSetting;
                    SettingsDownloaded += new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);

                    DownloadSettingRequestCommand.Instance.SendCommand(EncryptedStream, downloadRequestParameters);
                } else {

                    try
                    {
                        OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, selectedSetting.Path)));
                    } catch (OverlayLoadingException ole) {
                        MessageBox.Show(ole.Message);
                    }
                }

                // Send our selection
                Dictionary<string, object> selectSettingParameters = new Dictionary<string, object>();
                selectSettingParameters["selectedSetting"] = selectedSetting;
                OverlaySettingSelectCommand.Instance.SendCommand(EncryptedStream, selectSettingParameters);
        }
        // Returns true if processing was successful
        protected virtual bool ProcessSinglePacket(ControlCommand.Command? requiredCommand = null)
        {
            // Alias EncryptedStream as ns for cleaner code
                Stream ns = EncryptedStream;

                byte[] commandBuffer = new byte[1];
                int commandBytesRead = 0;

                // Try reading the command byte
                try
                {
                    commandBytesRead = ns.Read(commandBuffer, 0, 1);
                } catch (Exception) {
                    return false;
                }

                // If we read a command byte
                if (commandBytesRead == 1)
                {
                    ControlCommand.Command packetCommand = (ControlCommand.Command)commandBuffer[0];
                    CommandResult cr = null;

                    // Handle incoming commands
                    switch (packetCommand)
                    {
                        case ControlCommand.Command.AvailableSettingsRequest:
                            cr = AvailableSettingsRequestCommand.Instance.HandleCommand(ns);

                            Dictionary<string, object> availableResponseParameters = new Dictionary<string, object>();
                            availableResponseParameters["availableSettings"] = OverlaySettings.GetValidOverlaySettings();
                            bool responseSent = AvailableSettingsResponseCommand.Instance.SendCommand(ns, availableResponseParameters);

                            break;

                        case ControlCommand.Command.AvailableSettingsResponse:
                            cr = AvailableSettingsResponseCommand.Instance.HandleCommand(ns);

                            if (cr.Success)
                                AvailableRemoteSettings = (List<AvailableOverlaySetting>) cr.Data["remoteSettings"];

                            break;

                        case ControlCommand.Command.DownloadSettingRequest:
                            cr = DownloadSettingRequestCommand.Instance.HandleCommand(ns);

                            if (cr.Success)
                            {
                                string settingPath = (string)cr.Data["settingPath"];

                                FileInfo overlaySettingFile = new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, settingPath));
                                DirectoryInfo overlaySettingFolder = overlaySettingFile.Directory;

                                Dictionary<string, object> downloadResponseParameters = new Dictionary<string, object>();
                                downloadResponseParameters["fromDirectory"] = overlaySettingFolder;

                                DownloadSettingResponseCommand.Instance.SendCommand(ns, downloadResponseParameters);
                            }

                            break;

                        case ControlCommand.Command.DownloadSettingResponse:
                            cr = DownloadSettingResponseCommand.Instance.HandleCommand(ns);

                            if (cr.Success)
                            {
                                RaiseSettingsDownloaded((DirectoryInfo)cr.Data["savedDirectory"]);
                            }

                            break;

                        case ControlCommand.Command.OverlaySettingSelect:
                            cr = OverlaySettingSelectCommand.Instance.HandleCommand(ns);

                            if (cr.Success)
                            {
                                if ((bool) cr.Data["remote"])
                                {
                                    Dictionary<string, object> downloadParameters = new Dictionary<string, object>();
                                    downloadParameters["settingPath"] = cr.Data["selectedPath"];

                                    // Open when download completes
                                    SettingsDownloaded += new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);
                                    remoteSettingToDownload = new AvailableOverlaySetting() { Path = (string)cr.Data["selectedPath"], Local = !((bool)cr.Data["remote"]) };
                                    DownloadSettingRequestCommand.Instance.SendCommand(ns, downloadParameters);

                                } else {
                                    try
                                    {
                                        OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, (string)cr.Data["selectedPath"])));
                                    } catch (OverlayLoadingException ole) {
                                        MessageBox.Show(ole.Message);
                                        return false;
                                    }
                                    OverlaySettingLoadedCommand.Instance.SendCommand(ns);
                                }

                            }

                            break;

                        case ControlCommand.Command.OverlaySettingLoaded:
                            cr = OverlaySettingLoadedCommand.Instance.HandleCommand(ns);
                            break;

                        case ControlCommand.Command.UpdateVariable:

                            cr = UpdateVariableCommand.Instance.HandleCommand(ns);
                            if (cr.Success)
                            {
                                string variableName = (string)cr.Data["variableName"];
                                string variableValue = (string)cr.Data["variableValue"];

                                // Update our dictionary's value of variableName with variableValue
                                OverlaySettings.Instance.UpdateVariableFromNetwork(variableName, variableValue);
                            }

                            break;

                        case ControlCommand.Command.Close:

                            cr = CloseCommand.Instance.HandleCommand(ns);
                            if (cr.Success)
                                return false;

                            break;

                        // Unknown command
                        default:
                            return false;
                    }

                    if (!cr.Success)
                        return false;

                    // Mark packet processed if mutex is valid
                    if (ProcessedCommands.ContainsKey(packetCommand) && ProcessedCommands[packetCommand] != null)
                        ProcessedCommands[packetCommand].Set();

                    // If we processed a command that wasn't required, fail
                    if (requiredCommand.HasValue && ((ControlCommand.Command)commandBuffer[0]) != requiredCommand.Value)
                        return false;
                }

                return true;
        }
示例#11
0
        // Returns true if processing was successful
        protected virtual bool ProcessSinglePacket(ControlCommand.Command?requiredCommand = null)
        {
            // Alias EncryptedStream as ns for cleaner code
            Stream ns = EncryptedStream;

            byte[] commandBuffer    = new byte[1];
            int    commandBytesRead = 0;

            // Try reading the command byte
            try
            {
                commandBytesRead = ns.Read(commandBuffer, 0, 1);
            } catch (Exception) {
                return(false);
            }

            // If we read a command byte
            if (commandBytesRead == 1)
            {
                ControlCommand.Command packetCommand = (ControlCommand.Command)commandBuffer[0];
                CommandResult          cr            = null;

                // Handle incoming commands
                switch (packetCommand)
                {
                case ControlCommand.Command.AvailableSettingsRequest:
                    cr = AvailableSettingsRequestCommand.Instance.HandleCommand(ns);

                    Dictionary <string, object> availableResponseParameters = new Dictionary <string, object>();
                    availableResponseParameters["availableSettings"] = OverlaySettings.GetValidOverlaySettings();
                    bool responseSent = AvailableSettingsResponseCommand.Instance.SendCommand(ns, availableResponseParameters);

                    break;

                case ControlCommand.Command.AvailableSettingsResponse:
                    cr = AvailableSettingsResponseCommand.Instance.HandleCommand(ns);

                    if (cr.Success)
                    {
                        AvailableRemoteSettings = (List <AvailableOverlaySetting>)cr.Data["remoteSettings"];
                    }

                    break;

                case ControlCommand.Command.DownloadSettingRequest:
                    cr = DownloadSettingRequestCommand.Instance.HandleCommand(ns);

                    if (cr.Success)
                    {
                        string settingPath = (string)cr.Data["settingPath"];

                        FileInfo      overlaySettingFile   = new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, settingPath));
                        DirectoryInfo overlaySettingFolder = overlaySettingFile.Directory;

                        Dictionary <string, object> downloadResponseParameters = new Dictionary <string, object>();
                        downloadResponseParameters["fromDirectory"] = overlaySettingFolder;

                        DownloadSettingResponseCommand.Instance.SendCommand(ns, downloadResponseParameters);
                    }

                    break;

                case ControlCommand.Command.DownloadSettingResponse:
                    cr = DownloadSettingResponseCommand.Instance.HandleCommand(ns);

                    if (cr.Success)
                    {
                        RaiseSettingsDownloaded((DirectoryInfo)cr.Data["savedDirectory"]);
                    }

                    break;


                case ControlCommand.Command.OverlaySettingSelect:
                    cr = OverlaySettingSelectCommand.Instance.HandleCommand(ns);

                    if (cr.Success)
                    {
                        if ((bool)cr.Data["remote"])
                        {
                            Dictionary <string, object> downloadParameters = new Dictionary <string, object>();
                            downloadParameters["settingPath"] = cr.Data["selectedPath"];

                            // Open when download completes
                            SettingsDownloaded     += new SettingsDownloadedHandler(SettingsDownloaded_OpenOnComplete);
                            remoteSettingToDownload = new AvailableOverlaySetting()
                            {
                                Path = (string)cr.Data["selectedPath"], Local = !((bool)cr.Data["remote"])
                            };
                            DownloadSettingRequestCommand.Instance.SendCommand(ns, downloadParameters);
                        }
                        else
                        {
                            try
                            {
                                OverlaySettings.Instance.Load(new FileInfo(Path.Combine(OverlaySettings.OverlaysBasePath.FullName, (string)cr.Data["selectedPath"])));
                            } catch (OverlayLoadingException ole) {
                                MessageBox.Show(ole.Message);
                                return(false);
                            }
                            OverlaySettingLoadedCommand.Instance.SendCommand(ns);
                        }
                    }

                    break;

                case ControlCommand.Command.OverlaySettingLoaded:
                    cr = OverlaySettingLoadedCommand.Instance.HandleCommand(ns);
                    break;

                case ControlCommand.Command.UpdateVariable:

                    cr = UpdateVariableCommand.Instance.HandleCommand(ns);
                    if (cr.Success)
                    {
                        string variableName  = (string)cr.Data["variableName"];
                        string variableValue = (string)cr.Data["variableValue"];

                        // Update our dictionary's value of variableName with variableValue
                        OverlaySettings.Instance.UpdateVariableFromNetwork(variableName, variableValue);
                    }

                    break;

                case ControlCommand.Command.Close:

                    cr = CloseCommand.Instance.HandleCommand(ns);
                    if (cr.Success)
                    {
                        return(false);
                    }

                    break;

                // Unknown command
                default:
                    return(false);
                }

                if (!cr.Success)
                {
                    return(false);
                }

                // Mark packet processed if mutex is valid
                if (ProcessedCommands.ContainsKey(packetCommand) && ProcessedCommands[packetCommand] != null)
                {
                    ProcessedCommands[packetCommand].Set();
                }

                // If we processed a command that wasn't required, fail
                if (requiredCommand.HasValue && ((ControlCommand.Command)commandBuffer[0]) != requiredCommand.Value)
                {
                    return(false);
                }
            }

            return(true);
        }