Class: DEVp2p::P2PProtocol::Disconnect

Inherits:
Command
  • Object
show all
Defined in:
lib/devp2p/p2p_protocol.rb

Constant Summary collapse

Reason =
{
  disconnect_requested: 0,
  tcp_sub_system_error: 1,
  bad_protocol: 2, # e.g. a malformed message, bad RLP, incorrect magic number
  useless_peer: 3,
  too_many_peers: 4,
  already_connected: 5,
  incompatible_p2p_version: 6,
  null_node_identity_received: 7,
  client_quitting: 8,
  unexpected_identity: 9,
  connected_to_self: 10,
  timeout: 11,
  subprotocol_error: 12,
  other: 16
}.freeze

Instance Attribute Summary

Attributes inherited from Command

#receive_callbacks

Instance Method Summary collapse

Methods inherited from Command

decode_payload, encode_payload, #initialize, sedes

Methods included from Configurable

#add_config

Constructor Details

This class inherits a constructor from DEVp2p::Command

Instance Method Details

#create(proto, reason = ) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
# File 'lib/devp2p/p2p_protocol.rb', line 98

def create(proto, reason=Reason[:client_quitting])
  raise ArgumentError, "unknown reason" unless reason_key(reason)
  logger.debug "send_disconnect", peer: proto.peer, reason: reason_name(reason)

  proto.peer.async.report_error "sending disconnect #{reason_name(reason)}"

  Concurrent::ScheduledTask.execute(0.5) { proto.peer.async.stop }

  {reason: reason}
end

#reason_key(id) ⇒ Object



89
90
91
# File 'lib/devp2p/p2p_protocol.rb', line 89

def reason_key(id)
  Reason.invert[id]
end

#reason_name(id) ⇒ Object



93
94
95
96
# File 'lib/devp2p/p2p_protocol.rb', line 93

def reason_name(id)
  key = reason_key id
  key ? key.to_s : "unknown (id:#{id})"
end

#receive(proto, data) ⇒ Object



109
110
111
112
113
# File 'lib/devp2p/p2p_protocol.rb', line 109

def receive(proto, data)
  logger.debug "receive_disconnect", peer: proto.peer, reason: reason_name(data[:reason])
  proto.peer.async.report_error "disconnected #{reason_name(data[:reason])}"
  proto.peer.async.stop
end