Add single UDP send stand-alone mode in NetMarshalClient.

Fix dsocket setting issue in NetMarshalClient.
This commit is contained in:
Captain ALM 2023-05-21 20:50:26 +01:00
parent eb886b430d
commit dfa921fa41
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
2 changed files with 43 additions and 2 deletions

View File

@ -212,6 +212,7 @@ public class NetMarshalClient implements Closeable {
*/
public NetMarshalClient(MulticastSocket socketIn, InetAddress multicastGroupAddress, int multicastGroupPort, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions) throws IOException {
this(multicastGroupAddress, multicastGroupPort, factory, loader, true, socketIn == null, fragmentationOptions);
dsocket = socketIn;
socketIn.joinGroup(multicastGroupAddress);
NetworkOutputStream netOut = new NetworkOutputStream(socketIn, 65535);
netOut.setDatagramTarget(multicastGroupAddress, multicastGroupPort);
@ -219,7 +220,7 @@ public class NetMarshalClient implements Closeable {
}
/**
* Constructs a new NetMarshalClient with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link IPacketFactory}, {@link PacketLoader} and {@link FragmentationOptions}.
* Constructs a new NetMarshalClient with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link InputStream}, {@link IPacketFactory}, {@link PacketLoader} and {@link FragmentationOptions}.
*
* @param socketIn The datagram socket to use.
* @param remoteAddress The remote address to send data to.
@ -234,11 +235,30 @@ public class NetMarshalClient implements Closeable {
public NetMarshalClient(DatagramSocket socketIn, InetAddress remoteAddress, int remotePort, InputStream inputStream, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions) {
this(remoteAddress, remotePort, factory, loader, false, socketIn == null, fragmentationOptions);
if (inputStream == null) throw new NullPointerException("inputStream is null");
dsocket = socketIn;
setStreams(null, new NetworkOutputStream(socketIn, 65535, remoteAddress, remotePort));
rootInputStream = inputStream;
this.inputStream = inputStream;
}
/**
* Constructs a new NetMarshalClient with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link IPacketFactory}, {@link PacketLoader} and {@link FragmentationOptions}.
*
* @param socketIn The datagram socket to use.
* @param remoteAddress The remote address to send data to.
* @param remotePort The remote port to send data to.
* @param factory The packet factory to use.
* @param loader The loader to use.
* @param fragmentationOptions The fragmentation options, null to disable fragmentation.
* @throws NullPointerException socketIn, remoteAddress, factory or loader is null.
* @throws IllegalArgumentException remotePort is less than 0 or greater than 65535 or fragmentation options failed validation.
*/
public NetMarshalClient(DatagramSocket socketIn, InetAddress remoteAddress, int remotePort, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions) {
this(remoteAddress, remotePort, factory, loader, false, socketIn == null, fragmentationOptions);
dsocket = socketIn;
setStreams(new NetworkInputStream(socketIn), new NetworkOutputStream(socketIn, 65535, remoteAddress, remotePort));
}
protected void setStreams(InputStream inputStream, OutputStream outputStream) {
if (inputStream != null) rootInputStream = inputStream;
this.inputStream = rootInputStream;

View File

@ -66,7 +66,7 @@ public class NetMarshalClientWrapped extends NetMarshalClient {
}
/**
* Constructs a new NetMarshalClientWrapped with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link IPacketFactory},
* Constructs a new NetMarshalClientWrapped with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link InputStream}, {@link IPacketFactory},
* {@link PacketLoader}, {@link FragmentationOptions}, {@link Function} for wrapping the input stream and the {@link Function} for wrapping the output stream.
* Wrapped streams should close the underlying stream when closed.
*
@ -87,6 +87,27 @@ public class NetMarshalClientWrapped extends NetMarshalClient {
setupWrappers(inputStreamWrapper, outputStreamWrapper);
}
/**
* Constructs a new NetMarshalClientWrapped with the specified {@link DatagramSocket}, remote {@link InetAddress}, remote port, {@link IPacketFactory},
* {@link PacketLoader}, {@link FragmentationOptions}, {@link Function} for wrapping the input stream and the {@link Function} for wrapping the output stream.
* Wrapped streams should close the underlying stream when closed.
*
* @param socketIn The datagram socket to use.
* @param remoteAddress The remote address to send data to.
* @param remotePort The remote port to send data to.
* @param factory The packet factory to use.
* @param loader The loader to use.
* @param fragmentationOptions The fragmentation options, null to disable fragmentation.
* @param inputStreamWrapper The input stream wrapper to use (Can be null).
* @param outputStreamWrapper The output stream wrapper to use (Can be null).
* @throws NullPointerException socketIn, remoteAddress, factory or loader is null.
* @throws IllegalArgumentException remotePort is less than 0 or greater than 65535 or fragmentation options failed validation.
*/
public NetMarshalClientWrapped(DatagramSocket socketIn, InetAddress remoteAddress, int remotePort, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions, Function<InputStream, InputStream> inputStreamWrapper, Function<OutputStream, OutputStream> outputStreamWrapper) {
super(socketIn, remoteAddress, remotePort, factory, loader, fragmentationOptions);
setupWrappers(inputStreamWrapper, outputStreamWrapper);
}
protected void setupWrappers(Function<InputStream, InputStream> inputStreamWrapper, Function<OutputStream, OutputStream> outputStreamWrapper) {
wrapperInputStream = inputStreamWrapper;
if (wrapperInputStream != null) inputStream = wrapperInputStream.apply(rootInputStream);