| | |
| | | public ReplicationMsg receive() throws IOException, |
| | | DataFormatException, NotSupportedOldVersionPDUException |
| | | { |
| | | return receive(false); |
| | | } |
| | | |
| | | /** |
| | | * Receives the first message of a new connection, distinguishing a disconnect before |
| | | * any frame bytes arrive from a frame interrupted during transmission. |
| | | * |
| | | * @return the first replication message |
| | | * @throws EarlyPeerDisconnectException if the stream ends or the socket fails before the first byte |
| | | * @throws IOException if another I/O error occurs |
| | | * @throws DataFormatException if the frame is malformed |
| | | * @throws NotSupportedOldVersionPDUException if the message uses an unsupported old protocol |
| | | */ |
| | | public ReplicationMsg receiveFirstMessage() throws IOException, |
| | | DataFormatException, NotSupportedOldVersionPDUException |
| | | { |
| | | return receive(true); |
| | | } |
| | | |
| | | /** The peer disconnected before sending any bytes of its first replication frame. */ |
| | | public static final class EarlyPeerDisconnectException extends IOException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private EarlyPeerDisconnectException(IOException cause) |
| | | { |
| | | super(cause.getMessage(), cause); |
| | | } |
| | | } |
| | | |
| | | private ReplicationMsg receive(boolean firstMessage) |
| | | throws IOException, DataFormatException, NotSupportedOldVersionPDUException |
| | | { |
| | | try |
| | | { |
| | | /* |
| | |
| | | lastReceiveTime = System.currentTimeMillis(); |
| | | |
| | | // Read the first 8 bytes containing the packet length. |
| | | read(rcvLengthBuf); |
| | | read(rcvLengthBuf, firstMessage); |
| | | final int totalLength = Integer.parseInt(new String(rcvLengthBuf), 16); |
| | | |
| | | try |
| | | { |
| | | final byte[] buffer = new byte[totalLength]; |
| | | read(buffer); |
| | | read(buffer, false); |
| | | |
| | | /* |
| | | * We do not want the heartbeat to close the session when we are |
| | |
| | | } |
| | | } |
| | | |
| | | private void read(byte[] buffer) throws IOException |
| | | private void read(byte[] buffer, boolean firstFrameHeader) throws IOException |
| | | { |
| | | final int totalLength = buffer.length; |
| | | int length = 0; |
| | | while (length < totalLength) |
| | | { |
| | | final int read = input.read(buffer, length, totalLength - length); |
| | | if (read == -1) |
| | | try |
| | | { |
| | | lastReceiveTime = 0; |
| | | throw new EOFException("no more data"); |
| | | final int read = input.read(buffer, length, totalLength - length); |
| | | if (read == -1) |
| | | { |
| | | lastReceiveTime = 0; |
| | | throw new EOFException("no more data"); |
| | | } |
| | | length += read; |
| | | } |
| | | length += read; |
| | | catch (EOFException | SocketException e) |
| | | { |
| | | if (firstFrameHeader && length == 0) |
| | | { |
| | | throw new EarlyPeerDisconnectException(e); |
| | | } |
| | | throw e; |
| | | } |
| | | } |
| | | } |
| | | |