class HrrRbSsh::Connection::Channel::ChannelType::ForwardedTcpip

Constants

NAME

Public Class Methods

new(connection, channel, message, socket, logger: nil) click to toggle source
# File lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb, line 15
def initialize connection, channel, message, socket, logger: nil
  self.logger = logger
  @connection = connection
  @channel = channel
  @socket = socket
end

Public Instance Methods

close() click to toggle source
# File lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb, line 27
def close
  begin
    if @sender_thread_finished && @receiver_thread_finished
      log_info { "closing forwarded-tcpip" }
      @socket.close
      log_info { "closing channel IOs" }
      @channel.io.each{ |io| io.close rescue nil }
      log_info { "channel IOs closed" }
      @channel.close from=:channel_type_instance
      log_info { "forwarded-tcpip closed" }
    end
  rescue => e
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
  end
end
receiver_thread() click to toggle source
# File lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb, line 72
def receiver_thread
  Thread.new(@socket){ |s|
    begin
      loop do
        begin
          s.write @channel.io[0].readpartial(10240)
        rescue EOFError
          log_info { "io is EOF" }
          s.close_write
          break
        rescue IOError
          log_info { "socket is closed" }
          break
        rescue => e
          log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
          s.close_write
          break
        end
      end
      log_info { "finishing receiver thread" }
      @receiver_thread_finished = true
      close
    ensure
      log_info { "receiver thread finished" }
    end
  }
end
sender_thread() click to toggle source
# File lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb, line 43
def sender_thread
  Thread.new(@socket){ |s|
    begin
      loop do
        begin
          @channel.io[1].write s.readpartial(10240)
        rescue EOFError
          log_info { "socket is EOF" }
          @channel.io[1].close rescue nil
          break
        rescue IOError
          log_info { "socket is closed" }
          @channel.io[1].close rescue nil
          break
        rescue => e
          log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
          @channel.io[1].close rescue nil
          break
        end
      end
      log_info { "finishing sender thread" }
      @sender_thread_finished = true
      close
    ensure
      log_info { "sender thread finished" }
    end
  }
end
start() click to toggle source
# File lib/hrr_rb_ssh/connection/channel/channel_type/forwarded_tcpip.rb, line 22
def start
  @sender_thread = sender_thread
  @receiver_thread = receiver_thread
end