mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-20 02:09:29 +01:00
MessageWriteTarget->interface, writeBytes throws IOException
This commit is contained in:
parent
edd0a0907c
commit
cf23e18f23
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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<MessageType extends MessageLite> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user