Class: Kward::RPC::Transport
- Inherits:
-
Object
- Object
- Kward::RPC::Transport
- Defined in:
- lib/kward/rpc/transport.rb
Overview
Line-delimited JSON-RPC transport over input and output streams.
Instance Method Summary collapse
-
#initialize(input:, output:) ⇒ Transport
constructor
A new instance of Transport.
- #read_message ⇒ Object
- #write_message(message) ⇒ Object
Constructor Details
#initialize(input:, output:) ⇒ Transport
Returns a new instance of Transport.
9 10 11 12 13 |
# File 'lib/kward/rpc/transport.rb', line 9 def initialize(input:, output:) @input = input @output = output @write_mutex = Mutex.new end |
Instance Method Details
#read_message ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kward/rpc/transport.rb', line 15 def headers = read_headers return nil unless headers length = headers["content-length"].to_i raise "Missing Content-Length header" if length <= 0 body = @input.read(length) raise "Unexpected EOF while reading JSON-RPC body" unless body && body.bytesize == length JSON.parse(body) end |
#write_message(message) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/kward/rpc/transport.rb', line 28 def () body = JSON.generate() @write_mutex.synchronize do @output.write("Content-Length: #{body.bytesize}\r\n\r\n") @output.write(body) @output.flush if @output.respond_to?(:flush) end end |