/** * Method creates {@link DatagramPacket} from given {@link Packet} and sends it to address:port from destination. * @param destination instance of {@link NodeInfo} with net address for sending. * @param packet data to send. It's {@link Packet#makeByteArray()} should returns data with size less than {@link DatagramAdapter#MAX_PACKET_SIZE} */ private void sendPacket(NodeInfo destination, Packet packet) { byte[] payload = packet.makeByteArray(); DatagramPacket dp = new DatagramPacket(payload, payload.length, destination.getNodeAddress().getAddress(), destination.getNodeAddress().getPort()); try { report(logLabel, ()->"sendPacket datagram size: " + payload.length, VerboseLevel.DETAILED); if ((testMode == TestModes.LOST_PACKETS || testMode == TestModes.LOST_AND_SHUFFLE_PACKETS) && (new Random().nextInt(100) < lostPacketsPercent)) report(logLabel, ()->"test mode: skip socket.send", VerboseLevel.BASE); else socket.send(dp); } catch (Exception e) { callErrorCallbacks("sendPacket exception: " + e); } }
protected void sendBlock(UDPAdapter.Packet packet, DatagramSocket socket, NodeInfo destination) throws InterruptedException { byte[] out = packet.makeByteArray(); DatagramPacket dp = new DatagramPacket(out, out.length, destination.getNodeAddress().getAddress(), destination.getNodeAddress().getPort()); try { socket.send(dp); } catch (IOException e) { e.printStackTrace(); } }