Sha256: f374135735aaa7d91b67ea7364de34cc1162f6388306b55b19faa574959b95c5

Contents?: true

Size: 1.6 KB

Versions: 61

Compression:

Stored size: 1.6 KB

Contents

require "socket"

module Bunny
  begin
    require "openssl"

    # TLS-enabled TCP socket that implements convenience
    # methods found in Bunny::Socket.
    class SSLSocket < OpenSSL::SSL::SSLSocket

      # IO::WaitReadable is 1.9+ only
      READ_RETRY_EXCEPTION_CLASSES = [Errno::EAGAIN, Errno::EWOULDBLOCK]
      READ_RETRY_EXCEPTION_CLASSES << IO::WaitReadable if IO.const_defined?(:WaitReadable)


      # Reads given number of bytes with an optional timeout
      #
      # @param [Integer] count How many bytes to read
      # @param [Integer] timeout Timeout
      #
      # @return [String] Data read from the socket
      # @api public
      def read_fully(count, timeout = nil)
        return nil if @__bunny_socket_eof_flag__

        value = ''
        begin
          loop do
            value << read_nonblock(count - value.bytesize)
            break if value.bytesize >= count
          end
        rescue EOFError => e
          @__bunny_socket_eof_flag__ = true
        rescue OpenSSL::SSL::SSLError => e
          if e.message == "read would block"
            if IO.select([self], nil, nil, timeout)
              retry
            else
              raise Timeout::Error, "IO timeout when reading #{count} bytes"
            end
          else
            raise e
          end
        rescue *READ_RETRY_EXCEPTION_CLASSES => e
          if IO.select([self], nil, nil, timeout)
            retry
          else
            raise Timeout::Error, "IO timeout when reading #{count} bytes"
          end
        end
        value
      end
    end
  rescue LoadError => le
    puts "Could not load OpenSSL"
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
bunny-1.5.1 lib/bunny/cruby/ssl_socket.rb
bunny-1.5.0 lib/bunny/cruby/ssl_socket.rb
bunny-1.5.0.pre2 lib/bunny/cruby/ssl_socket.rb
bunny-1.5.0.pre1 lib/bunny/cruby/ssl_socket.rb
bunny-1.4.1 lib/bunny/cruby/ssl_socket.rb
bunny-1.4.0 lib/bunny/cruby/ssl_socket.rb
bunny-1.3.1 lib/bunny/cruby/ssl_socket.rb
bunny-1.3.0 lib/bunny/cruby/ssl_socket.rb
bunny-1.2.2 lib/bunny/cruby/ssl_socket.rb
bunny-1.2.1 lib/bunny/cruby/ssl_socket.rb
bunny-1.2.0 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.9 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.8 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.7 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.6 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.5 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.4 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.3 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.2 lib/bunny/cruby/ssl_socket.rb
bunny-1.1.1 lib/bunny/cruby/ssl_socket.rb