@Override public synchronized void writeBytes(byte[] message) throws IOException { handler.writeTarget.writeBytes(message); } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { handler.writeTarget.writeBytes(message); } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { handler.writeTarget.writeBytes(message); } }
@Override public synchronized void writeBytes(byte[] message) throws IOException { handler.writeTarget.writeBytes(message); } }
/** * <p>Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.</p> * * <p>Provides a write-order guarantee.</p> * * @throws IllegalStateException If the encoded message is larger than the maximum message size. */ public void write(MessageType msg) throws IllegalStateException { byte[] messageBytes = msg.toByteArray(); checkState(messageBytes.length <= maxMessageSize); byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); try { MessageWriteTarget target = writeTarget.get(); target.writeBytes(messageLength); target.writeBytes(messageBytes); } catch (IOException e) { closeConnection(); } } }
/** * <p>Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.</p> * * <p>Provides a write-order guarantee.</p> * * @throws IllegalStateException If the encoded message is larger than the maximum message size. */ public void write(MessageType msg) throws IllegalStateException { byte[] messageBytes = msg.toByteArray(); checkState(messageBytes.length <= maxMessageSize); byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); try { MessageWriteTarget target = writeTarget.get(); target.writeBytes(messageLength); target.writeBytes(messageBytes); } catch (IOException e) { closeConnection(); } } }
/** * <p>Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.</p> * * <p>Provides a write-order guarantee.</p> * * @throws IllegalStateException If the encoded message is larger than the maximum message size. */ public void write(MessageType msg) throws IllegalStateException { byte[] messageBytes = msg.toByteArray(); checkState(messageBytes.length <= maxMessageSize); byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); try { MessageWriteTarget target = writeTarget.get(); target.writeBytes(messageLength); target.writeBytes(messageBytes); } catch (IOException e) { closeConnection(); } } }
/** * <p>Writes the given message to the other side of the connection, prefixing it with the proper 4-byte prefix.</p> * * <p>Provides a write-order guarantee.</p> * * @throws IllegalStateException If the encoded message is larger than the maximum message size. */ public void write(MessageType msg) throws IllegalStateException { byte[] messageBytes = msg.toByteArray(); checkState(messageBytes.length <= maxMessageSize); byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); try { MessageWriteTarget target = writeTarget.get(); target.writeBytes(messageLength); target.writeBytes(messageBytes); } catch (IOException e) { closeConnection(); } } }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
@Test public void disconnectOldVersions1() throws Exception { // Set up the connection with an old version. final SettableFuture<Void> connectedFuture = SettableFuture.create(); final SettableFuture<Void> disconnectedFuture = SettableFuture.create(); peer.addConnectedEventListener(new PeerConnectedEventListener() { @Override public void onPeerConnected(Peer peer, int peerCount) { connectedFuture.set(null); } }); peer.addDisconnectedEventListener(new PeerDisconnectedEventListener() { @Override public void onPeerDisconnected(Peer peer, int peerCount) { disconnectedFuture.set(null); } }); connectWithVersion(500, VersionMessage.NODE_NETWORK); // We must wait uninterruptibly here because connect[WithVersion] generates a peer that interrupts the current // thread when it disconnects. Uninterruptibles.getUninterruptibly(connectedFuture); Uninterruptibles.getUninterruptibly(disconnectedFuture); try { peer.writeTarget.writeBytes(new byte[1]); fail(); } catch (IOException e) { assertTrue((e.getCause() != null && e.getCause() instanceof CancelledKeyException) || (e instanceof SocketException && e.getMessage().equals("Socket is closed"))); } }
writeTarget.writeTarget.writeBytes(out.toByteArray()); try { result.get(); peer.writeTarget.writeBytes(new byte[1]); fail(); } catch (IOException e) {
byte[] messageLength = new byte[4]; Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0); client.writeBytes(new byte[]{messageLength[0], messageLength[1]}); Thread.sleep(10); client.writeBytes(new byte[]{messageLength[2], messageLength[3]}); Thread.sleep(10); client.writeBytes(new byte[]{messageBytes[0], messageBytes[1]}); Thread.sleep(10); client.writeBytes(Arrays.copyOfRange(messageBytes, 2, messageBytes.length - 1)); Thread.sleep(10); System.arraycopy(messageLength2, 0, sendBytes, 5 + messageBytes2.length, 4); System.arraycopy(messageBytes2, 0, sendBytes, 9 + messageBytes2.length, messageBytes2.length); client.writeBytes(sendBytes); assertEquals(msg, clientMessage1Received.get()); assertEquals(msg2, clientMessage2Received.get()); client.writeBytes(new byte[]{messageLength2[0], messageLength2[1]}); Thread.sleep(10); client.writeBytes(new byte[]{messageLength2[2], messageLength2[3]}); Thread.sleep(10); client.writeBytes(new byte[]{messageBytes2[0], messageBytes2[1]}); Thread.sleep(10); client.writeBytes(new byte[]{messageBytes2[2], messageBytes2[3]}); Thread.sleep(10); client.writeBytes(Arrays.copyOfRange(messageBytes2, 4, messageBytes2.length)); assertEquals(msg2, clientMessage4Received.get()); client.writeBytes(messageLength5);