Sha256: 25f7bfb12054bffe740ca96e500f11d6020c9659a8a5c3a810062d2a01e77f95

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Sip2
  #
  # Sip2 Client
  #
  class Client
    def initialize(host:, port:, ignore_error_detection: false, timeout: nil, ssl_context: nil)
      @host = host
      @port = port
      @ignore_error_detection = ignore_error_detection
      @timeout = timeout || NonBlockingSocket::DEFAULT_TIMEOUT
      @ssl_context = ssl_context
    end

    # rubocop:disable Metrics/MethodLength
    def connect
      socket = NonBlockingSocket.connect host: @host, port: @port, timeout: @timeout

      # If we've been provided with an SSL context then use it to wrap out existing connection
      if @ssl_context
        socket = ::OpenSSL::SSL::SSLSocket.new socket, @ssl_context
        socket.hostname = @host # Needed for SNI
        socket.sync_close = true
        socket.connect
        socket.post_connection_check @host # Validate the peer certificate matches the host
      end

      if block_given?
        yield Connection.new(socket: socket, ignore_error_detection: @ignore_error_detection)
      end
    ensure
      socket&.close
    end
    # rubocop:enable Metrics/MethodLength
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sip2-0.2.1 lib/sip2/client.rb