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.



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_queueObject (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

#offsetObject (readonly)

Returns the value of attribute offset.



32
33
34
# File 'lib/ciri/devp2p/protocol_io.rb', line 32

def offset
  @offset
end

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



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

Raises:



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