Class: Moped::Connection::TCPSocket Private

Inherits:
TCPSocket
  • Object
show all
Defined in:
lib/moped/connection.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.

This is a wrapper around a tcp socket.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, *args) ⇒ TCPSocket

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.

Initialize the new TCPSocket.

Examples:

Initialize the socket.

TCPSocket.new("127.0.0.1", 27017)

Parameters:

Since:

  • 1.2.0



201
202
203
204
# File 'lib/moped/connection.rb', line 201

def initialize(host, port, *args)
  @host, @port = host, port
  handle_socket_errors { super }
end

Instance Attribute Details

#hostObject (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.



174
175
176
# File 'lib/moped/connection.rb', line 174

def host
  @host
end

#portObject (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.



174
175
176
# File 'lib/moped/connection.rb', line 174

def port
  @port
end

Class Method Details

.connect(host, port, timeout) ⇒ TCPSocket

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.

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:

Since:

  • 1.0.0



258
259
260
261
262
263
264
265
# File 'lib/moped/connection.rb', line 258

def connect(host, port, timeout)
  Timeout::timeout(timeout) do
    sock = new(host, port)
    sock.set_encoding('binary')
    sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
    sock
  end
end

Instance Method Details

#alive?true, false

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.

Is the socket connection alive?

Examples:

Is the socket alive?

socket.alive?

Returns:

  • (true, false)

    If the socket is alive.

Since:

  • 1.0.0



184
185
186
187
188
189
190
# File 'lib/moped/connection.rb', line 184

def alive?
  if Kernel::select([ self ], nil, nil, 0)
    !eof? rescue false
  else
    true
  end
end

#read(length) ⇒ 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.

Read from the TCP socket.

Parameters:

  • length (Integer)

    The length to read.

Returns:

Since:

  • 1.2.0



213
214
215
# File 'lib/moped/connection.rb', line 213

def read(length)
  handle_socket_errors { super }
end

#write(*args) ⇒ Integer

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.

Write to the socket.

Examples:

Write to the socket.

socket.write(data)

Parameters:

  • args (Object)

    The data to write.

Returns:

  • (Integer)

    The number of bytes written.

Raises:

Since:

  • 1.0.0



227
228
229
230
# File 'lib/moped/connection.rb', line 227

def write(*args)
  raise Errors::ConnectionFailure, "Socket connection was closed by remote host" unless alive?
  handle_socket_errors { super }
end