Package org.bitcoinj.core
Class PeerSocketHandler
- java.lang.Object
- 
- org.bitcoinj.net.AbstractTimeoutHandler
- 
- org.bitcoinj.core.PeerSocketHandler
 
 
- 
- All Implemented Interfaces:
- StreamConnection
 - Direct Known Subclasses:
- Peer
 
 public abstract class PeerSocketHandler extends AbstractTimeoutHandler implements StreamConnection Handles high-level message (de)serialization for peers, acting as the bridge between theorg.bitcoinj.netclasses andPeer.
- 
- 
Field SummaryFields Modifier and Type Field Description protected PeerAddresspeerAddressprotected MessageWriteTargetwriteTarget
 - 
Constructor SummaryConstructors Constructor Description PeerSocketHandler(NetworkParameters params, java.net.InetSocketAddress remoteIp)PeerSocketHandler(NetworkParameters params, PeerAddress peerAddress)
 - 
Method SummaryAll Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the connection to the peer if one exists, or immediately closes the connection as soon as it opensPeerAddressgetAddress()intgetMaxMessageSize()Returns the maximum message size of a message on the socket.protected abstract voidprocessMessage(Message m)Called every time a message is received from the networkintreceiveBytes(java.nio.ByteBuffer buff)Called when new bytes are available from the remote end.com.google.common.util.concurrent.ListenableFuturesendMessage(Message message)Sends the given message to the peer.voidsetWriteTarget(MessageWriteTarget writeTarget)Sets theMessageWriteTargetused to write messages to the peer.protected voidtimeoutOccurred()- 
Methods inherited from class org.bitcoinj.net.AbstractTimeoutHandlerresetTimeout, setSocketTimeout, setTimeoutEnabled
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface org.bitcoinj.net.StreamConnectionconnectionClosed, connectionOpened
 
- 
 
- 
- 
- 
Field Detail- 
peerAddressprotected PeerAddress peerAddress 
 - 
writeTargetprotected MessageWriteTarget writeTarget 
 
- 
 - 
Constructor Detail- 
PeerSocketHandlerpublic PeerSocketHandler(NetworkParameters params, java.net.InetSocketAddress remoteIp) 
 - 
PeerSocketHandlerpublic PeerSocketHandler(NetworkParameters params, PeerAddress peerAddress) 
 
- 
 - 
Method Detail- 
sendMessagepublic com.google.common.util.concurrent.ListenableFuture sendMessage(Message message) throws java.nio.channels.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:
- java.nio.channels.NotYetConnectedException
 
 - 
closepublic void close() Closes the connection to the peer if one exists, or immediately closes the connection as soon as it opens
 - 
timeoutOccurredprotected void timeoutOccurred() - Specified by:
- timeoutOccurredin class- AbstractTimeoutHandler
 
 - 
processMessageprotected abstract void processMessage(Message m) throws java.lang.Exception Called every time a message is received from the network- Throws:
- java.lang.Exception
 
 - 
receiveBytespublic int receiveBytes(java.nio.ByteBuffer buff) Description copied from interface:StreamConnectionCalled 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:
- receiveBytesin interface- StreamConnection
- Returns:
- The amount of bytes consumed which should not be provided again
 
 - 
setWriteTargetpublic void setWriteTarget(MessageWriteTarget writeTarget) Sets theMessageWriteTargetused to write messages to the peer. This should almost never be called, it is called automatically byNioClientorNioClientManageronce the socket finishes initialization.- Specified by:
- setWriteTargetin interface- StreamConnection
 
 - 
getMaxMessageSizepublic int getMaxMessageSize() Description copied from interface:StreamConnectionReturns the maximum message size of a message on the socket. This is used in calculating size of buffers to allocate.- Specified by:
- getMaxMessageSizein interface- StreamConnection
 
 - 
getAddresspublic PeerAddress getAddress() - Returns:
- the IP address and port of peer.
 
 
- 
 
-