use of org.spongepowered.api.network.channel.Channel in project SpongeCommon by SpongePowered.
the class SpongeChannelManager method ofType.
@Override
public <C extends Channel> C ofType(final ResourceKey channelKey, final Class<C> channelType) {
Objects.requireNonNull(channelKey, "channelKey");
Objects.requireNonNull(channelType, "channelType");
final Channel binding = this.channels.get(channelKey);
if (binding != null) {
if (!channelType.isInstance(binding)) {
throw new IllegalStateException("There's already a channel registered for " + channelKey + ", but it is not of the requested type " + channelType);
}
return (C) binding;
}
return this.createChannel(channelKey, channelType);
}
use of org.spongepowered.api.network.channel.Channel in project SpongeCommon by SpongePowered.
the class ChannelTest method onConnectionHandshake.
@Listener
public void onConnectionHandshake(final ServerSideConnectionEvent.Handshake event) {
this.plugin.logger().info("Starting handshake phase.");
final PingPacket pingPacket1 = new PingPacket(123);
final ServerSideConnection connection = event.connection();
this.channel.sendTo(connection, pingPacket1).thenAccept(response1 -> {
this.logReceived(this.channel, response1, connection);
final PingPacket pingPacket2 = new PingPacket(456);
this.channel.sendTo(connection, pingPacket2).thenAccept(response2 -> {
this.logReceived(this.channel, response2, connection);
this.channel.sendTo(connection, new PrintTextPacket("Finished handshake phase."));
this.plugin.logger().info("Finished handshake phase.");
}).exceptionally(cause -> {
this.plugin.logger().error("Failed to get a response to {}", pingPacket2, cause);
return null;
});
}).exceptionally(cause -> {
this.plugin.logger().error("Failed to get a response to {}", pingPacket1, cause);
return null;
});
final PingPacket basicPingPacket1 = new PingPacket(1123);
this.basicChannel.handshake().sendTo(connection, basicPingPacket1).thenAccept(response1 -> {
this.logReceived(this.basicChannel, response1, connection);
final PingPacket basicPingPacket2 = new PingPacket(1456);
this.basicChannel.handshake().sendTo(connection, basicPingPacket2).thenAccept(response2 -> {
this.logReceived(this.channel, response2, connection);
this.basicChannel.handshake().sendTo(connection, new PrintTextPacket("Finished handshake phase for basic channel."));
this.plugin.logger().info("Finished handshake phase for basic channel.");
}).exceptionally(cause -> {
this.plugin.logger().error("Failed to get a response to {}", basicPingPacket2, cause);
return null;
});
}).exceptionally(cause -> {
this.plugin.logger().error("Failed to get a response to {}", pingPacket1, cause);
return null;
});
this.rawChannel.handshake().sendTo(connection, buf -> buf.writeVarInt(200)).thenAccept(response -> this.logReceived(this.rawChannel, response.readVarInt(), connection)).exceptionally(cause -> {
this.plugin.logger().error("Failed to get a response to raw 200 value", cause);
return null;
});
this.rawChannel.handshake().sendTo(connection, buf -> buf.writeVarInt(0)).thenAccept(response -> this.logReceived(this.rawChannel, response.readVarInt(), connection)).exceptionally(cause -> {
if (cause instanceof CompletionException) {
cause = cause.getCause();
}
if (cause instanceof NoResponseException) {
this.plugin.logger().error("Successfully received no response exception");
} else {
this.plugin.logger().error("Failed to get a response to raw 0 value", cause);
}
return null;
});
}
Aggregations