Class: JRPC::Transport::SocketTcp

Inherits:
SocketBase show all
Defined in:
lib/jrpc/transport/socket_tcp.rb

Instance Attribute Summary collapse

Attributes inherited from SocketBase

#options, #read_timeout, #write_timeout

Instance Method Summary collapse

Methods inherited from SocketBase

connect, #connect

Constructor Details

#initialize(options) ⇒ SocketTcp

Returns a new instance of SocketTcp.



7
8
9
10
# File 'lib/jrpc/transport/socket_tcp.rb', line 7

def initialize(options)
  super
  @socket = build_socket
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



5
6
7
# File 'lib/jrpc/transport/socket_tcp.rb', line 5

def socket
  @socket
end

Instance Method Details

#closeObject



38
39
40
# File 'lib/jrpc/transport/socket_tcp.rb', line 38

def close
  @socket.close
end

#closed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/jrpc/transport/socket_tcp.rb', line 42

def closed?
  @socket.closed?
end

#read(length, timeout = @read_timeout) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jrpc/transport/socket_tcp.rb', line 12

def read(length, timeout = @read_timeout)
  received = ''
  length_to_read = length
  while length_to_read > 0
    io_read, = IO.select([@socket], [], [], timeout)
    raise ReadTimeoutError unless io_read
    chunk = io_read[0].read_nonblock(length_to_read)
    received += chunk
    length_to_read -= chunk.bytesize
  end
  received
end

#write(data, timeout = @write_timeout) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jrpc/transport/socket_tcp.rb', line 25

def write(data, timeout = @write_timeout)
  length_written = 0
  data_to_write = data
  while data_to_write.bytesize > 0
    _, io_write, = IO.select([], [@socket], [], timeout)
    raise WriteTimeoutError unless io_write
    chunk_length = io_write[0].write_nonblock(data_to_write)
    length_written += chunk_length
    data_to_write = data.byteslice(length_written, data.length)
  end
  length_written
end