Class: Mongo::Socket

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants
Defined in:
lib/mongo/socket.rb,
lib/mongo/socket/ssl.rb,
lib/mongo/socket/tcp.rb,
lib/mongo/socket/unix.rb

Overview

Provides additional data around sockets for the driver's use.

Since:

  • 2.0.0

Direct Known Subclasses

SSL, TCP, Unix

Defined Under Namespace

Classes: SSL, TCP, Unix

Constant Summary collapse

SSL_ERROR =

Error message for SSL related exceptions.

Since:

  • 2.0.0

'MongoDB may not be configured with SSL support'.freeze
TIMEOUT_ERROR =
Deprecated.

Error message for timeouts on socket calls.

Since:

  • 2.0.0

'Socket request timed out'.freeze
TIMEOUT_PACK =

The pack directive for timeouts.

Since:

  • 2.0.0

'l_2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(family) ⇒ Socket

Create the new socket for the provided family - ipv4, piv6, or unix.

Examples:

Create a new ipv4 socket.

Socket.new(Socket::PF_INET)

Parameters:

  • family (Integer)

    The socket domain.

Since:

  • 2.0.0



106
107
108
109
110
# File 'lib/mongo/socket.rb', line 106

def initialize(family)
  @family = family
  @socket = ::Socket.new(family, SOCK_STREAM, 0)
  set_socket_options(@socket)
end

Instance Attribute Details

#familyInteger (readonly)

Returns family The type of host family.

Returns:

  • (Integer)

    family The type of host family.

Since:

  • 2.0.0



46
47
48
# File 'lib/mongo/socket.rb', line 46

def family
  @family
end

#optionsHash (readonly)

Returns The options.

Returns:

  • (Hash)

    The options.

Since:

  • 2.0.0



52
53
54
# File 'lib/mongo/socket.rb', line 52

def options
  @options
end

#socketSocket (readonly)

Returns socket The wrapped socket.

Returns:

  • (Socket)

    socket The wrapped socket.

Since:

  • 2.0.0



49
50
51
# File 'lib/mongo/socket.rb', line 49

def socket
  @socket
end

Instance Method Details

#alive?true, false

Deprecated.

Use #connectable? on the connection instead.

Is the socket connection alive?

Examples:

Is the socket alive?

socket.alive?

Returns:

  • (true, false)

    If the socket is alive.

Since:

  • 2.0.0



62
63
64
65
66
67
68
69
# File 'lib/mongo/socket.rb', line 62

def alive?
  sock_arr = [ @socket ]
  if Kernel::select(sock_arr, nil, sock_arr, 0)
    eof?
  else
    true
  end
end

#closetrue

Close the socket.

Examples:

Close the socket.

socket.close

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



79
80
81
82
# File 'lib/mongo/socket.rb', line 79

def close
  @socket.close rescue true
  true
end

#connectable?true

Deprecated.

For backwards compatibilty only, do not use.

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0



178
179
180
# File 'lib/mongo/socket.rb', line 178

def connectable?
  true
end

#eof?Boolean

Tests if this socket has reached EOF. Primarily used for liveness checks.

Returns:

  • (Boolean)

Since:

  • 2.0.5



167
168
169
170
171
# File 'lib/mongo/socket.rb', line 167

def eof?
  @socket.eof?
rescue IOError, SystemCallError
  true
end

#gets(*args) ⇒ Object

Delegates gets to the underlying socket.

Examples:

Get the next line.

socket.gets(10)

Parameters:

  • args (Array<Object>)

    The arguments to pass through.

Returns:

  • (Object)

    The returned bytes.

Since:

  • 2.0.0



94
95
96
# File 'lib/mongo/socket.rb', line 94

def gets(*args)
  handle_errors { @socket.gets(*args) }
end

#read(length) ⇒ Object

Will read all data from the socket for the provided number of bytes. If no data is returned, an exception will be raised.

Examples:

Read all the requested data from the socket.

socket.read(4096)

Parameters:

  • length (Integer)

    The number of bytes to read.

Returns:

  • (Object)

    The data from the socket.

Raises:

  • (Mongo::SocketError)

    If not all data is returned.

Since:

  • 2.0.0



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mongo/socket.rb', line 125

def read(length)
  handle_errors do
    data = read_from_socket(length)
    raise IOError unless (data.length > 0 || length == 0)
    while data.length < length
      chunk = read_from_socket(length - data.length)
      raise IOError unless (chunk.length > 0 || length == 0)
      data << chunk
    end
    data
  end
end

#readbyteObject

Read a single byte from the socket.

Examples:

Read a single byte.

socket.readbyte

Returns:

  • (Object)

    The read byte.

Since:

  • 2.0.0



146
147
148
# File 'lib/mongo/socket.rb', line 146

def readbyte
  handle_errors { @socket.readbyte }
end

#write(*args) ⇒ Integer

Writes data to the socket instance.

Examples:

Write to the socket.

socket.write(data)

Parameters:

  • args (Array<Object>)

    The data to be written.

Returns:

  • (Integer)

    The length of bytes written to the socket.

Since:

  • 2.0.0



160
161
162
# File 'lib/mongo/socket.rb', line 160

def write(*args)
  handle_errors { @socket.write(*args) }
end