Class: RSpec::Conductor::Protocol::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/conductor/protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Socket

Returns a new instance of Socket.



11
12
13
# File 'lib/rspec/conductor/protocol.rb', line 11

def initialize(io)
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



9
10
11
# File 'lib/rspec/conductor/protocol.rb', line 9

def io
  @io
end

Instance Method Details

#closeObject



38
39
40
# File 'lib/rspec/conductor/protocol.rb', line 38

def close
  io.close unless io.closed?
end

#receive_messageObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/conductor/protocol.rb', line 25

def receive_message
  length_bytes = io.read(4)
  return nil unless length_bytes&.bytesize == 4

  length = length_bytes.unpack1("N")
  json = io.read(length)
  return nil unless json&.bytesize == length

  JSON.parse(json, symbolize_names: true)
rescue Errno::ECONNRESET, IOError, JSON::ParserError
  nil
end

#send_message(message) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/rspec/conductor/protocol.rb', line 15

def send_message(message)
  json = JSON.generate(message)
  length = [json.bytesize].pack("N")
  io.write(length)
  io.write(json.b)
  io.flush
rescue Errno::EPIPE, IOError
  nil
end