Class: Ciri::DevP2P::ProtocolIO
- Inherits:
-
Object
- Object
- Ciri::DevP2P::ProtocolIO
- Defined in:
- lib/ciri/devp2p/protocol_io.rb
Overview
send/read sub protocol msg
Defined Under Namespace
Classes: Error, InvalidMessageCode
Instance Attribute Summary collapse
-
#msg_queue ⇒ Object
readonly
Returns the value of attribute msg_queue.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
-
#initialize(protocol, offset, frame_io) ⇒ ProtocolIO
constructor
A new instance of ProtocolIO.
- #read_msg ⇒ Object
- #send_data(code, data) ⇒ Object
- #write_msg(msg) ⇒ Object
Constructor Details
#initialize(protocol, offset, frame_io) ⇒ ProtocolIO
Returns a new instance of ProtocolIO.
34 35 36 37 38 39 40 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 34 def initialize(protocol, offset, frame_io) @protocol = protocol @offset = offset @frame_io = frame_io @msg_queue = Queue.new @mutex = Mutex.new end |
Instance Attribute Details
#msg_queue ⇒ Object (readonly)
Returns the value of attribute msg_queue.
32 33 34 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 32 def msg_queue @msg_queue end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
32 33 34 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 32 def offset @offset end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
32 33 34 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 32 def protocol @protocol end |
Instance Method Details
#read_msg ⇒ Object
55 56 57 58 59 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 55 def read_msg msg = msg_queue.pop msg.code -= offset msg end |
#send_data(code, data) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 42 def send_data(code, data) @mutex.synchronize do msg = RLPX::Message.new(code: code, size: data.size, payload: data) write_msg(msg) end end |
#write_msg(msg) ⇒ Object
49 50 51 52 53 |
# File 'lib/ciri/devp2p/protocol_io.rb', line 49 def write_msg(msg) raise InvalidMessageCode, "code #{code} must less than length #{protocol.length}" if msg.code > protocol.length msg.code += offset @frame_io.write_msg(msg) end |