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

Included in:
SSL, TCP
Defined in:
lib/bones/rpc/connection/socket/connectable.rb

Overview

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject (readonly)

Since:

  • 2.0.0



8
9
10
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 8

def host
  @host
end

#portObject (readonly)

Since:

  • 2.0.0



8
9
10
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 8

def port
  @port
end

Class Method Details

.included(klass) ⇒ Object

Bring in the class methods when included.

Examples:

Extend the class methods.

Connectable.included(class)

Parameters:

  • klass (Class)

    The class including the module.

Since:

  • 1.3.0



37
38
39
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 37

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#alive?true, false

Is the socket connection alive?

Examples:

Is the socket alive?

socket.alive?

Returns:

  • (true, false)

    If the socket is alive.

Since:

  • 1.0.0



18
19
20
21
22
23
24
25
26
27
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 18

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

#read(size = nil, buf = nil) ⇒ Object

Read from the TCP socket.

Parameters:

  • size (Integer) (defaults to: nil)

    The length to read.

  • buf (String, NilClass) (defaults to: nil)

    The string which will receive the data.

Returns:

  • (Object)

    The data.

Since:

  • 1.2.0



49
50
51
52
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 49

def read(size = nil, buf = nil)
  check_if_alive!
  handle_socket_errors { super }
end

#readpartial(maxlen, buf = nil) ⇒ Object

Read from the TCP socket.

Parameters:

  • size (Integer)

    The maximum length to read.

  • buf (String, NilClass) (defaults to: nil)

    The string which will receive the data.

Returns:

  • (Object)

    The data.

Since:

  • 1.2.0



62
63
64
65
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 62

def readpartial(maxlen, buf = nil)
  check_if_alive!
  handle_socket_errors { super }
end

#set_encoding(string) ⇒ Object

Set the encoding of the underlying socket.

Parameters:

  • string (String)

    The encoding.

Since:

  • 1.3.0



72
73
74
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 72

def set_encoding(string)
  to_io.set_encoding(string)
end

#write(*args) ⇒ Integer

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.

Since:

  • 1.0.0



86
87
88
89
# File 'lib/bones/rpc/connection/socket/connectable.rb', line 86

def write(*args)
  check_if_alive!
  handle_socket_errors { super }
end