Compare commits
No commits in common. "0f553c6a0e527166b367b380e777ace3fa728d26" and "790426eb3ebcad59c234f39c10535e0e6e93cbff" have entirely different histories.
0f553c6a0e
...
790426eb3e
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2023, Captain ALM
|
||||
Copyright (c) 2022, Captain ALM
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -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 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.
|
||||
Sending large amounts over UDP is also do-able using the fragmentation part of the library.
|
||||
|
||||
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
|
||||
|
@ -11,7 +11,10 @@ import com.captainalm.lib.calmnet.stream.NetworkOutputStream;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
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.InetAddress;
|
||||
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.
|
||||
* NOTE: Methods that are synchronised are used here, do NOT use instances of these classes as monitors.
|
||||
*
|
||||
* @author Captain ALM
|
||||
*/
|
||||
@ -36,7 +38,7 @@ public class NetMarshalClient implements Closeable {
|
||||
protected InputStream rootInputStream;
|
||||
protected OutputStream rootOutputStream;
|
||||
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 boolean disablePacketReading;
|
||||
|
||||
@ -50,7 +52,7 @@ public class NetMarshalClient implements Closeable {
|
||||
protected final Queue<IPacket> receivedPackets = new LinkedList<>();
|
||||
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 (remoteAddress == null) throw new NullPointerException(((isMulticast) ? "multicastGroupAddress" : "remoteAddress") + " is null");
|
||||
this.remoteAddress = remoteAddress;
|
||||
@ -275,7 +277,6 @@ public class NetMarshalClient implements Closeable {
|
||||
*/
|
||||
public void setPacketsShouldBeRead(boolean shouldRead) {
|
||||
synchronized (slockPacketRead) {
|
||||
if (receiveThread.isAlive()) receiveThread.interrupt();
|
||||
disablePacketReading = !shouldRead;
|
||||
if (!disablePacketReading) slockPacketRead.notify();
|
||||
}
|
||||
@ -318,22 +319,19 @@ public class NetMarshalClient implements Closeable {
|
||||
protected synchronized void sslUpgrade(SSLContext context, String remoteHostName) throws SSLUtilityException, IOException {
|
||||
if (!running || socket == null || socket instanceof SSLSocket) return;
|
||||
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");
|
||||
Socket originalSocket = socket;
|
||||
synchronized (originalSocket) {
|
||||
synchronized (socket) {
|
||||
Socket originalSocket = socket;
|
||||
try {
|
||||
socket = SSLUtilities.upgradeClientSocketToSSL(context, socket, remoteHostName, socket.getPort(), true, remoteHostName != null);
|
||||
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream) rootInputStream).setSocket(socket);
|
||||
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream) rootOutputStream).setSocket(socket);
|
||||
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream)rootInputStream).setSocket(socket);
|
||||
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream)rootOutputStream).setSocket(socket);
|
||||
} catch (SSLUtilityException | IOException e) {
|
||||
socket = originalSocket;
|
||||
try {
|
||||
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream) rootInputStream).setSocket(socket);
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
try {
|
||||
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream) rootOutputStream).setSocket(socket);
|
||||
if (rootInputStream instanceof NetworkInputStream) ((NetworkInputStream)rootInputStream).setSocket(socket);
|
||||
if (rootOutputStream instanceof NetworkOutputStream) ((NetworkOutputStream)rootOutputStream).setSocket(socket);
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
throw e;
|
||||
@ -354,7 +352,6 @@ public class NetMarshalClient implements Closeable {
|
||||
* Sets the {@link BiConsumer} receiver consumer.
|
||||
*
|
||||
* @param consumer The new receiver consumer.
|
||||
* @throws NullPointerException consumer is null.
|
||||
*/
|
||||
public void setReceiveBiConsumer(BiConsumer<IPacket, NetMarshalClient> consumer) {
|
||||
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() {
|
||||
return receiveExceptionBiConsumer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public static BiConsumer<IPacket, NetMarshalClient> getDummyReceiver() {
|
||||
return dummy;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -405,21 +391,16 @@ public class NetMarshalClient implements Closeable {
|
||||
while (disablePacketReading) slockPacketRead.wait();
|
||||
}
|
||||
IPacket packet = loader.readStreamedPacket(inputStream, factory, null);
|
||||
synchronized (slockPacketRead) {
|
||||
if (packet == null || !packet.isValid()) return;
|
||||
if (receiveBiConsumer != null) receiveBiConsumer.accept(packet, this);
|
||||
synchronized (slockReceive) {
|
||||
receivedPackets.add(packet);
|
||||
slockReceive.notify();
|
||||
}
|
||||
if (packet == null || !packet.isValid()) return;
|
||||
if (receiveBiConsumer != null) receiveBiConsumer.accept(packet, this);
|
||||
synchronized (slockReceive) {
|
||||
receivedPackets.add(packet);
|
||||
slockReceive.notify();
|
||||
}
|
||||
} catch (InterruptedException | InterruptedIOException e) {
|
||||
} catch (PacketException | IOException e) {
|
||||
if (receiveExceptionBiConsumer != null) receiveExceptionBiConsumer.accept(e, this);
|
||||
} catch (InterruptedException | PacketException | IOException e) {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException ex) {
|
||||
if (receiveExceptionBiConsumer != null) receiveExceptionBiConsumer.accept(ex, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user