diff --git a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ConnectionHandler.java b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ConnectionHandler.java index 6a7e1ba9c..d9d0baa24 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ConnectionHandler.java +++ b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ConnectionHandler.java @@ -32,7 +32,7 @@ import static com.google.common.base.Preconditions.checkState; /** * A simple connection handler which handles all the business logic of a connection */ -class ConnectionHandler extends MessageWriteTarget { +class ConnectionHandler implements MessageWriteTarget { private static final org.slf4j.Logger log = LoggerFactory.getLogger(ConnectionHandler.class); private static final int BUFFER_SIZE_LOWER_BOUND = 4096; @@ -57,7 +57,7 @@ class ConnectionHandler extends MessageWriteTarget { } @Override - void writeBytes(byte[] message) { + public void writeBytes(byte[] message) throws IOException { lock.lock(); try { if (channel.write(ByteBuffer.wrap(message)) != message.length) @@ -65,13 +65,14 @@ class ConnectionHandler extends MessageWriteTarget { } catch (IOException e) { log.error("Error writing message to connection, closing connection", e); closeConnection(); + throw e; } finally { lock.unlock(); } } @Override - void closeConnection() { + public void closeConnection() { try { channel.close(); } catch (IOException e) { diff --git a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/MessageWriteTarget.java b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/MessageWriteTarget.java index b3bcba46c..6bee48beb 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/MessageWriteTarget.java +++ b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/MessageWriteTarget.java @@ -16,10 +16,12 @@ package com.google.bitcoin.protocols.niowrapper; +import java.io.IOException; + /** * A target to which messages can be written/connection can be closed */ -abstract class MessageWriteTarget { - abstract void writeBytes(byte[] message); - abstract void closeConnection(); +public interface MessageWriteTarget { + void writeBytes(byte[] message) throws IOException; + void closeConnection(); } diff --git a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/NioClient.java b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/NioClient.java index 3a63ee9e7..66652d90f 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/NioClient.java +++ b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/NioClient.java @@ -31,7 +31,7 @@ import static com.google.common.base.Preconditions.checkState; /** * Creates a simple connection to a server using a {@link StreamParser} to process data. */ -public class NioClient extends MessageWriteTarget { +public class NioClient implements MessageWriteTarget { private static final org.slf4j.Logger log = LoggerFactory.getLogger(NioClient.class); private static final int BUFFER_SIZE_LOWER_BOUND = 4096; @@ -111,7 +111,7 @@ public class NioClient extends MessageWriteTarget { // Writes raw bytes to the channel (used by the write method in StreamParser) @Override - synchronized void writeBytes(byte[] message) { + public synchronized void writeBytes(byte[] message) { try { if (sc.write(ByteBuffer.wrap(message)) != message.length) throw new IOException("Couldn't write all of message to socket"); diff --git a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ProtobufParser.java b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ProtobufParser.java index ec875b7d7..892bb4b97 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ProtobufParser.java +++ b/core/src/main/java/com/google/bitcoin/protocols/niowrapper/ProtobufParser.java @@ -20,6 +20,7 @@ import com.google.bitcoin.core.Utils; import com.google.protobuf.ByteString; import com.google.protobuf.MessageLite; +import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -201,7 +202,11 @@ public class ProtobufParser extends AbstractTim checkState(messageBytes.length <= maxMessageSize); byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); - writeTarget.writeBytes(messageLength); - writeTarget.writeBytes(messageBytes); + try { + writeTarget.writeBytes(messageLength); + writeTarget.writeBytes(messageBytes); + } catch (IOException e) { + closeConnection(); + } } }