Class: JRPC::Transport::SocketTcp

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

Instance Attribute Summary

Attributes inherited from SocketBase

#options, #read_timeout, #write_timeout

Instance Method Summary collapse

Methods inherited from SocketBase

connect, #connect, #initialize

Constructor Details

This class inherits a constructor from JRPC::Transport::SocketBase

Instance Method Details

#closeObject



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

def close
  return if @socket.nil?
  socket.close
end

#closed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/jrpc/transport/socket_tcp.rb', line 44

def closed?
  @socket.nil? || socket.closed?
end

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/jrpc/transport/socket_tcp.rb', line 5

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
rescue Errno::EPIPE, EOFError => e
  # EPIPE, in this case, means that the data connection was unexpectedly terminated.
  clear_socket!
  raise ReadFailedError, "#{e.class} #{e.message}"
end

#socketObject



48
49
50
# File 'lib/jrpc/transport/socket_tcp.rb', line 48

def socket
  @socket ||= build_socket
end

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



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

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
rescue Errno::EPIPE => e
  # EPIPE, in this case, means that the data connection was unexpectedly terminated.
  clear_socket!
  raise WriteFailedError, "#{e.class} #{e.message}"
end