private void comboBoxSteps_Validating(object sender, System.ComponentModel.CancelEventArgs e)
 {
     try
     {
         int value = int.Parse(textBoxPorts.Text);
         if (value < 0)
         {
             throw new LocalizedException("ValueMustGreaterThanZero");
         }
     }
     catch (Exception ex)
     {
         e.Cancel = true;
         textBoxPorts.Focus();
         LocalizedException.ShowMessage(this, ex);
     }
 }
Exemple #2
0
        Answer SendCommand(StreamWriter writer, StreamReader reader, string command)
        {
            Debug.WriteLine(command, "SEND");
            writer.WriteLine(command);

            string completeLine = string.Empty;

            if (srcpVersion >= Version8 || command.StartsWith("GET"))
            {
                while (true)
                {
                    string line = reader.ReadLine();
                    Debug.WriteLine(line, "RECEIVE");
                    if (line == null)
                    {
                        throw new Exception("Connection broken");
                    }
                    if (!line.EndsWith(@"\"))
                    {
                        completeLine += line;
                        break;
                    }
                    else
                    {
                        completeLine += line.Substring(0, line.Length - 1);
                    }
                }
            }
            else
            {
                if (reader.Peek() != -1)
                {
                    completeLine = reader.ReadToEnd();
                    Debug.WriteLine(completeLine, "RECEIVE");
                }
            }

            try
            {
                return(new Answer(srcpVersion, command, completeLine, true));
            }
            catch (Exception e)
            {
                throw new LocalizedException("CommandFailed", command, LocalizedException.GetFullMessage(e));
            }
        }
Exemple #3
0
        void EnterMode(out TcpClient client, out StreamWriter writer, out StreamReader reader, bool command, bool initVersionAndWelcome)
        {
            writer = null;
            reader = null;
            string welcome = "";

            client = null;
            bool infoChannel7 = command == false && srcpVersion < Version8;

            if (infoChannel7)
            {
                client = new TcpClient(Server, Port + 1);
            }
            else
            {
                try
                {
                    client = new TcpClient(Server, Port);
                }
                catch (SocketException)
                {
                    if (!SrcpServerAutoStart)
                    {
                        throw;
                    }
                    // Autostart srcp Server
                    Form activeForm = Form.ActiveForm;
                    try
                    {
                        srcpServerProcess = Process.Start(SrcpServerPath);
                    }
                    catch (Exception ex)
                    {
                        throw new LocalizedException("StartingSRCPServerFailed", SrcpServerPath, ex.Message);
                    }
                    if (activeForm != null)
                    {
                        activeForm.Activate();
                    }
                    Exception connectionError = null;
                    for (int i = 0; i < 10; i++)
                    {
                        // Give server time to open socket
                        Thread.Sleep(200);
                        try
                        {
                            // try to open
                            client = new TcpClient(Server, Port);
                            break;
                        }
                        catch (Exception ex)
                        {
                            connectionError = ex;
                        }
                    }
                    if (client == null)
                    {
                        StopSrcpServer();
                        throw new LocalizedException("ConnectionToNewlyStartedSRCPServerFailed", LocalizedException.GetFullMessage(connectionError));
                    }
                }
            }
            try
            {
                client.SendTimeout = 2000;
                if (command)
                {
                    client.ReceiveTimeout = 2000;
                }

                reader = new StreamReader(client.GetStream(), Encoding.ASCII);

                if (!infoChannel7)
                {
                    writer = new StreamWriter(client.GetStream(), Encoding.ASCII);


                    writer.AutoFlush = true;
                    try
                    {
                        while (-1 != reader.Peek())
                        {
                            string line = string.Empty;
                            do
                            {
                                line = reader.ReadLine();
                            }while (line == "");


                            welcome += line;

                            // Workaround DDW:
                            Thread.Sleep(500);
                        }
                    }
                    catch (SystemException)
                    {
                        // Ignore
                    }

                    Version versionNumber = new Version(0, 7);
                    if (initVersionAndWelcome)
                    {
                        ArrayList bussesInformations = new ArrayList();
                        this.welcome = welcome;
                        if (welcome.IndexOf(" DDW ") != -1 || welcome.IndexOf(" DDW;") != -1)
                        {
                            DDW = true;
                        }
                        else
                        {
                            DDW = false;
                        }
                        string[] parts = welcome.Split(";".ToCharArray());
                        for (int i = 0; i < parts.Length; i++)
                        {
                            string part          = parts[i].Trim();
                            string partLowerCase = part.ToLower(CultureInfo.InvariantCulture);
                            parts[i] = part;
                            if (partLowerCase.StartsWith("server information:"))
                            {
                                try
                                {
                                    string   serverInformation = part.Substring(19).Trim();
                                    string[] busses            = Regex.Split(serverInformation, @"bus\s*(?<Number>\d+):", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
                                    if (busses.Length >= 2)
                                    {
                                        if (busses[0] == "")
                                        {
                                            for (int iBus = 1; iBus < busses.Length - 1; iBus++)
                                            {
                                                int busNumber = int.Parse(busses[iBus]);
                                                iBus++;
                                                string busDescription = busses[iBus].Trim();
                                                bussesInformations.Add(new BusInformation(busNumber, busDescription));
                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                    // Ignore - no bus information
                                }
                            }
                            if (DDW && partLowerCase.StartsWith("server information:"))
                            {
                                // Workaround DDW 6.7 do not send SRCP version
                                if (versionNumber == new Version(0, 7))
                                {
                                    versionNumber = new Version(0, 8);
                                }
                            }
                            if (partLowerCase.StartsWith("srcp "))
                            {
                                int iBegin = 5;
                                if (iBegin != -1)
                                {
                                    int iEnd = part.IndexOf(';', iBegin);
                                    if (iEnd == -1)
                                    {
                                        iEnd = part.Length;
                                    }
                                    string version = part.Substring(iBegin, iEnd - iBegin);
                                    version.Trim();
                                    versionNumber = new Version(version);
                                }
                            }
                        }
                        this.srcpVersion = versionNumber;
                        if (bussesInformations.Count == 0)
                        {
                            for (int i = 0; i <= 100; i++)
                            {
                                bussesInformations.Add(new BusInformation(i));
                            }
                        }
                        this.busInformations = (BusInformation[])bussesInformations.ToArray(typeof(BusInformation));
                    }
                    if (srcpVersion >= Version8)
                    {
                        if (srcpVersion >= MaxSupportedVersion)
                        {
                            srcpVersion = MaxSupportedVersion;
                        }

                        SendCommand(writer, reader, string.Concat("SET PROTOCOL SRCP ", srcpVersion.ToString(3)));
                        SendCommand(writer, reader, "SET CONNECTIONMODE SRCP " + (command ? "COMMAND" : "INFO"));
                        SendCommand(writer, reader, "GO");
                    }
                }
            }
            catch
            {
                if (writer != null)
                {
                    ((IDisposable)writer).Dispose();
                    writer = null;
                }
                if (reader != null)
                {
                    ((IDisposable)reader).Dispose();
                    reader = null;
                }
                if (client != null)
                {
                    ((IDisposable)client).Dispose();
                    client = null;
                }
                throw;
            }
        }
Exemple #4
0
        public static ServerConnection GetConnection(Control parent, bool start, string connectionName, bool throwExceptionIfCanceled)
        {
            ServerConnection connection = null;

            if (connectionName != null)
            {
                connection = ServerConnections[connectionName];
            }
            while (true)
            {
                if (connection == null)
                {
                    SelectSrcpServer selectServer = new SelectSrcpServer(connectionName, start);
                    selectServer.ShowDialog(parent);
                    connection = selectServer.Connection;
                }
                if (start && connection != null && !connection.Started)
                {
                    try
                    {
                        connection.Start();
                    }
                    catch (Exception ex)
                    {
                        connection = null;
                        MessageBox.Show(parent, LocalizedString.Format("ConnectionToSRCPServerFailed", LocalizedException.GetFullMessage(ex)), null, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        continue;
                    }
                }
                break;
            }
            if (connection == null && throwExceptionIfCanceled)
            {
                throw new LocalizedException("OperationCanceled");
            }
            return(connection);
        }
Exemple #5
0
        bool CheckAnswer(string answer, int forAddress, bool throwError)
        {
            bool newState = false;
            int  address  = -2;

            try
            {
                while (answer.Contains("  "))
                {
                    answer = answer.Replace("  ", " ");
                }

                string[] parts = answer.Split(" ".ToCharArray());
                if (Connection.SrcpVersion >= ServerConnection.Version8)
                {
                    if (parts.Length == 7)
                    {
                        if (parts[2] == "INFO" && parts[4] == "FB" && parts[3] == Bus.ToString(CultureInfo.InvariantCulture))
                        {
                            address = int.Parse(parts[5]);
                            if (forAddress != -1 && forAddress != address && throwError)
                            {
                                throw new ApplicationException("Wrong server address");
                            }
                            int newValue = int.Parse(parts[6]);
                            newState = newValue > 0 ? true : false;
                        }
                    }
                }
                else
                {
                    if (parts.Length == 5)
                    {
                        if (parts[0] == "INFO" && parts[1] == "FB" && parts[2] == type)
                        {
                            address = int.Parse(parts[3]);
                            if (forAddress != -1 && forAddress != address && throwError)
                            {
                                throw new ApplicationException("Wrong server address");
                            }
                            int newValue = int.Parse(parts[4]);
                            newState = newValue > 0 ? true : false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (throwError)
                {
                    throw new LocalizedException("InvalidServerAnswerFormat", LocalizedException.GetFullMessage(e));
                }
                return(false);
            }
            if (address == -2)
            {
                if (throwError)
                {
                    throw new LocalizedException("InvalidServerAnswer");
                }
                return(false);
            }

            object oldState = values[address];

            if (!newState.Equals(oldState))
            {
                OnFeedbackStateChanged(new FeedbackEventArgs(address, newState));
                values[address] = newState;
            }
            return(newState);
        }
Exemple #6
0
        private void buttonOk_Click(object sender, System.EventArgs e)
        {
            Control control = null;

            try
            {
                control = textBoxName;
                string name = textBoxName.Text;
                if (name == "")
                {
                    throw new LocalizedException("EnterAName");
                }

                if (ServerConnection.ServerConnections.Contains(name))
                {
                    if (DialogResult.No == MessageBox.Show(this, LocalizedString.Format("NameAlreadyExist", name), null, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        textBoxName.Focus();
                        return;
                    }
                }
                control = textBoxServer;
                string server = textBoxServer.Text;
                if (server == "")
                {
                    throw new LocalizedException("EnterAServer");
                }

                control = textBoxPort;
                int port;
                try
                {
                    port = int.Parse(textBoxPort.Text);
                    if (port < 1 || port > 65535)
                    {
                        throw new LocalizedException("PortMustBetween1And65535");
                    }
                }
                catch
                {
                    throw new LocalizedException("PortMustBetween1And65535");
                }
                control = textBoxSrcpServerPath;
                string srcpServerPath = string.Empty;
                if (checkBoxAutoStartSRCPServer.Checked)
                {
                    srcpServerPath = textBoxSrcpServerPath.Text;
                    if (srcpServerPath == "")
                    {
                        throw new LocalizedException("EnterLocalPathToSRCPServer");
                    }

                    if (!File.Exists(srcpServerPath))
                    {
                        throw new LocalizedException("SRCPServerDoesNotExist", srcpServerPath);
                    }
                }

                control = null;
                if (Connection != null)
                {
                    ServerConnection.ServerConnections.Remove(Connection.Name);
                }

                Connection = new ServerConnection(name, server, port, checkBoxAutoStartSRCPServer.Checked, checkBoxAutostopSRCPServer.Checked, srcpServerPath, start);

                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                if (control != null)
                {
                    control.Focus();
                }
                LocalizedException.ShowMessage(this, ex);
                return;
            }
        }