Class: Mongo::Socket::TCP

Inherits:
Mongo::Socket show all
Defined in:
lib/mongo/socket/tcp.rb

Overview

Wrapper for TCP sockets.

Since:

  • 2.0.0

Constant Summary

Constants inherited from Mongo::Socket

SSL_ERROR, TIMEOUT_ERROR, TIMEOUT_PACK

Instance Attribute Summary collapse

Attributes inherited from Mongo::Socket

#family, #socket

Instance Method Summary collapse

Methods inherited from Mongo::Socket

#alive?, #close, #eof?, #gets, #read, #readbyte, #write

Constructor Details

#initialize(host, port, timeout, family) ⇒ TCP

Initializes a new TCP socket.

Examples:

Create the TCP socket.

TCP.new('::1', 27017, 30, Socket::PF_INET)
TCP.new('127.0.0.1', 27017, 30, Socket::PF_INET)

Parameters:

  • host (String)

    The hostname or IP address.

  • port (Integer)

    The port number.

  • timeout (Float)

    The socket timeout value.

  • family (Integer)

    The socket family.

Since:

  • 2.0.0



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

def initialize(host, port, timeout, family)
  @host, @port, @timeout = host, port, timeout
  super(family)
end

Instance Attribute Details

#hostString (readonly)

Returns host The host to connect to.

Returns:

  • (String)

    host The host to connect to.

Since:

  • 2.0.0



24
25
26
# File 'lib/mongo/socket/tcp.rb', line 24

def host
  @host
end

#portInteger (readonly)

Returns port The port to connect to.

Returns:

  • (Integer)

    port The port to connect to.

Since:

  • 2.0.0



27
28
29
# File 'lib/mongo/socket/tcp.rb', line 27

def port
  @port
end

#timeoutFloat (readonly)

Returns timeout The socket timeout.

Returns:

  • (Float)

    timeout The socket timeout.

Since:

  • 2.0.0



30
31
32
# File 'lib/mongo/socket/tcp.rb', line 30

def timeout
  @timeout
end

Instance Method Details

#connect!(connect_timeout = nil) ⇒ TCP

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



43
44
45
46
47
48
49
50
# File 'lib/mongo/socket/tcp.rb', line 43

def connect!(connect_timeout = nil)
  Timeout.timeout(connect_timeout, Error::SocketTimeoutError) do
    socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
    socket.setsockopt(SOL_SOCKET, SO_KEEPALIVE, true)
    handle_errors { socket.connect(::Socket.pack_sockaddr_in(port, host)) }
    self
  end
end

#connectable?true, false

This object does not wrap another socket so it's always connectable.

Examples:

Is the socket connectable?

socket.connectable?

Returns:

  • (true, false)

    If the socket is connectable.

Since:

  • 2.2.5



77
78
79
# File 'lib/mongo/socket/tcp.rb', line 77

def connectable?
  true
end