Compare commits

..

No commits in common. "0f553c6a0e527166b367b380e777ace3fa728d26" and "790426eb3ebcad59c234f39c10535e0e6e93cbff" have entirely different histories.

3 changed files with 26 additions and 45 deletions

View File

@ -1,6 +1,6 @@
BSD 3-Clause License BSD 3-Clause License
Copyright (c) 2023, Captain ALM Copyright (c) 2022, Captain ALM
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without

View File

@ -2,11 +2,11 @@
This is the java network library with support for SSL, standard cipher encryption (.net Compatible!), fragmentation, packet object and network stream support. This is the java network library with support for SSL, standard cipher encryption (.net Compatible!), fragmentation, packet object and network stream support.
This requires the [calmstdcrypt library](https://code.mrmelon54.com/alfred/calmstdcrypt), which is my cryptographic wrapper library. This requires the [calmstdcrypt library](https://code.mrmelon54.xyz/alfred/calmstdcrypt), which is my cryptographic wrapper library.
This library contains streams that can wrap both Socket and DatagramSocket objects. This library contains streams that can wrap both Socket and DatagramSocket objects.
Sending large amounts over UDP is also do-able using the fragmentation part of the library. Sending large amounts over UDP is also do-able using the fragmentation part of the library.
This library targets Java 8. This library targets Java 8.
(C) Captain ALM 2023 - Under the BSD 3-Clause License (C) Captain ALM 2022 - Under the BSD 3-Clause License

View File

@ -11,7 +11,10 @@ import com.captainalm.lib.calmnet.stream.NetworkOutputStream;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import java.io.*; import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
@ -22,7 +25,6 @@ import java.util.function.BiConsumer;
/** /**
* This class provides a managed way of networking on the client side. * This class provides a managed way of networking on the client side.
* NOTE: Methods that are synchronised are used here, do NOT use instances of these classes as monitors.
* *
* @author Captain ALM * @author Captain ALM
*/ */
@ -36,7 +38,7 @@ public class NetMarshalClient implements Closeable {
protected InputStream rootInputStream; protected InputStream rootInputStream;
protected OutputStream rootOutputStream; protected OutputStream rootOutputStream;
protected BiConsumer<IPacket, NetMarshalClient> receiveBiConsumer; protected BiConsumer<IPacket, NetMarshalClient> receiveBiConsumer;
protected BiConsumer<Exception, NetMarshalClient> receiveExceptionBiConsumer; protected static final BiConsumer<IPacket, NetMarshalClient> dummy = (packet, netMarshalClient) -> {};
protected final Object slockPacketRead = new Object(); protected final Object slockPacketRead = new Object();
protected boolean disablePacketReading; protected boolean disablePacketReading;
@ -50,7 +52,7 @@ public class NetMarshalClient implements Closeable {
protected final Queue<IPacket> receivedPackets = new LinkedList<>(); protected final Queue<IPacket> receivedPackets = new LinkedList<>();
protected final Object slockReceive = new Object(); protected final Object slockReceive = new Object();
protected NetMarshalClient(InetAddress remoteAddress, int remotePort, IPacketFactory factory, PacketLoader loader, boolean isMulticast, boolean isSocketNull) { private NetMarshalClient(InetAddress remoteAddress, int remotePort, IPacketFactory factory, PacketLoader loader, boolean isMulticast, boolean isSocketNull) {
if (isSocketNull) throw new NullPointerException("socketIn is null"); if (isSocketNull) throw new NullPointerException("socketIn is null");
if (remoteAddress == null) throw new NullPointerException(((isMulticast) ? "multicastGroupAddress" : "remoteAddress") + " is null"); if (remoteAddress == null) throw new NullPointerException(((isMulticast) ? "multicastGroupAddress" : "remoteAddress") + " is null");
this.remoteAddress = remoteAddress; this.remoteAddress = remoteAddress;
@ -275,7 +277,6 @@ public class NetMarshalClient implements Closeable {
*/ */
public void setPacketsShouldBeRead(boolean shouldRead) { public void setPacketsShouldBeRead(boolean shouldRead) {
synchronized (slockPacketRead) { synchronized (slockPacketRead) {
if (receiveThread.isAlive()) receiveThread.interrupt();
disablePacketReading = !shouldRead; disablePacketReading = !shouldRead;
if (!disablePacketReading) slockPacketRead.notify(); if (!disablePacketReading) slockPacketRead.notify();
} }
@ -318,22 +319,19 @@ public class NetMarshalClient implements Closeable {
protected synchronized void sslUpgrade(SSLContext context, String remoteHostName) throws SSLUtilityException, IOException { protected synchronized void sslUpgrade(SSLContext context, String remoteHostName) throws SSLUtilityException, IOException {
if (!running || socket == null || socket instanceof SSLSocket) return; if (!running || socket == null || socket instanceof SSLSocket) return;
if (context == null) throw new NullPointerException("context is null"); if (context == null) throw new NullPointerException("context is null");
if (!disablePacketReading && Thread.currentThread() != receiveThread) throw new IllegalStateException("sslUpgrade methods should be called in a BiConsumer (for setReceiveBiConsumer) within the target NetMarshalClient" + if (Thread.currentThread() != receiveThread || !disablePacketReading) throw new IllegalStateException("sslUpgrade methods should be called in a BiConsumer (for setReceiveBiConsumer) within the target NetMarshalClient" +
" or when reading packets (arePacketsBeingRead) is disabled on the NetMarshalClient"); " or when reading packets (arePacketsBeingRead) is disabled on the NetMarshalClient");
Socket originalSocket = socket; synchronized (socket) {
synchronized (originalSocket) { Socket originalSocket = socket;
try { try {
socket = SSLUtilities.upgradeClientSocketToSSL(context, socket, remoteHostName, socket.getPort(), true, remoteHostName != null); socket = SSLUtilities.upgradeClientSocketToSSL(context, socket, remoteHostName, socket.getPort(), true, remoteHostName != null);
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream) rootInputStream).setSocket(socket); if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream)rootInputStream).setSocket(socket);
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream) rootOutputStream).setSocket(socket); if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream)rootOutputStream).setSocket(socket);
} catch (SSLUtilityException | IOException e) { } catch (SSLUtilityException | IOException e) {
socket = originalSocket; socket = originalSocket;
try { try {
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream) rootInputStream).setSocket(socket); if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream)rootInputStream).setSocket(socket);
} catch (IOException ex) { if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream)rootOutputStream).setSocket(socket);
}
try {
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream) rootOutputStream).setSocket(socket);
} catch (IOException ex) { } catch (IOException ex) {
} }
throw e; throw e;
@ -354,7 +352,6 @@ public class NetMarshalClient implements Closeable {
* Sets the {@link BiConsumer} receiver consumer. * Sets the {@link BiConsumer} receiver consumer.
* *
* @param consumer The new receiver consumer. * @param consumer The new receiver consumer.
* @throws NullPointerException consumer is null.
*/ */
public void setReceiveBiConsumer(BiConsumer<IPacket, NetMarshalClient> consumer) { public void setReceiveBiConsumer(BiConsumer<IPacket, NetMarshalClient> consumer) {
if (consumer == null) throw new NullPointerException("consumer is null"); if (consumer == null) throw new NullPointerException("consumer is null");
@ -362,23 +359,12 @@ public class NetMarshalClient implements Closeable {
} }
/** /**
* Gets the {@link BiConsumer} receive exception consumer. * Gets a dummy receiver consumer that does nothing.
* *
* @return The exception consumer or null. * @return The dummy receiver consumer.
*/ */
public BiConsumer<Exception, NetMarshalClient> getReceiveExceptionBiConsumer() { public static BiConsumer<IPacket, NetMarshalClient> getDummyReceiver() {
return receiveExceptionBiConsumer; return dummy;
}
/**
* Sets the {@link BiConsumer} receive exception consumer.
*
* @param consumer The new exception consumer.
* @throws NullPointerException consumer is null.
*/
public void setReceiveExceptionBiConsumer(BiConsumer<Exception, NetMarshalClient> consumer) {
if (consumer == null) throw new NullPointerException("consumer is null");
receiveExceptionBiConsumer = consumer;
} }
/** /**
@ -405,21 +391,16 @@ public class NetMarshalClient implements Closeable {
while (disablePacketReading) slockPacketRead.wait(); while (disablePacketReading) slockPacketRead.wait();
} }
IPacket packet = loader.readStreamedPacket(inputStream, factory, null); IPacket packet = loader.readStreamedPacket(inputStream, factory, null);
synchronized (slockPacketRead) { if (packet == null || !packet.isValid()) return;
if (packet == null || !packet.isValid()) return; if (receiveBiConsumer != null) receiveBiConsumer.accept(packet, this);
if (receiveBiConsumer != null) receiveBiConsumer.accept(packet, this); synchronized (slockReceive) {
synchronized (slockReceive) { receivedPackets.add(packet);
receivedPackets.add(packet); slockReceive.notify();
slockReceive.notify();
}
} }
} catch (InterruptedException | InterruptedIOException e) { } catch (InterruptedException | PacketException | IOException e) {
} catch (PacketException | IOException e) {
if (receiveExceptionBiConsumer != null) receiveExceptionBiConsumer.accept(e, this);
try { try {
close(); close();
} catch (IOException ex) { } catch (IOException ex) {
if (receiveExceptionBiConsumer != null) receiveExceptionBiConsumer.accept(ex, this);
} }
} }
} }