From b9640efde986489cc898f640e0ca92b3647994e1 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 20 May 2023 00:18:59 +0100 Subject: [PATCH] Update: Add new methods to NetMarshalClient. Add new datagram packet reading methods to NetworkInputStream. Fix up Javadoc. --- .../calmnet/marshal/FragmentationOptions.java | 2 +- .../lib/calmnet/marshal/NetMarshalClient.java | 51 +++++++++++++ .../calmnet/stream/NetworkInputStream.java | 71 +++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/com/captainalm/lib/calmnet/marshal/FragmentationOptions.java b/src/com/captainalm/lib/calmnet/marshal/FragmentationOptions.java index d5036f1..a7b2ddc 100644 --- a/src/com/captainalm/lib/calmnet/marshal/FragmentationOptions.java +++ b/src/com/captainalm/lib/calmnet/marshal/FragmentationOptions.java @@ -5,7 +5,7 @@ import com.captainalm.lib.calmnet.packet.fragment.FragmentSender; /** * This class provides fragmentation options for using {@link FragmentSender}s and - * {@link FragmentReceiver}s in this package. + * {@link FragmentReceiver}s in this package. * * @author Captain ALM */ diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java index b51bd0e..58eb13c 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java @@ -294,6 +294,34 @@ public class NetMarshalClient implements Closeable { return rootOutputStream; } + /** + * Gets the packet factory in use. + * + * @return The packet factory. + */ + public IPacketFactory getPacketFactory() { + return factory; + } + + /** + * Gets the packet loader in use. + * + * @return The packet loader. + */ + public PacketLoader getPacketLoader() { + return loader; + } + + /** + * Clears the fragment storage registries if fragmentation is enabled. + * WARNING: Use of this method is not recommended. + */ + public synchronized final void clearFragmentStorage() { + if (fragmentationOptions == null) return; + fragmentReceiver.clearRegistry(); + fragmentSender.clearRegistry(); + } + /** * Get the local {@link InetAddress}. * @@ -379,6 +407,18 @@ public class NetMarshalClient implements Closeable { } } + /** + * Flushes the output streams. + * + * @throws IOException A stream exception has occurred. + */ + public synchronized final void flush() throws IOException { + synchronized ((socket == null) ? dsocket : socket) { + outputStream.flush(); + rootOutputStream.flush(); + } + } + /** * Gets if there are received {@link IPacket}s. * @@ -403,6 +443,17 @@ public class NetMarshalClient implements Closeable { } } + /** + * Receives a {@link IPacket} polled. + * + * @return The received packet. + */ + public IPacket receivePacketPolled() { + synchronized (slockReceive) { + return receivedPackets.poll(); + } + } + /** * Are {@link IPacket}s being read. * diff --git a/src/com/captainalm/lib/calmnet/stream/NetworkInputStream.java b/src/com/captainalm/lib/calmnet/stream/NetworkInputStream.java index 2c0a47b..7ac7c19 100644 --- a/src/com/captainalm/lib/calmnet/stream/NetworkInputStream.java +++ b/src/com/captainalm/lib/calmnet/stream/NetworkInputStream.java @@ -86,6 +86,77 @@ public class NetworkInputStream extends InputStream { } } + /** + * Reads a single datagram packet. + * + * @return A byte array of the single datagram packet. + * @throws IOException if an I/O error occurs, stream closed or not using a datagram socket. + */ + public byte[] readPacket() throws IOException { + if (closed) throw new IOException("stream closed"); + if (dsocket == null) throw new IOException("not using a datagram socket"); + assureDSocketPacket(); + if (dsocketPacket == null) { + return new byte[0]; + } else { + byte[] toret = new byte[dlen-dsocketPacketIndex]; + System.arraycopy(dsocketPacket.getData(), dsocketPacketIndex, toret, 0, toret.length); + dsocketPacket = null; + return toret; + } + } + + /** + * Reads a single datagram packet into the specified buffer storing with no offset. + * + * @param b The buffer to store the packet in. + * @return The number of bytes stored or -1 for end of stream. + * @throws NullPointerException b is null. + * @throws IOException if an I/O error occurs, stream closed or not using a datagram socket. + */ + public int readPacket(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + /** + * Reads a single datagram packet into the specified buffer + * storing from the specified offset and the specified number of bytes. + * + * @param b The buffer to store the packet in. + * @param off The offset to store in the buffer from. + * @param len The number of bytes to store in the buffer. + * @return The number of bytes stored or -1 for end of stream. + * @throws NullPointerException b is null. + * @throws IndexOutOfBoundsException if off is negative, len is negative, or len is greater than the difference of the length of the buffer and off. + * @throws IOException if an I/O error occurs, stream closed or not using a datagram socket. + */ + public int readPacket(byte[] b, int off, int len) throws IOException { + if (closed) throw new IOException("stream closed"); + if (dsocket == null) throw new IOException("not using a datagram socket"); + if (b == null) throw new NullPointerException(); + if (off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException(); + if (len == 0) return 0; + assureDSocketPacket(); + if (dsocketPacket == null) return -1; + int rlen = Math.min(len, dlen-dsocketPacketIndex); + System.arraycopy(dsocketPacket.getData(), dsocketPacketIndex, b, off, rlen); + dsocketPacketIndex += rlen; + if (dsocketPacketIndex >= dlen) dsocketPacket = null; + return rlen; + } + + /** + * Gets the current datagram packet size. + * + * @return The datagram packet size. + * @throws IOException stream closed or not using a datagram socket. + */ + public int getPacketSize() throws IOException { + if (closed) throw new IOException("stream closed"); + if (dsocket == null) throw new IOException("not using a datagram socket"); + return dlen; + } + /** * Gets the current {@link InetAddress} of the stream. * Can be null.