MessageWriteTarget->interface, writeBytes throws IOException

This commit is contained in:
Matt Corallo 2013-07-16 20:05:20 +02:00 committed by Mike Hearn
parent edd0a0907c
commit cf23e18f23
4 changed files with 18 additions and 10 deletions

View File

@ -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 * 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 org.slf4j.Logger log = LoggerFactory.getLogger(ConnectionHandler.class);
private static final int BUFFER_SIZE_LOWER_BOUND = 4096; private static final int BUFFER_SIZE_LOWER_BOUND = 4096;
@ -57,7 +57,7 @@ class ConnectionHandler extends MessageWriteTarget {
} }
@Override @Override
void writeBytes(byte[] message) { public void writeBytes(byte[] message) throws IOException {
lock.lock(); lock.lock();
try { try {
if (channel.write(ByteBuffer.wrap(message)) != message.length) if (channel.write(ByteBuffer.wrap(message)) != message.length)
@ -65,13 +65,14 @@ class ConnectionHandler extends MessageWriteTarget {
} catch (IOException e) { } catch (IOException e) {
log.error("Error writing message to connection, closing connection", e); log.error("Error writing message to connection, closing connection", e);
closeConnection(); closeConnection();
throw e;
} finally { } finally {
lock.unlock(); lock.unlock();
} }
} }
@Override @Override
void closeConnection() { public void closeConnection() {
try { try {
channel.close(); channel.close();
} catch (IOException e) { } catch (IOException e) {

View File

@ -16,10 +16,12 @@
package com.google.bitcoin.protocols.niowrapper; package com.google.bitcoin.protocols.niowrapper;
import java.io.IOException;
/** /**
* A target to which messages can be written/connection can be closed * A target to which messages can be written/connection can be closed
*/ */
abstract class MessageWriteTarget { public interface MessageWriteTarget {
abstract void writeBytes(byte[] message); void writeBytes(byte[] message) throws IOException;
abstract void closeConnection(); void closeConnection();
} }

View File

@ -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. * 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 org.slf4j.Logger log = LoggerFactory.getLogger(NioClient.class);
private static final int BUFFER_SIZE_LOWER_BOUND = 4096; 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) // Writes raw bytes to the channel (used by the write method in StreamParser)
@Override @Override
synchronized void writeBytes(byte[] message) { public synchronized void writeBytes(byte[] message) {
try { try {
if (sc.write(ByteBuffer.wrap(message)) != message.length) if (sc.write(ByteBuffer.wrap(message)) != message.length)
throw new IOException("Couldn't write all of message to socket"); throw new IOException("Couldn't write all of message to socket");

View File

@ -20,6 +20,7 @@ import com.google.bitcoin.core.Utils;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite; import com.google.protobuf.MessageLite;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@ -201,7 +202,11 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
checkState(messageBytes.length <= maxMessageSize); checkState(messageBytes.length <= maxMessageSize);
byte[] messageLength = new byte[4]; byte[] messageLength = new byte[4];
Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0);
writeTarget.writeBytes(messageLength); try {
writeTarget.writeBytes(messageBytes); writeTarget.writeBytes(messageLength);
writeTarget.writeBytes(messageBytes);
} catch (IOException e) {
closeConnection();
}
} }
} }