示例#1
0
        public virtual void disconnect()
        {
            //Console.Error.WriteLine(this+":disconnect "+io+" "+connected);
            //Thread.dumpStack();

            lock (this)
            {
                if (!connected)
                {
                    return;
                }
                connected = false;
            }

            try
            {
                Close();

                eof_remote = eof_local = true;

                thread = null;

                try
                {
                    if (io != null)
                    {
                        io.Close();
                    }
                }
                catch //(Exception e)
                {
                    //e.printStackTrace();
                }
                // io=null;
            }
            finally
            {
                Channel.del(this);
            }
        }
        public override void connect()
        {
            try
            {
                Session _session = getSession();
                if (!_session.Connected)
                {
                    throw new JSchException("session is down");
                }
                Buffer buf    = new Buffer(150);
                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("direct-tcpip".getBytes());
                buf.putInt(id);
                buf.putInt(lwsize);
                buf.putInt(lmpsize);
                buf.putString(host.getBytes());
                buf.putInt(port);
                buf.putString(originator_IP_address.getBytes());
                buf.putInt(originator_port);
                _session.write(packet);

                int retry = 1000;
                try
                {
                    while (this.getRecipient() == -1 &&
                           _session.Connected &&
                           retry > 0 &&
                           !eof_remote)
                    {
                        //Thread.Sleep(500);
                        Thread.Sleep(50);
                        retry--;
                    }
                }
                catch // (Exception ee)
                {
                }
                if (!_session.Connected)
                {
                    throw new JSchException("session is down");
                }
                if (retry == 0 || this.eof_remote)
                {
                    throw new JSchException("channel is not opened.");
                }

                /*
                 * if(this.eof_remote){      // failed to open
                 * disconnect();
                 * return;
                 * }
                 */

                connected = true;

                if (io.In != null)
                {
                    thread      = new Thread(this.run);
                    thread.Name = "DirectTCPIP thread " + _session.getHost();
                    if (_session.daemon_thread)
                    {
                        thread.IsBackground = _session.daemon_thread;
                    }
                    thread.Start();
                }
            }
            catch (Exception e)
            {
                io.Close();
                io = null;
                Channel.del(this);
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
            }
        }