/** * Closes the connection to the peer if one exists, or immediately closes the connection as soon * as it opens */ public void close() { lock.lock(); try { if (writeTarget == null) { closePending = true; return; } } finally { lock.unlock(); } writeTarget.closeConnection(); }
/** * Sets the {@link MessageWriteTarget} used to write messages to the peer. This should almost * never be called, it is called automatically by {@link org.bitcoinj.net.NioClient} or {@link * org.bitcoinj.net.NioClientManager} once the socket finishes initialization. */ @Override public void setWriteTarget(MessageWriteTarget writeTarget) { checkArgument(writeTarget != null); lock.lock(); boolean closeNow = false; try { checkArgument(this.writeTarget == null); closeNow = closePending; this.writeTarget = writeTarget; } finally { lock.unlock(); } if (closeNow) writeTarget.closeConnection(); }