// Returns whether the command sent successfully or not.
        //  In Parameters: 1
        //   string settingPath
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            // Validate that parameters is valid.
            if (parameters == null)
            {
                throw new CommandException("DownloadSettingRequestCommand", true, CommandException.Reason.MissingParameter);
            }

            string settingPath = null;

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

            byte[] settingPathBytes = SerializedString.ToNetworkBytes(settingPath);
            byte[] bufferBytes      = new byte[1 + settingPathBytes.Length];

            bufferBytes[0] = (byte)ControlCommand.Command.DownloadSettingRequest;
            Array.Copy(settingPathBytes, 0, bufferBytes, 1, settingPathBytes.Length);

            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            return(true);
        }
Пример #2
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);
        }
Пример #3
0
        private void SendDirectory(DirectoryInfo currentDirectory, Stream ns)
        {
            // Send bytes of Directory name
            byte[] directoryNameBytes = SerializedString.ToNetworkBytes(currentDirectory.Name);
            ns.Write(directoryNameBytes, 0, directoryNameBytes.Length);


            FileInfo[]      files       = currentDirectory.GetFiles();
            DirectoryInfo[] directories = currentDirectory.GetDirectories();

            int filesCount       = files.Length;
            int directoriesCount = directories.Length;

            // Send bytes of filesCount and directoriesCount
            ns.Write(BitConverter.GetBytes(filesCount), 0, 4);
            ns.Write(BitConverter.GetBytes(directoriesCount), 0, 4);

            for (int x = 0; x < filesCount; ++x)
            {
                FileStream fs = files[x].OpenRead();

                // Send bytes of file name
                byte[] fileNameBytes = SerializedString.ToNetworkBytes(files[x].Name);
                ns.Write(fileNameBytes, 0, fileNameBytes.Length);

                // Send bytes of file length (long = Int64)
                ns.Write(BitConverter.GetBytes(fs.Length), 0, 8);

                int    bytesRead = 0;
                byte[] fileBytes = new byte[8192];

                do
                {
                    // Read in 8kB chunks
                    bytesRead = fs.Read(fileBytes, 0, 8192);

                    // Send in 8kB chunks
                    if (bytesRead > 0)
                    {
                        ns.Write(fileBytes, 0, bytesRead);
                    }
                } while (bytesRead != 0);

                fs.Close();
            }

            for (int x = 0; x < directoriesCount; ++x)
            {
                SendDirectory(directories[x], ns);
            }
        }
Пример #4
0
        // Returns whether the command sent successfully or not.
        //  In Parameters: 2
        //      string variableName - Name of updated variable
        //      string variableValue - Value of updated variable
        public override bool SendCommand(Stream ns, Dictionary <string, object> parameters = null)
        {
            if (parameters == null)
            {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.MissingParameter);
            }

            string variableName  = null;
            string variableValue = null;

            try
            {
                variableName  = (string)parameters["variableName"];
                variableValue = (string)parameters["variableValue"];
            } catch (KeyNotFoundException) {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.MissingParameter);
            } catch (InvalidCastException) {
                throw new CommandException("UpdateVariableCommand", true, CommandException.Reason.InvalidParameterType);
            }

            // Don't resend what they just told us if it was on the same stream
            if (ns == lastStream && lastVariableName != null && lastVariableValue != null &&
                lastVariableName == variableName && lastVariableValue == variableValue)
            {
                return(false);
            }

            byte[] nameBytes  = SerializedString.ToNetworkBytes(variableName);
            byte[] valueBytes = SerializedString.ToNetworkBytes(variableValue);

            byte[] bufferBytes = new byte[1 + nameBytes.Length + valueBytes.Length];

            bufferBytes[0] = (byte)ControlCommand.Command.UpdateVariable;
            Array.Copy(nameBytes, 0, bufferBytes, 1, nameBytes.Length);
            Array.Copy(valueBytes, 0, bufferBytes, 1 + nameBytes.Length, valueBytes.Length);

            try
            {
                ns.Write(bufferBytes, 0, bufferBytes.Length);
            } catch (Exception) {
                return(false);
            }

            lastStream        = ns;
            lastVariableName  = variableName;
            lastVariableValue = variableValue;

            return(true);
        }
Пример #5
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);
        }