Class PeerSocketHandler

java.lang.Object
org.bitcoinj.core.PeerSocketHandler
All Implemented Interfaces:
StreamConnection, TimeoutHandler
Direct Known Subclasses:
Peer

public abstract class PeerSocketHandler extends Object implements TimeoutHandler, StreamConnection
Handles high-level message (de)serialization for peers, acting as the bridge between the org.bitcoinj.net classes and Peer.
  • Field Details

  • Constructor Details

  • Method Details

    • setTimeoutEnabled

      public void setTimeoutEnabled(boolean timeoutEnabled)
      Description copied from interface: TimeoutHandler

      Enables or disables the timeout entirely. This may be useful if you want to store the timeout value but wish to temporarily disable/enable timeouts.

      The default is for timeoutEnabled to be true but timeout to be set to 0 (ie disabled).

      This call will reset the current progress towards the timeout.

      Specified by:
      setTimeoutEnabled in interface TimeoutHandler
    • setSocketTimeout

      public void setSocketTimeout(Duration timeout)
      Description copied from interface: TimeoutHandler

      Sets the receive timeout, automatically killing the connection if no messages are received for this long

      A timeout of Duration.ZERO is interpreted as no timeout.

      The default is for timeoutEnabled to be true but timeout to be set to Duration.ZERO (ie disabled).

      This call will reset the current progress towards the timeout.

      Specified by:
      setSocketTimeout in interface TimeoutHandler
    • sendMessage

      public ListenableCompletableFuture<Void> sendMessage(Message message) throws NotYetConnectedException
      Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. TODO: Maybe use something other than the unchecked NotYetConnectedException here
      Throws:
      NotYetConnectedException
    • close

      public void close()
      Closes the connection to the peer if one exists, or immediately closes the connection as soon as it opens
    • timeoutOccurred

      protected void timeoutOccurred()
    • processMessage

      protected abstract void processMessage(Message m) throws Exception
      Called every time a message is received from the network
      Throws:
      Exception
    • receiveBytes

      public int receiveBytes(ByteBuffer buff)
      Description copied from interface: StreamConnection

      Called when new bytes are available from the remote end. This should only ever be called by the single writeTarget associated with any given StreamConnection, multiple callers will likely confuse implementations.

      Implementers/callers must follow the following conventions exactly:
      • buff will start with its limit set to the position we can read to and its position set to the location we will start reading at (always 0)
      • May read more than one message (recursively) if there are enough bytes available
      • Uses some internal buffering to store message which are larger (incl their length prefix) than buff's capacity(), ie it is up to this method to ensure we don't run out of buffer space to decode the next message.
      • buff will end with its limit the same as it was previously, and its position set to the position up to which bytes have been read (the same as its return value)
      • buff must be at least the size of a Bitcoin header (incl magic bytes).
      Specified by:
      receiveBytes in interface StreamConnection
      Returns:
      The amount of bytes consumed which should not be provided again
    • setWriteTarget

      public void setWriteTarget(MessageWriteTarget writeTarget)
      Sets the MessageWriteTarget used to write messages to the peer. This should almost never be called, it is called automatically by NioClient or NioClientManager once the socket finishes initialization.
      Specified by:
      setWriteTarget in interface StreamConnection
    • getMaxMessageSize

      public int getMaxMessageSize()
      Description copied from interface: StreamConnection
      Returns the maximum message size of a message on the socket. This is used in calculating size of buffers to allocate.
      Specified by:
      getMaxMessageSize in interface StreamConnection
    • getAddress

      public PeerAddress getAddress()
      Returns:
      the IP address and port of peer.