Class: Mongo::Socket::TCP Private
- Inherits:
-
Mongo::Socket
- Object
- Mongo::Socket
- Mongo::Socket::TCP
- Defined in:
- lib/mongo/socket/tcp.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Wrapper for TCP sockets.
Constant Summary
Constants inherited from Mongo::Socket
SSL_ERROR, TIMEOUT_ERROR, TIMEOUT_PACK, WRITE_CHUNK_SIZE
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
private
Host The host to connect to.
-
#port ⇒ Integer
readonly
private
Port The port to connect to.
Attributes inherited from Mongo::Socket
#family, #options, #socket, #timeout
Instance Method Summary collapse
-
#connect! ⇒ TCP
private
Establishes a socket connection.
- #connect_with_timeout(sockaddr, connect_timeout) ⇒ Object private
- #connect_without_timeout(sockaddr) ⇒ Object private
-
#initialize(host, port, timeout, family, options = {}) ⇒ TCP
constructor
private
Initializes a new TCP socket.
Methods inherited from Mongo::Socket
#alive?, #close, #connectable?, #connection_address, #connection_generation, #eof?, #gets, #monitor?, #read, #readbyte, #summary, #write
Constructor Details
#initialize(host, port, timeout, family, options = {}) ⇒ TCP
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a new TCP socket.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mongo/socket/tcp.rb', line 48 def initialize(host, port, timeout, family, = {}) if family.nil? raise ArgumentError, 'family must be specified' end super(timeout, ) @host, @port = host, port @family = family @socket = ::Socket.new(family, SOCK_STREAM, 0) begin (@socket) connect! rescue @socket.close raise end end |
Instance Attribute Details
#host ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns host The host to connect to.
66 67 68 |
# File 'lib/mongo/socket/tcp.rb', line 66 def host @host end |
#port ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns port The port to connect to.
69 70 71 |
# File 'lib/mongo/socket/tcp.rb', line 69 def port @port end |
Instance Method Details
#connect! ⇒ TCP
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method mutates the object by setting the socket internally.
Establishes a socket connection.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mongo/socket/tcp.rb', line 83 def connect! socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) sockaddr = ::Socket.pack_sockaddr_in(port, host) connect_timeout = [:connect_timeout] map_exceptions do if connect_timeout && connect_timeout != 0 connect_with_timeout(sockaddr, connect_timeout) else connect_without_timeout(sockaddr) end end self end |
#connect_with_timeout(sockaddr, connect_timeout) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mongo/socket/tcp.rb', line 103 def connect_with_timeout(sockaddr, connect_timeout) if connect_timeout <= 0 raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect" end deadline = Utils.monotonic_time + connect_timeout begin socket.connect_nonblock(sockaddr) rescue IO::WaitWritable select_timeout = deadline - Utils.monotonic_time if select_timeout <= 0 raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect" end if IO.select(nil, [socket], nil, select_timeout) retry else socket.close raise Error::SocketTimeoutError, "The socket took over #{connect_timeout} seconds to connect" end rescue Errno::EISCONN # Socket is connected, nothing more to do end end |
#connect_without_timeout(sockaddr) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
98 99 100 |
# File 'lib/mongo/socket/tcp.rb', line 98 def connect_without_timeout(sockaddr) socket.connect(sockaddr) end |