Add new methods to NetMarshalClient.
Add new datagram packet reading methods to NetworkInputStream.
Fix up Javadoc.
This commit is contained in:
Captain ALM 2023-05-20 00:18:59 +01:00
parent 41ed32f3af
commit b9640efde9
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
3 changed files with 123 additions and 1 deletions

View File

@ -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
*/

View File

@ -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.
*

View File

@ -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.