-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/com/rtm516/mcxboxbroadcast/core/ping/ClientPingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.rtm516.mcxboxbroadcast.core.ping; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelDuplexHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.util.concurrent.Promise; | ||
import io.netty.util.concurrent.ScheduledFuture; | ||
import org.cloudburstmc.netty.channel.raknet.RakPong; | ||
import org.cloudburstmc.protocol.bedrock.BedrockPong; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* Licenced under GPL-2.0 | ||
* | ||
* https://github.com/WaterdogPE/WaterdogPE/blob/4e2b3cd1a3d5e4d3599476fd907b6bd3186783eb/src/main/java/dev/waterdog/waterdogpe/network/connection/codec/client/ClientPingHandler.java | ||
*/ | ||
public class ClientPingHandler extends ChannelDuplexHandler { | ||
|
||
private final Promise<BedrockPong> future; | ||
|
||
private final long timeout; | ||
private final TimeUnit timeUnit; | ||
private ScheduledFuture<?> timeoutFuture; | ||
|
||
public ClientPingHandler(Promise<BedrockPong> future, long timeout, TimeUnit timeUnit) { | ||
this.future = future; | ||
this.timeout = timeout; | ||
this.timeUnit = timeUnit; | ||
} | ||
|
||
private void onTimeout(Channel channel) { | ||
channel.close(); | ||
this.future.tryFailure(new TimeoutException()); | ||
} | ||
|
||
@Override | ||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { | ||
this.timeoutFuture = ctx.channel().eventLoop().schedule(() -> this.onTimeout(ctx.channel()), this.timeout, this.timeUnit); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
if (!(msg instanceof RakPong rakPong)) { | ||
super.channelRead(ctx, msg); | ||
return; | ||
} | ||
|
||
if (this.timeoutFuture != null) { | ||
this.timeoutFuture.cancel(false); | ||
this.timeoutFuture = null; | ||
} | ||
|
||
ctx.channel().close(); | ||
|
||
this.future.trySuccess(BedrockPong.fromRakNet(rakPong.getPongData())); | ||
} | ||
|
||
@Override | ||
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { | ||
super.close(ctx, promise); | ||
|
||
if (this.timeoutFuture != null) { | ||
this.timeoutFuture.cancel(false); | ||
this.timeoutFuture = null; | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core/src/main/java/com/rtm516/mcxboxbroadcast/core/ping/PingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.rtm516.mcxboxbroadcast.core.ping; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.EventLoop; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioDatagramChannel; | ||
import io.netty.util.concurrent.Promise; | ||
import org.cloudburstmc.netty.channel.raknet.RakChannelFactory; | ||
import org.cloudburstmc.netty.channel.raknet.RakPing; | ||
import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption; | ||
import org.cloudburstmc.protocol.bedrock.BedrockPong; | ||
import org.java_websocket.util.NamedThreadFactory; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Licenced under GPL-2.0 | ||
* Modified to fit MCXboxBroadcast | ||
* | ||
* https://github.com/WaterdogPE/WaterdogPE/blob/4e2b3cd1a3d5e4d3599476fd907b6bd3186783eb/src/main/java/dev/waterdog/waterdogpe/network/serverinfo/BedrockServerInfo.java | ||
*/ | ||
public class PingUtil { | ||
private static EventLoopGroup workerEventLoopGroup; | ||
|
||
static { | ||
workerEventLoopGroup = new NioEventLoopGroup(0, new NamedThreadFactory("MCXboxBroadcast Ping Thread")); | ||
} | ||
|
||
public static Promise<BedrockPong> ping(InetSocketAddress server, long timeout, TimeUnit timeUnit) { | ||
EventLoop eventLoop = workerEventLoopGroup.next(); | ||
Promise<BedrockPong> promise = eventLoop.newPromise(); | ||
|
||
new Bootstrap() | ||
.channelFactory(RakChannelFactory.client(NioDatagramChannel.class)) | ||
.group(workerEventLoopGroup) | ||
.option(RakChannelOption.RAK_GUID, ThreadLocalRandom.current().nextLong()) | ||
.handler(new ClientPingHandler(promise, timeout, timeUnit)) | ||
.bind(ThreadLocalRandom.current().nextInt(10000, 15000)) | ||
.addListener((ChannelFuture future) -> { | ||
if (future.cause() != null) { | ||
promise.tryFailure(future.cause()); | ||
future.channel().close(); | ||
} else { | ||
RakPing ping = new RakPing(System.currentTimeMillis(), server); | ||
future.channel().writeAndFlush(ping).addListener(future1 -> { | ||
if (future1.cause() != null) { | ||
promise.tryFailure(future1.cause()); | ||
future.channel().close(); | ||
} | ||
}); | ||
} | ||
}); | ||
return promise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters