Module: Bones::RPC::Connection::Socket::Connectable::ClassMethods

Defined in:
lib/bones/rpc/connection/socket/connectable.rb

Overview

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#connect(host, port, timeout) ⇒ TCPSocket

Connect to the tcp server.

Examples:

Connect to the server.

TCPSocket.connect("127.0.0.1", 27017, 30)

Parameters:

  • host (String)

    The host to connect to.

  • post (Integer)

    The server port.

  • timeout (Integer)

    The connection timeout.

Returns:

  • (TCPSocket)

    The socket.

Since:

  • 1.0.0



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 176

def connect(host, port, timeout)
  begin
    Timeout::timeout(timeout) do
      sock = new(host, port)
      sock.set_encoding('binary')
      timeout_val = [ timeout, 0 ].pack("l_2")
      sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
      sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeout_val)
      sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeout_val)
      sock
    end
  rescue Timeout::Error
    raise Errors::ConnectionFailure, "Timed out connection to Bones RPC on #{host}:#{port}"
  end
end