class BitcoinNode::P2p::Client

Attributes

handshaked[RW]
handshaked?[RW]
version[RW]

Public Class Methods

connect(host, port = 8333, probe = LoggingProbe.new("client- click to toggle source
# File lib/bitcoin_node/p2p/client.rb, line 6
def self.connect(host, port = 8333, probe = LoggingProbe.new("client-#{host}"))
  new(host, port, probe)
end
new(host, port = 8333, probe = LoggingProbe.new("client- click to toggle source
# File lib/bitcoin_node/p2p/client.rb, line 12
def initialize(host, port = 8333, probe = LoggingProbe.new("client-#{host}"))
  @host, @buffer, @probe = host, String.new, probe
  @socket = TCPSocket.new(host, port)
  @handshaked = false
  @version = BN::Protocol::VERSION
  @probe << { connected: host }
end

Public Instance Methods

close!() click to toggle source
# File lib/bitcoin_node/p2p/client.rb, line 40
def close!
  @socket.close
  @probe << { closed: @host }
end
send(message) click to toggle source
# File lib/bitcoin_node/p2p/client.rb, line 22
def send(message)
  raise ArgumentError unless BN::Protocol::Message === message

  @probe << { sending: message.command }
  @socket.write(message.raw)

  loop {
    @buffer << @socket.readpartial(1024)
    handler = CommandHandler.new(self, @buffer, @probe)
    if handler.valid_message?
      handler.parse
      break
    end
  }
rescue IOError => e
  BN::ClientLogger.error(e.message)
end