Class: Protocol::Jsonrpc::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/jsonrpc/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(framer) ⇒ Connection

Initialize a new Connection instance

Parameters:



11
12
13
# File 'lib/protocol/jsonrpc/connection.rb', line 11

def initialize(framer)
  @framer = framer
end

Instance Method Details

#closeObject



19
20
21
# File 'lib/protocol/jsonrpc/connection.rb', line 19

def close
  @framer.close
end

#flushObject



15
16
17
# File 'lib/protocol/jsonrpc/connection.rb', line 15

def flush
  @framer.flush
end

#read {|Protocol::Jsonrpc::Message| ... } ⇒ Protocol::Jsonrpc::Message, Protocol::Jsonrpc::Batch

Read the next message or batch of messages from the framer

Yields:

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/protocol/jsonrpc/connection.rb', line 26

def read(&block)
  flush
  frame = read_frame
  message = Message.load(frame.unpack)
  yield message if block_given?
  message
rescue => e
  InvalidMessage.new(error: e)
end

#read_frame {|Protocol::Jsonrpc::Frame| ... } ⇒ Protocol::Jsonrpc::Frame

Low level read a frame from the framer

Yields:

Returns:



46
47
48
49
50
# File 'lib/protocol/jsonrpc/connection.rb', line 46

def read_frame(&)
  frame = @framer.read_frame
  yield frame if block_given?
  frame
end

#write(message) ⇒ Boolean

Write a message to the framer

Parameters:

Returns:

  • (Boolean)

    True if successful



39
40
41
# File 'lib/protocol/jsonrpc/connection.rb', line 39

def write(message)
  write_frame Frame.pack(message)
end

#write_frame(frame) ⇒ Boolean

Low level write a frame to the framer

Parameters:

Returns:

  • (Boolean)

    True if successful



55
56
57
# File 'lib/protocol/jsonrpc/connection.rb', line 55

def write_frame(frame)
  @framer.write_frame(frame)
end