Skip to content

Commit

Permalink
More shutdown-related cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Nov 28, 2021
1 parent afbf44a commit a95a51a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public final class ClientSocketManager implements SocketManager {
private static final @NotNull Logger logger = Logger.getLogger("CC-ClientSocket");
private int sleep = 1;
private boolean connected = false;
private volatile boolean disconnectMessageSent = false;

/**
* Creates a new client-side socket manager. This is intended only for use by the library.
Expand Down Expand Up @@ -86,16 +85,11 @@ private void loop() {

@Override
public void shutdown(@Nullable Request cause, @Nullable String reason) throws IOException {
if (!disconnectMessageSent) {
disconnectMessageSent = true;
DummyResponse.from(cause, reason).write(socket);
}
rawShutdown();
}

private void rawShutdown() throws IOException {
if (!running) return;
running = false;
if (socket != null && !socket.isClosed())
if (socket != null && !socket.isClosed()) {
DummyResponse.from(cause, reason).write(socket);
socket.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void loop() {

@Override
public void shutdown(@Nullable Request cause, @Nullable String reason) throws IOException {
if (!running) return;
running = false;
for (SocketThread socketThread : socketThreads) {
if (socketThread.isSocketActive())
Expand Down
26 changes: 10 additions & 16 deletions src/main/java/dev/qixils/crowdcontrol/socket/SocketThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* Handles the connection to a Crowd Control client when operating in server mode.
*/
final class SocketThread extends Thread implements SocketManager {
private static final String RAW_PASSWORD_REQUEST;
private static final byte[] PASSWORD_REQUEST;
private static final @NotNull String RAW_PASSWORD_REQUEST;
private static final byte @NotNull[] PASSWORD_REQUEST;
static {
DummyResponse resp = new DummyResponse();
resp.type = PacketType.LOGIN;
Expand All @@ -30,12 +30,11 @@ final class SocketThread extends Thread implements SocketManager {
PASSWORD_REQUEST = Arrays.copyOf(json, json.length+1);
}

private static final Logger logger = Logger.getLogger("CC-SocketThread");
final ServerSocketManager socketManager;
final Socket socket;
final String displayName = UUID.randomUUID().toString().substring(30).toUpperCase(Locale.ENGLISH);
private static final @NotNull Logger logger = Logger.getLogger("CC-SocketThread");
final @NotNull ServerSocketManager socketManager;
final @NotNull Socket socket;
final @NotNull String displayName = UUID.randomUUID().toString().substring(30).toUpperCase(Locale.ENGLISH);
private volatile boolean running = true;
private volatile boolean disconnectMessageSent = false;

SocketThread(@NotNull ServerSocketManager socketManager, @NotNull Socket clientSocket) {
this.socketManager = Objects.requireNonNull(socketManager, "socketManager cannot be null");
Expand Down Expand Up @@ -88,16 +87,11 @@ public boolean isSocketClosed() {

@Override
public void shutdown(@Nullable Request cause, @Nullable String reason) throws IOException {
if (!disconnectMessageSent) {
disconnectMessageSent = true;
DummyResponse.from(cause, reason).write(socket);
}
rawShutdown();
}

private void rawShutdown() throws IOException {
if (!running) return;
running = false;
if (!socket.isClosed())
if (!socket.isClosed()) {
DummyResponse.from(cause, reason).write(socket);
socket.close();
}
}
}

0 comments on commit a95a51a

Please sign in to comment.