Search in sources :

Example 6 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project flink by apache.

the class KvStateServerHandlerTest method testCloseChannelOnExceptionCaught.

/**
	 * Tests that the channel is closed if an Exception reaches the channel
	 * handler.
	 */
@Test
public void testCloseChannelOnExceptionCaught() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertTrue(response.getMessage().contains("Expected test Exception"));
    channel.closeFuture().await(READ_TIMEOUT_MILLIS);
    assertFalse(channel.isActive());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 7 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project flink by apache.

the class KvStateServerHandlerTest method testUnexpectedMessage.

/**
	 * Tests response on unexpected messages.
	 */
@Test
public void testUnexpectedMessage() throws Exception {
    KvStateRegistry registry = new KvStateRegistry();
    AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
    KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
    EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
    // Write the request and wait for the response
    ByteBuf unexpectedMessage = Unpooled.buffer(8);
    unexpectedMessage.writeInt(4);
    unexpectedMessage.writeInt(123238213);
    channel.writeInbound(unexpectedMessage);
    ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    Throwable response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertEquals(0, stats.getNumRequests());
    assertEquals(0, stats.getNumFailed());
    unexpectedMessage = KvStateRequestSerializer.serializeKvStateRequestResult(channel.alloc(), 192, new byte[0]);
    channel.writeInbound(unexpectedMessage);
    buf = (ByteBuf) readInboundBlocking(channel);
    // skip frame length
    buf.skipBytes(4);
    // Verify the response
    assertEquals(KvStateRequestType.SERVER_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
    response = KvStateRequestSerializer.deserializeServerFailure(buf);
    assertTrue("Unexpected failure cause " + response.getClass().getName(), response instanceof IllegalArgumentException);
    assertEquals(0, stats.getNumRequests());
    assertEquals(0, stats.getNumFailed());
}
Also used : KvStateRegistry(org.apache.flink.runtime.query.KvStateRegistry) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 8 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project MinecraftForge by MinecraftForge.

the class FMLNetworkHandler method openGui.

public static void openGui(EntityPlayer entityPlayer, Object mod, int modGuiId, World world, int x, int y, int z) {
    ModContainer mc = FMLCommonHandler.instance().findContainerFor(mod);
    if (entityPlayer instanceof EntityPlayerMP && !(entityPlayer instanceof FakePlayer)) {
        EntityPlayerMP entityPlayerMP = (EntityPlayerMP) entityPlayer;
        Container remoteGuiContainer = NetworkRegistry.INSTANCE.getRemoteGuiContainer(mc, entityPlayerMP, modGuiId, world, x, y, z);
        if (remoteGuiContainer != null) {
            entityPlayerMP.getNextWindowId();
            entityPlayerMP.closeContainer();
            int windowId = entityPlayerMP.currentWindowId;
            FMLMessage.OpenGui openGui = new FMLMessage.OpenGui(windowId, mc.getModId(), modGuiId, x, y, z);
            EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER);
            embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.PLAYER);
            embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entityPlayerMP);
            embeddedChannel.writeOutbound(openGui);
            entityPlayerMP.openContainer = remoteGuiContainer;
            entityPlayerMP.openContainer.windowId = windowId;
            entityPlayerMP.openContainer.addListener(entityPlayerMP);
            net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.player.PlayerContainerEvent.Open(entityPlayer, entityPlayer.openContainer));
        }
    } else if (entityPlayer instanceof FakePlayer) {
    // NO OP - I won't even log a message!
    } else if (FMLCommonHandler.instance().getSide().equals(Side.CLIENT)) {
        Object guiContainer = NetworkRegistry.INSTANCE.getLocalGuiContainer(mc, entityPlayer, modGuiId, world, x, y, z);
        FMLCommonHandler.instance().showGuiScreen(guiContainer);
    } else {
        FMLLog.fine("Invalid attempt to open a local GUI on a dedicated server. This is likely a bug. GUI ID: %s,%d", mc.getModId(), modGuiId);
    }
}
Also used : ModContainer(net.minecraftforge.fml.common.ModContainer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FMLEmbeddedChannel(net.minecraftforge.fml.common.network.FMLEmbeddedChannel) FakePlayer(net.minecraftforge.common.util.FakePlayer) ModContainer(net.minecraftforge.fml.common.ModContainer) FMLContainer(net.minecraftforge.fml.common.FMLContainer) Container(net.minecraft.inventory.Container) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) JsonObject(com.google.gson.JsonObject)

Example 9 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project MinecraftForge by MinecraftForge.

the class FMLNetworkHandler method registerChannel.

public static void registerChannel(FMLContainer container, Side side) {
    channelPair = NetworkRegistry.INSTANCE.newChannel(container, "FML", new FMLRuntimeCodec(), new HandshakeCompletionHandler());
    EmbeddedChannel embeddedChannel = channelPair.get(Side.SERVER);
    embeddedChannel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.NOWHERE);
    if (side == Side.CLIENT) {
        addClientHandlers();
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FMLEmbeddedChannel(net.minecraftforge.fml.common.network.FMLEmbeddedChannel)

Example 10 with EmbeddedChannel

use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.

the class Http2ServerDowngraderTest method testPassThroughOther.

@Test
public void testPassThroughOther() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
    Http2ResetFrame reset = new DefaultHttp2ResetFrame(0);
    Http2GoAwayFrame goaway = new DefaultHttp2GoAwayFrame(0);
    assertTrue(ch.writeInbound(reset));
    assertTrue(ch.writeInbound(goaway.retain()));
    assertEquals(reset, ch.readInbound());
    Http2GoAwayFrame frame = ch.readInbound();
    try {
        assertEquals(goaway, frame);
        assertThat(ch.readInbound(), is(nullValue()));
        assertFalse(ch.finish());
    } finally {
        goaway.release();
        frame.release();
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1027 Test (org.junit.jupiter.api.Test)515 ByteBuf (io.netty.buffer.ByteBuf)356 Test (org.junit.Test)342 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)85 HttpResponse (io.netty.handler.codec.http.HttpResponse)73 HttpRequest (io.netty.handler.codec.http.HttpRequest)69 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)64 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)60 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)55 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)50 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)49 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)46 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)42 IOException (java.io.IOException)38 InetSocketAddress (java.net.InetSocketAddress)38 Executable (org.junit.jupiter.api.function.Executable)36 ArrayList (java.util.ArrayList)34 Before (org.junit.Before)32 ChannelHandler (io.netty.channel.ChannelHandler)27