Class: Mongo::Socket
- Inherits:
-
Object
- Object
- Mongo::Socket
- 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.
Defined Under Namespace
Constant Summary collapse
- SSL_ERROR =
Error message for SSL related exceptions.
'SSL handshake failed. MongoDB may not be configured with SSL support.'.freeze
- TIMEOUT_ERROR =
Error message for timeouts on socket calls.
'Socket request timed out'.freeze
- TIMEOUT_PACK =
The pack directive for timeouts.
'l_2'.freeze
Instance Attribute Summary collapse
-
#family ⇒ Integer
readonly
Family The type of host family.
-
#socket ⇒ Socket
readonly
Socket The wrapped socket.
Instance Method Summary collapse
-
#alive? ⇒ true, false
deprecated
Deprecated.
Use #connectable? on the connection instead.
-
#close ⇒ true
Close the socket.
-
#eof? ⇒ Boolean
Tests if this socket has reached EOF.
-
#gets(*args) ⇒ Object
Delegates gets to the underlying socket.
-
#initialize(family) ⇒ Socket
constructor
Create the new socket for the provided family - ipv4, piv6, or unix.
-
#read(length) ⇒ Object
Will read all data from the socket for the provided number of bytes.
-
#readbyte ⇒ Object
Read a single byte from the socket.
-
#write(*args) ⇒ Integer
Writes data to the socket instance.
Constructor Details
#initialize(family) ⇒ Socket
Create the new socket for the provided family - ipv4, piv6, or unix.
102 103 104 105 106 |
# File 'lib/mongo/socket.rb', line 102 def initialize(family) @family = family @socket = ::Socket.new(family, SOCK_STREAM, 0) (@socket) end |
Instance Attribute Details
#family ⇒ Integer (readonly)
Returns family The type of host family.
45 46 47 |
# File 'lib/mongo/socket.rb', line 45 def family @family end |
#socket ⇒ Socket (readonly)
Returns socket The wrapped socket.
48 49 50 |
# File 'lib/mongo/socket.rb', line 48 def socket @socket end |
Instance Method Details
#alive? ⇒ true, false
Use #connectable? on the connection instead.
Is the socket connection alive?
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 |
#close ⇒ true
Close the socket.
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.
163 164 165 166 167 |
# File 'lib/mongo/socket.rb', line 163 def eof? @socket.eof? rescue IOError, SystemCallError => e true end |
#gets(*args) ⇒ Object
Delegates gets to the underlying socket.
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.
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 |
#readbyte ⇒ Object
Read a single byte from the socket.
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.
156 157 158 |
# File 'lib/mongo/socket.rb', line 156 def write(*args) handle_errors { @socket.write(*args) } end |