@Override public CustomMessage send(InetAddress server, int port, CustomMessage request) { try (Socket socket = new Socket(server, port)) { socket.setSoTimeout(Connection.READ_TIMEOUT); new NetworkMessage(request).write(socket.getOutputStream()); NetworkMessage networkMessage = Factory.getNetworkMessage(3, socket.getInputStream()); if (networkMessage != null && networkMessage.getPayload() instanceof CustomMessage) { return (CustomMessage) networkMessage.getPayload(); } else { if (networkMessage == null) { throw new NodeException("No response from node " + server); } else { throw new NodeException("Unexpected response from node " + server + ": " + networkMessage.getPayload().getCommand()); } } } catch (IOException e) { throw new NodeException(e.getMessage(), e); } }
/** * For improved memory efficiency, you should use {@link #writeHeaderAndGetPayloadBuffer(ByteBuffer)} * and write the header buffer as well as the returned payload buffer into the channel. * * @param buffer where everything gets written to. Needs to be large enough for the whole message * to be written. */ @Override public void write(ByteBuffer buffer) { byte[] payloadBytes = writeHeader(buffer); buffer.put(payloadBytes); }
public void updateWriter() { if (!headerOut.hasRemaining() && !sendingQueue.isEmpty()) { headerOut.clear(); MessagePayload payload = sendingQueue.poll(); payloadOut = new NetworkMessage(payload).writeHeaderAndGetPayloadBuffer(headerOut); headerOut.flip(); lastUpdate = System.currentTimeMillis(); } }
@Override protected void send(MessagePayload payload) { try { if (payload instanceof GetData) { requestedObjects.addAll(((GetData) payload).getInventory()); } synchronized (this) { new NetworkMessage(payload).write(out); } } catch (IOException e) { LOG.error(e.getMessage(), e); disconnect(); } }
channel.configureBlocking(true); ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_SIZE); ByteBuffer payloadBuffer = new NetworkMessage(request).writeHeaderAndGetPayloadBuffer(headerBuffer); headerBuffer.flip(); while (headerBuffer.hasRemaining()) { if (networkMessage != null && networkMessage.getPayload() instanceof CustomMessage) { return (CustomMessage) networkMessage.getPayload(); } else { if (networkMessage == null || networkMessage.getPayload() == null) { throw new NodeException("Empty response from node " + server); } else { throw new NodeException("Unexpected response from node " + server + ": " + networkMessage.getPayload().getClass());
public static NetworkMessage read(InputStream in) throws IOException { findMagic(in); String command = getCommand(in); int length = (int) Decode.uint32(in); byte[] checksum = Decode.bytes(in, 4); byte[] payloadBytes = Decode.bytes(in, length); if (testChecksum(checksum, payloadBytes)) { MessagePayload payload = getPayload(command, new ByteArrayInputStream(payloadBytes), length); if (payload != null) return new NetworkMessage(payload); else return null; } else { throw new IOException("Checksum failed for message '" + command + "'"); } }
out.write(getChecksum(payloadBytes)); } catch (GeneralSecurityException e) { throw new RuntimeException(e);
public void updateReader() { reader.update(); if (!reader.getMessages().isEmpty()) { Iterator<NetworkMessage> iterator = reader.getMessages().iterator(); NetworkMessage msg = null; while (iterator.hasNext()) { msg = iterator.next(); handleMessage(msg.getPayload()); iterator.remove(); } syncFinished = syncFinished(msg); } lastUpdate = System.currentTimeMillis(); }
length); if (payload != null) { messages.add(new NetworkMessage(payload));
@Override public void write(OutputStream out) throws IOException { // magic Encode.int32(MAGIC, out); // ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected) String command = payload.getCommand().name().toLowerCase(); out.write(command.getBytes("ASCII")); for (int i = command.length(); i < 12; i++) { out.write('\0'); } byte[] payloadBytes = Encode.bytes(payload); // Length of payload in number of bytes. Because of other restrictions, there is no reason why this length would // ever be larger than 1600003 bytes. Some clients include a sanity-check to avoid processing messages which are // larger than this. Encode.int32(payloadBytes.length, out); // checksum try { out.write(getChecksum(payloadBytes)); } catch (GeneralSecurityException e) { throw new ApplicationException(e); } // message payload out.write(payloadBytes); }
private void receive() throws InterruptedException { try { NetworkMessage msg = Factory.getNetworkMessage(version, in); if (msg == null) return; handleMessage(msg.getPayload()); if (socket.isClosed() || syncFinished(msg) || checkOpenRequests()) disconnect(); } catch (SocketTimeoutException ignore) { if (state == ACTIVE && syncFinished(null)) disconnect(); } }
public static NetworkMessage read(InputStream in) throws IOException { findMagic(in); String command = getCommand(in); int length = (int) Decode.uint32(in); if (length > 1600003) { throw new NodeException("Payload of " + length + " bytes received, no more than 1600003 was expected."); } byte[] checksum = Decode.bytes(in, 4); byte[] payloadBytes = Decode.bytes(in, length); if (testChecksum(checksum, payloadBytes)) { MessagePayload payload = getPayload(command, new ByteArrayInputStream(payloadBytes), length); if (payload != null) return new NetworkMessage(payload); else return null; } else { throw new IOException("Checksum failed for message '" + command + "'"); } }
out.put(getChecksum(payloadBytes)); } catch (GeneralSecurityException e) { throw new ApplicationException(e);
/** * A more efficient implementation of the write method, writing header data to the provided buffer and returning * a new buffer containing the payload. * * @param headerBuffer where the header data is written to (24 bytes) * @return a buffer containing the payload, ready to be read. */ public ByteBuffer writeHeaderAndGetPayloadBuffer(ByteBuffer headerBuffer) { return ByteBuffer.wrap(writeHeader(headerBuffer)); }