123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Socket::TCP Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, ::Mongo::Socket, Socket::Constants
Inherits: Mongo::Socket
Defined in: lib/mongo/socket/tcp.rb

Overview

Wrapper for TCP sockets.

Since:

  • 2.0.0

Constant Summary

::Mongo::Socket - Inherited

DEFAULT_TCP_KEEPCNT, DEFAULT_TCP_KEEPIDLE, DEFAULT_TCP_KEEPINTVL, DEFAULT_TCP_USER_TIMEOUT, SSL_ERROR, TIMEOUT_ERROR, TIMEOUT_PACK, WRITE_CHUNK_SIZE

Class Method Summary

::Mongo::Socket - Inherited

.new

Initializes common socket attributes.

Instance Attribute Summary

::Mongo::Socket - Inherited

#alive?

Is the socket connection alive?

#connectable?

For backwards compatibility only, do not use.

#eof?

Tests if this socket has reached EOF.

#family, #monitor?, #options, #socket, #timeout

Instance Method Summary

::Mongo::Socket - Inherited

#close

Close the socket.

#connection_address, #connection_generation,
#gets

Delegates gets to the underlying socket.

#read

Will read all data from the socket for the provided number of bytes.

#readbyte

Read a single byte from the socket.

#summary,
#write

Writes data to the socket instance.

#allocate_string,
#do_write

Writes data to the socket instance.

#human_address, #map_exceptions, #raise_timeout_error!, #read_buffer_size,
#read_from_socket

Reads the length bytes from the socket.

#read_with_timeout

Reads the length bytes from the socket, the read operation duration is limited to #timeout second.

#read_without_timeout

Reads the length bytes from the socket.

#set_keepalive_opts, #set_option, #set_socket_options, #unix_socket?, #write_chunk,
#write_with_timeout

Writes data to to the socket, the write duration is limited to #timeout.

#write_without_timeout

Writes data to to the socket.

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)

    host The host to connect to.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 66

attr_reader :host

#portInteger (readonly)

Returns:

  • (Integer)

    port The port to connect to.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 69

attr_reader :port

Instance Method Details

#connect!TCP

This method is for internal use only.
Note:

This method mutates the object by setting the socket internally.

Establishes a socket connection.

Examples:

Connect the socket.

sock.connect!

Returns:

  • (TCP)

    The connected socket instance.

Since:

  • 2.0.0

[ GitHub ]

  
# 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 = options[: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)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# 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)

This method is for internal use only.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 98

def connect_without_timeout(sockaddr)
  socket.connect(sockaddr)
end

#human_address (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/socket/tcp.rb', line 129

def human_address
  "#{host}:#{port} (no TLS)"
end