class DEBUGGER__::UI_TcpServer - debug: Ruby Standard Library Documentation

# class DEBUGGER__::UI_TcpServer

### Public Class Methods

new(host: nil, port: nil) click to toggle source
Calls superclass method `DEBUGGER__::UI_ServerBase::new`

    # File debug-1.10.0/lib/debug/server.rb, line 386
    def initialize host: nil, port: nil
      @local_addr = nil
      @host = host || CONFIG[:host]
      @port_save_file = nil
      @port = begin
        port_str = (port && port.to_s) || CONFIG[:port] || raise("Specify listening port by RUBY_DEBUG_PORT environment variable.")
        case port_str
        when /\A\d+\z/
          port_str.to_i
        when /\A(\d+):(.+)\z/
          @port_save_file = $2
          $1.to_i
        else
          raise "Specify digits for port number"
        end
      end
      @port_range = if @port.zero?
        0
      else
        port_range_str = (CONFIG[:port_range] || "0").to_s
        raise "Specify a positive integer <=16 for port range" unless port_range_str.match?(/\A\d+\z/) && port_range_str.to_i <= 16
        port_range_str.to_i
      end
      @uuid = nil # for CDP

      super()
    end

### Public Instance Methods

accept() { |sock_for_fork = sock| ... } click to toggle source
Calls superclass method `DEBUGGER__::UI_ServerBase#accept`

    # File debug-1.10.0/lib/debug/server.rb, line 427
        def accept
          retry_cnt = 0
          super # for fork

          begin
            Socket.tcp_server_sockets @host, @port do |socks|
              @local_addr = socks.first.local_address # Change this part if `socks` are multiple.
              rdbg = File.expand_path('../../exe/rdbg', __dir__)
              DEBUGGER__.warn "Debugger can attach via TCP/IP (#{@local_addr.inspect_sockaddr})"

              if @port_save_file
                File.write(@port_save_file, "#{socks[0].local_address.ip_port.to_s}\n")
                DEBUGGER__.warn "Port is saved into #{@port_save_file}"
              end

              DEBUGGER__.info <<~EOS
              With rdbg, use the following command line:
              #
              #   #{rdbg} --attach #{@local_addr.ip_address} #{@local_addr.ip_port}
              #
              EOS

              case CONFIG[:open]
              when 'chrome'
                chrome_setup
              when 'vscode'
                vscode_setup @local_addr.inspect_sockaddr
              end

              Socket.accept_loop(socks) do |sock, client|
                @client_addr = client
                yield @sock_for_fork = sock
              end
            end
          rescue Errno::EADDRINUSE
            number_of_retries = @port_range.zero? ? 10 : @port_range
            if retry_cnt < number_of_retries
              @port += 1 unless @port_range.zero?
              retry_cnt += 1
              sleep 0.1
              retry
            else
              raise
            end
          rescue Terminate
            # OK
          rescue => e
            $stderr.puts e.inspect, e.message
            pp e.backtrace
            exit
          end
        ensure
          @sock_for_fork = nil

          if @port_save_file && File.exist?(@port_save_file)
            File.unlink(@port_save_file)
          end
        end

chrome_setup() click to toggle source

    # File debug-1.10.0/lib/debug/server.rb, line 414
        def chrome_setup
          require_relative 'server_cdp'

          @uuid = SecureRandom.uuid
          @chrome_pid = UI_CDP.setup_chrome(@local_addr.inspect_sockaddr, @uuid)
          DEBUGGER__.warn <<~EOS
            With Chrome browser, type the following URL in the address-bar:

               devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&noJavaScriptCompletion=true&ws=#{@local_addr.inspect_sockaddr}/#{@uuid}

            EOS
        end