Пример #1
0
        protected void write(Packet packet)
        {
            if (reply)
            {
                channel.reply = -1;
            }
            session.write(packet);
            if (reply)
            {
                long start   = JavaCompat.CurrentTimeMillis();
                long timeout = channel.connectTimeout;
                while (channel.Connected && channel.reply == -1)
                {
                    try { Thread.Sleep(10); }
                    catch //(Exception ee)
                    {
                    }
                    if (timeout > 0L &&
                        (JavaCompat.CurrentTimeMillis() - start) > timeout)
                    {
                        channel.reply = 0;
                        throw new JSchException("channel request: timeout");
                    }
                }

                if (channel.reply == 0)
                {
                    throw new JSchException("failed to send channel request");
                }
            }
        }
Пример #2
0
        public void connect(int connectTimeout)
        {
            Session _session = getSession();

            if (!_session.Connected)
            {
                throw new JSchException("session is down");
            }
            this.connectTimeout = connectTimeout;
            try
            {
                Buffer buf    = new Buffer(100);
                Packet packet = new Packet(buf);
                // send
                // byte   SSH_MSG_CHANNEL_OPEN(90)
                // string channel type         //
                // uint32 sender channel       // 0
                // uint32 initial window size  // 0x100000(65536)
                // uint32 maxmum packet size   // 0x4000(16384)
                packet.reset();
                buf.putByte((byte)90);
                buf.putString(this.type);
                buf.putInt(this.id);
                buf.putInt(this.lwsize);
                buf.putInt(this.lmpsize);
                _session.write(packet);
                int  retry   = 1000;
                long start   = JavaCompat.CurrentTimeMillis();
                long timeout = connectTimeout;
                while (this.getRecipient() == -1 &&
                       _session.Connected &&
                       retry > 0)
                {
                    if (timeout > 0L)
                    {
                        if ((JavaCompat.CurrentTimeMillis() - start) > timeout)
                        {
                            retry = 0;
                            continue;
                        }
                    }
                    try { Thread.Sleep(50); }
                    catch  { }
                    retry--;
                }
                if (!_session.Connected)
                {
                    throw new JSchException("session is down");
                }
                if (retry == 0)
                {
                    throw new JSchException("channel is not opened.");
                }

                /*
                 * At the failure in opening the channel on the sshd,
                 * 'SSH_MSG_CHANNEL_OPEN_FAILURE' will be sent from sshd and it will
                 * be processed in Session#run().
                 */
                if (this.isClosed())
                {
                    throw new JSchException("channel is not opened.");
                }
                connected = true;
                Start();
            }
            catch (Exception e)
            {
                connected = false;
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                throw new JSchException(e.ToString(), e);
            }
        }