Search in sources :

Example 1 with Channel

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);
}
Also used : Channel(org.spongepowered.api.network.channel.Channel) BasicPacketChannel(org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) SpongePacketChannel(org.spongepowered.common.network.channel.packet.SpongePacketChannel) SpongeRawDataChannel(org.spongepowered.common.network.channel.raw.SpongeRawDataChannel) SpongeBasicPacketChannel(org.spongepowered.common.network.channel.packet.SpongeBasicPacketChannel) PacketChannel(org.spongepowered.api.network.channel.packet.PacketChannel)

Example 2 with Channel

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;
    });
}
Also used : Channel(org.spongepowered.api.network.channel.Channel) BasicPacketChannel(org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) ServerSideConnectionEvent(org.spongepowered.api.event.network.ServerSideConnectionEvent) Inject(com.google.inject.Inject) EngineConnection(org.spongepowered.api.network.EngineConnection) NoResponseException(org.spongepowered.api.network.channel.NoResponseException) EngineConnectionSide(org.spongepowered.api.network.EngineConnectionSide) CompletionException(java.util.concurrent.CompletionException) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) PluginContainer(org.spongepowered.plugin.PluginContainer) RegisterChannelEvent(org.spongepowered.api.event.lifecycle.RegisterChannelEvent) ResourceKey(org.spongepowered.api.ResourceKey) PacketChannel(org.spongepowered.api.network.channel.packet.PacketChannel) Listener(org.spongepowered.api.event.Listener) CompletionException(java.util.concurrent.CompletionException) ServerSideConnection(org.spongepowered.api.network.ServerSideConnection) NoResponseException(org.spongepowered.api.network.channel.NoResponseException) Listener(org.spongepowered.api.event.Listener)

Aggregations

Channel (org.spongepowered.api.network.channel.Channel)2 PacketChannel (org.spongepowered.api.network.channel.packet.PacketChannel)2 BasicPacketChannel (org.spongepowered.api.network.channel.packet.basic.BasicPacketChannel)2 RawDataChannel (org.spongepowered.api.network.channel.raw.RawDataChannel)2 Inject (com.google.inject.Inject)1 CompletionException (java.util.concurrent.CompletionException)1 ResourceKey (org.spongepowered.api.ResourceKey)1 Listener (org.spongepowered.api.event.Listener)1 RegisterChannelEvent (org.spongepowered.api.event.lifecycle.RegisterChannelEvent)1 ServerSideConnectionEvent (org.spongepowered.api.event.network.ServerSideConnectionEvent)1 EngineConnection (org.spongepowered.api.network.EngineConnection)1 EngineConnectionSide (org.spongepowered.api.network.EngineConnectionSide)1 ServerSideConnection (org.spongepowered.api.network.ServerSideConnection)1 NoResponseException (org.spongepowered.api.network.channel.NoResponseException)1 SpongeBasicPacketChannel (org.spongepowered.common.network.channel.packet.SpongeBasicPacketChannel)1 SpongePacketChannel (org.spongepowered.common.network.channel.packet.SpongePacketChannel)1 SpongeRawDataChannel (org.spongepowered.common.network.channel.raw.SpongeRawDataChannel)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1 Plugin (org.spongepowered.plugin.builtin.jvm.Plugin)1