Class: Mongo::Socket

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants
Defined in:
lib/mongo/socket.rb,
lib/mongo/socket/tcp.rb,
lib/mongo/socket/ssl.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

SSL_ERROR =

Error message for SSL related exceptions.

Since:

  • 2.0.0

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

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



102
103
104
105
106
# File 'lib/mongo/socket.rb', line 102

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



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

def family
  @family
end

#socketSocket (readonly)

Returns socket The wrapped socket.

Returns:

  • (Socket)

    socket The wrapped socket.

Since:

  • 2.0.0



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

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



58
59
60
61
62
63
64
65
# File 'lib/mongo/socket.rb', line 58

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



75
76
77
78
# File 'lib/mongo/socket.rb', line 75

def close
  @socket.close rescue true
  true
end

#eof?Boolean

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

Returns:

  • (Boolean)

Since:

  • 2.0.5



163
164
165
166
167
# File 'lib/mongo/socket.rb', line 163

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



90
91
92
# File 'lib/mongo/socket.rb', line 90

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



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mongo/socket.rb', line 121

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



142
143
144
# File 'lib/mongo/socket.rb', line 142

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



156
157
158
# File 'lib/mongo/socket.rb', line 156

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