Class: Ciri::DevP2P::ProtocolIO

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/devp2p/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

Returns a new instance of ProtocolIO.


40
41
42
43
44
45
# File 'lib/ciri/devp2p/protocol_io.rb', line 40

def initialize(protocol, offset, frame_io)
  @protocol = protocol
  @offset = offset
  @frame_io = frame_io
  @msg_queue = Queue.new
end

Instance Attribute Details

#msg_queueObject (readonly)

Returns the value of attribute msg_queue


38
39
40
# File 'lib/ciri/devp2p/protocol_io.rb', line 38

def msg_queue
  @msg_queue
end

#offsetObject (readonly)

Returns the value of attribute offset


38
39
40
# File 'lib/ciri/devp2p/protocol_io.rb', line 38

def offset
  @offset
end

#protocolObject (readonly)

Returns the value of attribute protocol


38
39
40
# File 'lib/ciri/devp2p/protocol_io.rb', line 38

def protocol
  @protocol
end

Instance Method Details

#read_msgObject


58
59
60
61
62
# File 'lib/ciri/devp2p/protocol_io.rb', line 58

def read_msg
  msg = msg_queue.pop
  msg.code -= offset
  msg
end

#send_data(code, data) ⇒ Object


47
48
49
50
# File 'lib/ciri/devp2p/protocol_io.rb', line 47

def send_data(code, data)
  msg = RLPX::Message.new(code: code, size: data.size, payload: data)
  write_msg(msg)
end

#write_msg(msg) ⇒ Object

Raises:


52
53
54
55
56
# File 'lib/ciri/devp2p/protocol_io.rb', line 52

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