From aab4fdd8faa2689eb0b54488825fee3537e0e6fc Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 20 May 2023 15:25:57 +0100 Subject: [PATCH] Finish NetMarshalServerWrapped. --- .../marshal/NetMarshalClientWrapped.java | 2 + .../marshal/NetMarshalServerWrapped.java | 90 +++++++++++++++++++ .../lib/calmnet/marshal/package-info.java | 6 +- 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 src/com/captainalm/lib/calmnet/marshal/NetMarshalServerWrapped.java diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java index 069481a..0d2894b 100644 --- a/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalClientWrapped.java @@ -90,8 +90,10 @@ public class NetMarshalClientWrapped extends NetMarshalClient { protected void setupWrappers(Function inputStreamWrapper, Function outputStreamWrapper) { wrapperInputStream = inputStreamWrapper; if (wrapperInputStream != null) inputStream = wrapperInputStream.apply(rootInputStream); + if (inputStream == null) inputStream = rootInputStream; wrapperOutputStream = outputStreamWrapper; if (wrapperOutputStream != null) outputStream = wrapperOutputStream.apply(rootOutputStream); + if (outputStream == null) outputStream = rootOutputStream; } /** diff --git a/src/com/captainalm/lib/calmnet/marshal/NetMarshalServerWrapped.java b/src/com/captainalm/lib/calmnet/marshal/NetMarshalServerWrapped.java new file mode 100644 index 0000000..b87e8e9 --- /dev/null +++ b/src/com/captainalm/lib/calmnet/marshal/NetMarshalServerWrapped.java @@ -0,0 +1,90 @@ +package com.captainalm.lib.calmnet.marshal; + +import com.captainalm.lib.calmnet.packet.PacketLoader; +import com.captainalm.lib.calmnet.packet.factory.IPacketFactory; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PipedInputStream; +import java.net.DatagramSocket; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.function.Function; + +/** + * This class provides a way of networking on the server side and holds a collection of {@link NetMarshalClient}s, allows stream wrapping. + * Wrapped streams should close the underlying stream when closed. + * NOTE: Methods that are synchronised are used here, do NOT use instances of these classes as monitors. + * + * @author Captain ALM + */ +public class NetMarshalServerWrapped extends NetMarshalServer { + protected Function wrapperInputStream; + protected Function wrapperOutputStream; + + /** + * Constructs a new NetMarshalServerWrapped with the specified {@link ServerSocket}, {@link IPacketFactory}, {@link PacketLoader}, {@link FragmentationOptions}, + * {@link Function} for wrapping the input stream and the {@link Function} for wrapping the output stream. + * + * @param socketIn The server socket to use. + * @param factory The packet factory to use. + * @param loader The packet 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, factory or loader is null. + * @throws IllegalArgumentException Fragmentation options failed validation. + */ + public NetMarshalServerWrapped(ServerSocket socketIn, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions, Function inputStreamWrapper, Function outputStreamWrapper) { + super(socketIn, factory, loader, fragmentationOptions); + wrapperInputStream = inputStreamWrapper; + wrapperOutputStream = outputStreamWrapper; + } + + /** + * Constructs a new NetMarshalServerWrapped with the specified {@link DatagramSocket}, {@link IPacketFactory}, {@link PacketLoader}, {@link FragmentationOptions}, + * {@link Function} for wrapping the input stream and the {@link Function} for wrapping the output stream. + * + * @param socketIn The datagram socket to use. + * @param factory The packet factory to use. + * @param loader The packet 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, factory or loader is null. + * @throws IllegalArgumentException Fragmentation options failed validation. + */ + public NetMarshalServerWrapped(DatagramSocket socketIn, IPacketFactory factory, PacketLoader loader, FragmentationOptions fragmentationOptions, Function inputStreamWrapper, Function outputStreamWrapper) { + super(socketIn, factory, loader, fragmentationOptions); + wrapperInputStream = inputStreamWrapper; + wrapperOutputStream = outputStreamWrapper; + } + + /** + * Gets the {@link Function} input stream wrapper function. + * + * @return The input stream wrapper function or null. + */ + public Function getWrapperInputStream() { + return wrapperInputStream; + } + + /** + * Gets the {@link Function} output stream wrapper function. + * + * @return The output stream wrapper function or null. + */ + public Function getWrapperOutputStream() { + return wrapperOutputStream; + } + + @Override + protected NetMarshalClient generateClientSocket(Socket socketIn) { + return new NetMarshalClientWrapped(socketIn, factory, loader, fragmentationOptions, wrapperInputStream, wrapperOutputStream); + } + + @Override + protected NetMarshalClient generateClientDSocket(CandidateClient candidate, PipedInputStream inputStream) { + return new NetMarshalClientWrapped(dsocket, candidate.address, candidate.port, inputStream, factory, loader, fragmentationOptions, wrapperInputStream, wrapperOutputStream); + } +} diff --git a/src/com/captainalm/lib/calmnet/marshal/package-info.java b/src/com/captainalm/lib/calmnet/marshal/package-info.java index 2bace37..b44ce4a 100644 --- a/src/com/captainalm/lib/calmnet/marshal/package-info.java +++ b/src/com/captainalm/lib/calmnet/marshal/package-info.java @@ -3,8 +3,4 @@ * * @author Captain ALM */ -package com.captainalm.lib.calmnet.marshal; -/*TODO: -NetMarshalServer - Has a thread for UDP receiving and has a dictionary of input streams (final, created in constructor) -NetMarshalServerWrapped - Constructs NetMarshalClientWrapped instead of NetMarshalClient, stream wrapping support - */ \ No newline at end of file +package com.captainalm.lib.calmnet.marshal; \ No newline at end of file