Class: Ciri::P2P::ProtocolIO
- Inherits:
-
Object
- Object
- Ciri::P2P::ProtocolIO
- Defined in:
- lib/ciri/p2p/protocol_io.rb
Overview
send/read sub protocol msg
Defined Under Namespace
Classes: Error, InvalidMessageCode
Instance Attribute Summary collapse
-
#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.
- #send_data(code, data) ⇒ Object
- #write_msg(msg) ⇒ Object
Constructor Details
#initialize(protocol, offset, frame_io) ⇒ ProtocolIO
41 42 43 44 45 46 |
# File 'lib/ciri/p2p/protocol_io.rb', line 41 def initialize(protocol, offset, frame_io) @protocol = protocol @offset = offset @frame_io = frame_io @semaphore = Async::Semaphore.new end |
Instance Attribute Details
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
39 40 41 |
# File 'lib/ciri/p2p/protocol_io.rb', line 39 def offset @offset end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
39 40 41 |
# File 'lib/ciri/p2p/protocol_io.rb', line 39 def protocol @protocol end |
Instance Method Details
#send_data(code, data) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/ciri/p2p/protocol_io.rb', line 48 def send_data(code, data) @semaphore.acquire do msg = RLPX::Message.new(code: code, size: data.size, payload: data) write_msg(msg) end end |
#write_msg(msg) ⇒ Object
55 56 57 58 59 |
# File 'lib/ciri/p2p/protocol_io.rb', line 55 def write_msg(msg) raise InvalidMessageCode, "code #{msg.code} must less than length #{protocol.length}" if msg.code > protocol.length msg.code += offset @frame_io.write_msg(msg) end |