Skip to content

Commit

Permalink
Fixes when some part of the connection creation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Mar 11, 2024
1 parent bf1572e commit c9daaa8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-codec-native-quic</artifactId>
<version>0.0.57.Final</version>
<version>0.0.59.Final</version>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.nethergames.proxytransport.integration;

import dev.waterdog.waterdogpe.network.connection.client.ClientConnection;
import dev.waterdog.waterdogpe.network.connection.handler.ReconnectReason;
import dev.waterdog.waterdogpe.player.ProxiedPlayer;
import dev.waterdog.waterdogpe.utils.types.TranslationContainer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

Expand All @@ -27,9 +29,14 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
return;
}

this.player.getLogger().warning("[" + connection.getSocketAddress() + "|" + this.player.getName() + "] - Exception in connection caught", cause);
this.player.onDownstreamTimeout(this.connection.getServerInfo());

this.player.getLogger().warning("[" + connection.getSocketAddress() + "|" + this.player.getName() + "] - exception caught", cause);
this.connection.disconnect();

TranslationContainer msg = new TranslationContainer("waterdog.downstream.down", this.connection.getServerInfo().getServerName(), cause.getMessage());
if (this.player.sendToFallback(this.connection.getServerInfo(), ReconnectReason.EXCEPTION, cause.getMessage())) {
this.player.sendMessage(msg);
} else {
this.player.disconnect(msg);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nethergames.proxytransport.integration;

import dev.waterdog.waterdogpe.logger.MainLogger;
import dev.waterdog.waterdogpe.network.connection.client.ClientConnection;
import dev.waterdog.waterdogpe.network.serverinfo.ServerInfo;
import dev.waterdog.waterdogpe.network.serverinfo.ServerInfoType;
Expand Down Expand Up @@ -51,8 +52,9 @@ public Future<ClientConnection> createConnection(ProxiedPlayer proxiedPlayer) {
EventLoop eventLoop = proxiedPlayer.getProxy().getWorkerEventLoopGroup().next();
Promise<ClientConnection> promise = eventLoop.newPromise();

this.createServerConnection(eventLoop, this.getAddress()).addListener((Future<QuicChannel> future) -> {
this.createServerConnection(eventLoop, proxiedPlayer.getLogger(), this.getAddress()).addListener((Future<QuicChannel> future) -> {
if (future.isSuccess()) {
proxiedPlayer.getLogger().debug("Creating stream for " + this.getServerName() + " server");
QuicChannel quicChannel = future.getNow();

quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, new TransportChannelInitializer(proxiedPlayer, this, promise)).addListener((Future<QuicStreamChannel> streamFuture) -> {
Expand All @@ -69,13 +71,15 @@ public Future<ClientConnection> createConnection(ProxiedPlayer proxiedPlayer) {
return promise;
}

private Future<QuicChannel> createServerConnection(EventLoopGroup eventLoopGroup, InetSocketAddress address) {
private Future<QuicChannel> createServerConnection(EventLoopGroup eventLoopGroup, MainLogger logger, InetSocketAddress address) {
EventLoop eventLoop = eventLoopGroup.next();

if (this.serverConnections.containsKey(address)) {
logger.info("Reusing connection to " + address + " for " + this.getServerName() + " server");
return this.serverConnections.get(address);
}

logger.info("Creating connection to " + address + " for " + this.getServerName() + " server");
Promise<QuicChannel> promise = eventLoop.newPromise();
this.serverConnections.put(address, promise);

Expand All @@ -96,25 +100,35 @@ private Future<QuicChannel> createServerConnection(EventLoopGroup eventLoopGroup
QuicChannel.newBootstrap(channelFuture.channel())
.streamHandler(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.close();
}
})
.remoteAddress(address)
.option(QuicChannelOption.QLOG, new QLogConfiguration("/" + this.getServerName() + "-" + System.currentTimeMillis() + ".qlog", this.getServerName(), this.getServerName()))
.connect().addListener((Future<QuicChannel> quicChannelFuture) -> {
if (quicChannelFuture.isSuccess()) {
logger.debug("Connection to " + address + " for " + this.getServerName() + " server established");

QuicChannel quicChannel = quicChannelFuture.getNow();
quicChannel.closeFuture().addListener(f -> serverConnections.remove(address));
quicChannel.closeFuture().addListener(f -> {
logger.debug("Connection to " + address + " for " + this.getServerName() + " server closed");
this.serverConnections.remove(address);
});

promise.trySuccess(quicChannel);
} else {
logger.warning("Connection to " + address + " for " + this.getServerName() + " server failed");

promise.tryFailure(quicChannelFuture.cause());
channelFuture.channel().close();
this.serverConnections.remove(address);
}
});
} else {
promise.tryFailure(channelFuture.cause());
channelFuture.channel().close();
this.serverConnections.remove(address);
}
});

Expand Down

0 comments on commit c9daaa8

Please sign in to comment.