From dfa921fa4104abcdd2221eda7e351f40b19ab364 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sun, 21 May 2023 20:50:26 +0100 Subject: [PATCH] Add single UDP send stand-alone mode in NetMarshalClient. Fix dsocket setting issue in NetMarshalClient. --- .../lib/calmnet/marshal/NetMarshalClient.java | 22 +++++++++++++++++- .../marshal/NetMarshalClientWrapped.java | 23 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java index d6d42f9..590b493 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClient.java @@ -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; diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java index 0d2894b..b763e68 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java @@ -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 inputStreamWrapper, Function outputStreamWrapper) { + super(socketIn, remoteAddress, remotePort, factory, loader, fragmentationOptions); + setupWrappers(inputStreamWrapper, outputStreamWrapper); + } + protected void setupWrappers(Function inputStreamWrapper, Function outputStreamWrapper) { wrapperInputStream = inputStreamWrapper; if (wrapperInputStream != null) inputStream = wrapperInputStream.apply(rootInputStream);