Class: Coronet::TransportMechanism::LengthPrefixedTcpTransport

Inherits:
Base
  • Object
show all
Defined in:
lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb

Overview

Supports length-prefixed TCP transport.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#transmit

Instance Attribute Details

#tcp_socketObject

Returns the value of attribute tcp_socket.



9
10
11
# File 'lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb', line 9

def tcp_socket
  @tcp_socket
end

Instance Method Details

#close(io) ⇒ Object



30
31
32
# File 'lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb', line 30

def close(io)
  io.close
end

#open(host, port) ⇒ Object



11
12
13
# File 'lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb', line 11

def open(host, port)
  TCPSocket.open(host, port)
end

#read(io) ⇒ Object



15
16
17
18
19
20
# File 'lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb', line 15

def read(io)
  length_header = io.read(2)
  p length_header
  length = length_header.unpack("S").first
  io.read(length)
end

#write(data, io) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/coronet/transport_mechanism/length_prefixed_tcp_transport.rb', line 22

def write(data, io)
  length = data.size
  prefix = to_byte_array(length).pack("S")

  io.print(prefix)
  io.print(data)
end