Class: Mongo::Socket::SSL

Inherits:
Mongo::Socket show all
Includes:
OpenSSL
Defined in:
lib/mongo/socket/ssl.rb

Overview

Wrapper for SSL sockets.

Since:

  • 2.0.0

Constant Summary

Constants inherited from Mongo::Socket

SSL_ERROR, TIMEOUT_ERROR, TIMEOUT_PACK, WRITE_CHUNK_SIZE

Instance Attribute Summary collapse

Attributes inherited from Mongo::Socket

#family, #options, #socket, #timeout

Instance Method Summary collapse

Methods inherited from Mongo::Socket

#alive?, #close, #connectable?, #eof?, #gets, #read, #write

Constructor Details

#initialize(host, port, host_name, timeout, family, options = {}) ⇒ SSL

Initializes a new SSL socket.

Examples:

Create the SSL socket.

SSL.new('::1', 27017, 30)

Parameters:

  • host (String)

    The hostname or IP address.

  • port (Integer)

    The port number.

  • timeout (Float)

    The socket timeout value.

  • family (Integer)

    The socket family.

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

Since:

  • 2.0.0



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mongo/socket/ssl.rb', line 84

def initialize(host, port, host_name, timeout, family, options = {})
  @host, @port, @host_name, @timeout, @options = host, port, host_name, timeout, options
  @context = create_context(options)
  @family = family
  @tcp_socket = ::Socket.new(family, SOCK_STREAM, 0)
  begin
    @tcp_socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
    set_socket_options(@tcp_socket)
    connect!
  rescue
    @tcp_socket.close
    raise
  end
end

Instance Attribute Details

#contextSSLContext (readonly)

Returns context The ssl context.

Returns:

  • (SSLContext)

    context The ssl context.

Since:

  • 2.0.0



25
26
27
# File 'lib/mongo/socket/ssl.rb', line 25

def context
  @context
end

#hostString (readonly)

Returns host The host to connect to.

Returns:

  • (String)

    host The host to connect to.

Since:

  • 2.0.0



28
29
30
# File 'lib/mongo/socket/ssl.rb', line 28

def host
  @host
end

#host_nameString (readonly)

Returns host_name The original host name.

Returns:

  • (String)

    host_name The original host name.

Since:

  • 2.0.0



31
32
33
# File 'lib/mongo/socket/ssl.rb', line 31

def host_name
  @host_name
end

#portInteger (readonly)

Returns port The port to connect to.

Returns:

  • (Integer)

    port The port to connect to.

Since:

  • 2.0.0



34
35
36
# File 'lib/mongo/socket/ssl.rb', line 34

def port
  @port
end

Instance Method Details

#readbyteObject

Read a single byte from the socket.

Examples:

Read a single byte.

socket.readbyte

Returns:

  • (Object)

    The read byte.

Since:

  • 2.0.0



107
108
109
110
111
112
# File 'lib/mongo/socket/ssl.rb', line 107

def readbyte
  handle_errors do
    byte = socket.read(1).bytes.to_a[0]
    byte.nil? ? raise(EOFError) : byte
  end
end