Class: Mongo::SSLSocket

Inherits:
Object show all
Includes:
SocketUtil
Defined in:
lib/mongo/util/ssl_socket.rb

Overview

A basic wrapper over Ruby’s SSLSocket that initiates a TCP connection over SSL and then provides an basic interface mirroring Ruby’s TCPSocket, vis., TCPSocket#send and TCPSocket#read.

Instance Attribute Summary

Attributes included from SocketUtil

#pid, #pool

Instance Method Summary collapse

Methods included from SocketUtil

#checkin, #checkout, #close, #closed?

Constructor Details

#initialize(host, port, op_timeout = nil, connect_timeout = nil) ⇒ SSLSocket

Returns a new instance of SSLSocket.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongo/util/ssl_socket.rb', line 13

def initialize(host, port, op_timeout=nil, connect_timeout=nil)
  @op_timeout = op_timeout
  @connect_timeout = connect_timeout
  @pid = Process.pid

  @tcp_socket = ::TCPSocket.new(host, port)
  @tcp_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

  @socket = OpenSSL::SSL::SSLSocket.new(@tcp_socket)
  @socket.sync_close = true

  connect
end

Instance Method Details

#connectObject



27
28
29
30
31
32
33
34
35
# File 'lib/mongo/util/ssl_socket.rb', line 27

def connect
  if @connect_timeout
    Timeout::timeout(@connect_timeout, ConnectionTimeoutError) do
      @socket.connect
    end
  else
    @socket.connect
  end
end

#read(length, buffer) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/mongo/util/ssl_socket.rb', line 41

def read(length, buffer)
  if @op_timeout
    Timeout::timeout(@op_timeout, OperationTimeout) do
      @socket.sysread(length, buffer)
    end
  else
    @socket.sysread(length, buffer)
  end 
end

#send(data) ⇒ Object



37
38
39
# File 'lib/mongo/util/ssl_socket.rb', line 37

def send(data)
  @socket.syswrite(data)
end