Class: ESM::Connection::ClientSocket

Inherits:
Socket
  • Object
show all
Defined in:
lib/esm/connection/client_socket.rb

Constant Summary collapse

HEADER_SIZE =
4.bytes
MAX_READ =
16.megabytes

Constants inherited from Socket

Socket::IGNORED_IO_ERRORS

Instance Attribute Summary

Attributes inherited from Socket

#address

Instance Method Summary collapse

Methods inherited from Socket

#close, #initialize, #readable?, #shutdown, #writeable?

Constructor Details

This class inherits a constructor from ESM::Connection::Socket

Instance Method Details

#readObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/esm/connection/client_socket.rb', line 9

def read
  return unless readable?

  length_bytes = @socket.read(HEADER_SIZE)
  return if length_bytes.blank?

  length = length_bytes.unpack1("N")
  raise Exception::MessageTooLarge, length if length >= MAX_READ

  data = @socket.read(length)
  Base64.strict_decode64(data)
rescue IOError => e
  return if ignored_io_error?(e)

  raise # Re-raise
rescue => e
  error!(address:, error: e)
  nil
end

#write(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/esm/connection/client_socket.rb', line 29

def write(data)
  raise TypeError, "Expected String, got #{data.class}" unless data.is_a?(String)
  return unless writeable?

  # Encode
  data = Base64.strict_encode64(data)

  # Send
  @socket.write(data)
rescue TypeError
  raise
rescue Errno::EPIPE
  close
rescue => e
  error!(error: e)
end