Sha256: d66fe71c3899a672c3c80fa4da744bd4f544042b4170c0b33c12bf9b55d89468

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

require 'socket'
require 'openssl'
require 'timeout'

module Mongo

  # 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.
  class SSLSocket

    attr_accessor :pool

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

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

      @ssl = OpenSSL::SSL::SSLSocket.new(@socket)
      @ssl.sync_close = true
      
      connect
    end

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

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

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

    def setsockopt(key, value, n)
      @ssl.setsockopt(key, value, n)
    end

    def close
      @ssl.close
    end

    def closed?
      @ssl.closed?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongo-1.7.1 lib/mongo/util/ssl_socket.rb
mongo-1.7.0 lib/mongo/util/ssl_socket.rb
mongo-1.7.0.rc0 lib/mongo/util/ssl_socket.rb
mongo-1.6.4 lib/mongo/util/ssl_socket.rb