Sha256: d40d58310428c18a36a6d695436f5510bcffa6386ad32494129ed3ef9cbfaf2b

Contents?: true

Size: 1.23 KB

Versions: 47

Compression:

Stored size: 1.23 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) 
      @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

47 entries across 47 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.7.4 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.7.3 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.7.2 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.7.1 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.7.0 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.9 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.8 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.7 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.6 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.5 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.4 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.3 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.2 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.1 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.6.0 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.5.17 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.5.16 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.5.15 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb
classiccms-0.5.14 vendor/bundle/gems/mongo-1.6.2/lib/mongo/util/ssl_socket.rb