Skip to content

Commit

Permalink
refactor: indicate client/server instance should be closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Zmax0 committed Jun 25, 2024
1 parent 702b0af commit 5fe4956
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion urban-spork-client/src/com/urbanspork/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -101,7 +102,8 @@ protected void initChannel(Channel ch) {
.bind(InetAddress.getLoopbackAddress(), config.getPort()).sync().channel();
}

public record Instance(ServerSocketChannel tcp, DatagramChannel udp, TrafficCounter traffic) {
public record Instance(ServerSocketChannel tcp, DatagramChannel udp, TrafficCounter traffic) implements Closeable {
@Override
public void close() {
traffic.stop();
tcp.close().awaitUninterruptibly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
if (cause.getCause() instanceof InvalidCipherTextException) {
logger.error("[{}][{}][{}] Invalid cipher text", transport, protocol, transLog);
} else {
String msg = String.format("[%s][%s][%s] Caught exception", transport, protocol, transLog);
logger.error(msg, cause);
logger.error("[{}][{}][{}] Caught exception", transport, protocol, transLog, cause);
}
ctx.close();
}
Expand Down
4 changes: 3 additions & 1 deletion urban-spork-server/src/com/urbanspork/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -127,7 +128,8 @@ protected void initChannel(Channel ch) {
}
}

public record Instance(ServerSocketChannel tcp, Optional<DatagramChannel> udp) {
public record Instance(ServerSocketChannel tcp, Optional<DatagramChannel> udp) implements Closeable {
@Override
public void close() {
tcp.close().awaitUninterruptibly();
udp.ifPresent(c -> c.close().awaitUninterruptibly());
Expand Down

0 comments on commit 5fe4956

Please sign in to comment.