Class: Arcp::Transport::StdioTransport

Inherits:
Base
  • Object
show all
Defined in:
lib/arcp/transport/stdio_transport.rb

Overview

Newline-delimited JSON over a pair of IOs. The child process reads from stdin and writes to stdout; the parent connects with its pipe ends. Exit on EOF.

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout) ⇒ StdioTransport

Returns a new instance of StdioTransport.



11
12
13
14
15
16
17
# File 'lib/arcp/transport/stdio_transport.rb', line 11

def initialize(input: $stdin, output: $stdout)
  super()
  @input = input
  @output = output
  @closed = false
  @write_mutex = Mutex.new
end

Instance Method Details

#close(reason: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/arcp/transport/stdio_transport.rb', line 43

def close(reason: nil)
  return if @closed

  @closed = true
  @output.close unless @output.closed? || @output == $stdout
  @input.close unless @input.closed? || @input == $stdin
rescue IOError
  nil
end

#closed?Boolean

Returns:

  • (Boolean)


53
# File 'lib/arcp/transport/stdio_transport.rb', line 53

def closed? = @closed

#receiveObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/arcp/transport/stdio_transport.rb', line 31

def receive
  return nil if @closed

  line = @input.gets
  if line.nil?
    @closed = true
    return nil
  end

  Arcp::Envelope.from_json(line)
end

#send(envelope) ⇒ Object

Raises:

  • (IOError)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/arcp/transport/stdio_transport.rb', line 19

def send(envelope)
  raise IOError, 'transport closed' if @closed

  line = Arcp::Serializer.dump(envelope.to_h)
  @write_mutex.synchronize do
    @output.write(line)
    @output.write("\n")
    @output.flush
  end
  nil
end