2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.net;
|
2020-02-08 15:43:19 +01:00
|
|
|
|
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import com.google.common.annotations.Beta;
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import fr.pandacube.lib.util.Log;
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
@Beta
|
2020-02-08 15:43:19 +01:00
|
|
|
public class PServer extends Thread implements Closeable {
|
2022-07-10 00:55:56 +02:00
|
|
|
private static final AtomicInteger connectionCounterId = new AtomicInteger(0);
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final int port;
|
2020-02-08 15:43:19 +01:00
|
|
|
private ServerSocket socket;
|
2022-07-10 00:55:56 +02:00
|
|
|
private final String socketName;
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final List<TCPServerClientConnection> clients = Collections.synchronizedList(new ArrayList<>());
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final AtomicBoolean isClosed = new AtomicBoolean(false);
|
2020-02-08 15:43:19 +01:00
|
|
|
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final List<PPacketListener<PPacket>> globalPacketListeners = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
private final List<PSocketConnectionListener> clientConnectionListeners = Collections.synchronizedList(new ArrayList<>());
|
2020-02-08 15:43:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private final String password;
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
public PServer(int port, String sckName, String password) {
|
2020-02-08 15:43:19 +01:00
|
|
|
super("PServer " + sckName);
|
|
|
|
setDaemon(true);
|
|
|
|
if (port <= 0 || port > 65535) throw new IllegalArgumentException("le numéro de port est invalide");
|
|
|
|
socketName = sckName;
|
|
|
|
this.port = port;
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
socket = new ServerSocket();
|
2021-03-21 20:17:31 +01:00
|
|
|
socket.setReceiveBufferSize(PSocket.NETWORK_TCP_BUFFER_SIZE);
|
2020-02-08 15:43:19 +01:00
|
|
|
socket.setPerformancePreferences(0, 1, 0);
|
|
|
|
socket.bind(new InetSocketAddress(port));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Socket socketClient = socket.accept();
|
2021-03-21 20:17:31 +01:00
|
|
|
socketClient.setSendBufferSize(PSocket.NETWORK_TCP_BUFFER_SIZE);
|
|
|
|
socketClient.setSoTimeout(PSocket.NETWORK_TIMEOUT);
|
2020-02-08 15:43:19 +01:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
TCPServerClientConnection co = new TCPServerClientConnection(socketClient,
|
|
|
|
connectionCounterId.getAndIncrement());
|
|
|
|
co.start();
|
2020-02-08 15:43:19 +01:00
|
|
|
}
|
2022-07-10 00:55:56 +02:00
|
|
|
} catch (SocketException ignored) {
|
2020-02-08 15:43:19 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.warning("Plus aucune connexion ne peux être acceptée", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void addPacketListener(PPacketListener<PPacket> l) {
|
|
|
|
globalPacketListeners.add(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removePacketListener(PPacketListener<PPacket> l) {
|
|
|
|
return globalPacketListeners.remove(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addConnectionListener(PSocketConnectionListener l) {
|
|
|
|
clientConnectionListeners.add(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeConnectionListener(PSocketConnectionListener l) {
|
|
|
|
clientConnectionListeners.remove(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected class TCPServerClientConnection extends PSocket {
|
|
|
|
|
|
|
|
boolean loggedIn;
|
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
private TCPServerClientConnection(Socket s, int coId) {
|
2022-07-20 13:18:57 +02:00
|
|
|
super(s, "Conn#" + coId + " via PServer " + socketName, password);
|
2020-02-08 15:43:19 +01:00
|
|
|
addConnectionListener(new PSocketConnectionListener() {
|
|
|
|
@Override
|
|
|
|
public void onDisconnect(PSocket connection) {
|
|
|
|
try {
|
|
|
|
clientConnectionListeners.forEach(l -> l.onDisconnect(connection));
|
|
|
|
} finally {
|
|
|
|
clients.remove((TCPServerClientConnection)connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onConnect(PSocket connection) {
|
|
|
|
clients.add((TCPServerClientConnection)connection);
|
|
|
|
clientConnectionListeners.forEach(l -> l.onConnect(connection));
|
|
|
|
}
|
|
|
|
});
|
2022-07-10 00:55:56 +02:00
|
|
|
addPacketListener((conn, packet) ->
|
|
|
|
globalPacketListeners.forEach(l -> {
|
|
|
|
try {
|
|
|
|
l.onPacketReceive(conn, packet);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.severe("Exception while calling PPacketListener.onPacketReceive().", e);
|
|
|
|
sendSilently(PPacketAnswer.buildExceptionPacket(packet, e.toString()));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2020-02-08 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
try {
|
|
|
|
if (isClosed.get()) return;
|
|
|
|
isClosed.set(true);
|
|
|
|
|
|
|
|
clients.forEach(PSocket::close);
|
|
|
|
|
|
|
|
socket.close();
|
2022-07-10 00:55:56 +02:00
|
|
|
} catch (IOException ignored) {}
|
2020-02-08 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isClosed() {
|
|
|
|
return isClosed.get() || socket.isClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<PSocket> getClients() {
|
|
|
|
synchronized (clients) {
|
|
|
|
return new ArrayList<>(clients);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2022-07-20 13:18:57 +02:00
|
|
|
return this.getClass().getName() + "{thread=" + getName() + ", socket=" + socket + "}";
|
2020-02-08 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|