Class: Ciri::P2P::ProtocolIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/p2p/protocol_io.rb

Overview

send/read sub protocol msg

Defined Under Namespace

Classes: Error, InvalidMessageCode

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#offsetObject (readonly)

Returns the value of attribute offset.



39
40
41
# File 'lib/ciri/p2p/protocol_io.rb', line 39

def offset
  @offset
end

#protocolObject (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

Raises:



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