Esempio n. 1
0
        // --------------------------------- Login ------------------------------
        public static string[] Login(
            TelnetConnection InConn, RunLogListBox InRunLog,
            string InUserName, string InPass, int InLoginTimeOutMs)
        {
            string s = null;

            int cx = 0;

            while (true)
            {
                InRunLog.Write("Read login prompt ...");
                s = InConn.Read(InLoginTimeOutMs);
                if (s.Length > 0)
                {
                    break;
                }
                cx += 1;
                if (cx > 5)
                {
                    throw new Exception("Did not receive any login prompt text from telnet server");
                }
            }

            if (!s.TrimEnd().EndsWith(":"))
            {
                throw new Exception("Failed to connect : no login prompt");
            }
            InRunLog.Write("Username " + InUserName);
            InConn.WriteLine(InUserName);

            s += InConn.Read(InLoginTimeOutMs);
            if (!s.TrimEnd().EndsWith(":"))
            {
                throw new Exception("Failed to connect : no password prompt");
            }
            InRunLog.Write("Password " + InPass);
            InConn.WriteLine(InPass);

            s += InConn.Read(InLoginTimeOutMs);

            string[] lines = SplitReadText(s);

            return(lines);
        }
        // The ReceiveThread is relatively simple.
        // It continuously reads from the socket connected to the telnet server. Whatever
        // is received is appended to a Read buffer and an event is signaled to alert
        // CommandThreads to come and get the data.  The CommandThread which gets the data
        // then empties the ReadBuffer and waits again on the "DataArrived" event to be
        // signaled by this thread.
        public void EntryPoint()
        {
            try
            {
                mRunLog.Write("Start reading from telnet server");

                while (mShutdownFlag.State == false)
                {
                    if (mConn.IsConnected == false)
                    {
                        break;
                    }

                    string s1 = mConn.Read();

                    // append what was just read to the ReadBuffer.
                    lock (mSupervisor.LockFlag)
                    {
                        if (mSupervisor.CurrentReadBuffer != null)
                        {
                            mSupervisor.CurrentReadBuffer.Buffer.Append(s1);
                        }

                        // Signal command threads that there is read data available. ( The command
                        // thread then resets the event after it checks/removes from the data buffer. )
                        mSupervisor.CurrentReadBuffer.GotDataEvent.Set();
                    }
                }

                mRunLog.Write("Exit.");
            }
            finally
            {
                // in case anyone waiting for this thread to end. Signal the ended event.
                ThreadEndedEvent.Set();
            }
        }
        public void ShutdownThreads()
        {
            foreach (CommandThread ct in mCommandThreadList)
            {
                ct.InitiateThreadShutdown();

                // wait for the command thread to end.
                ct.ThreadEndedEvent.WaitOne();
            }

            // instruct the receive thread to shutdown.
            ReceiveThread rrt = RunningReceiveThread;

            if ((rrt != null) && (rrt.IsRunning == true))
            {
                rrt.InitiateThreadShutdown();
                mRunLog.Write("Wait for ReceiveThread to end ...");
                rrt.ThreadEndedEvent.WaitOne();
            }
        }