use of io.netty.channel.ChannelHandlerContext in project vert.x by eclipse.
the class Http2ServerTest method testQueuePushPromise.
@Test
public void testQueuePushPromise() throws Exception {
Context ctx = vertx.getOrCreateContext();
int numPushes = 10;
Set<String> pushSent = new HashSet<>();
server.requestHandler(req -> {
req.response().setChunked(true).write("abc");
for (int i = 0; i < numPushes; i++) {
int val = i;
String path = "/wibble" + val;
req.response().push(HttpMethod.GET, path, ar -> {
assertTrue(ar.succeeded());
assertSame(ctx, Vertx.currentContext());
pushSent.add(path);
vertx.setTimer(10, id -> {
ar.result().end("wibble-" + val);
});
});
}
});
startServer(ctx);
TestClient client = new TestClient();
client.settings.maxConcurrentStreams(3);
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.decoder.frameListener(new Http2FrameAdapter() {
int count = numPushes;
Set<String> pushReceived = new HashSet<>();
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushReceived.add(headers.path().toString());
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
if (count-- == 0) {
vertx.runOnContext(v -> {
assertEquals(numPushes, pushSent.size());
assertEquals(pushReceived, pushSent);
testComplete();
});
}
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
});
fut.sync();
await();
}
use of io.netty.channel.ChannelHandlerContext in project vert.x by eclipse.
the class Http2ServerTest method testUnknownFrame.
@Test
public void testUnknownFrame() throws Exception {
Buffer expectedSend = TestUtils.randomBuffer(500);
Buffer expectedRecv = TestUtils.randomBuffer(500);
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
req.customFrameHandler(frame -> {
assertOnIOContext(ctx);
assertEquals(10, frame.type());
assertEquals(253, frame.flags());
assertEquals(expectedSend, frame.payload());
req.response().writeCustomFrame(12, 134, expectedRecv).end();
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
int status = 0;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
int s = status++;
vertx.runOnContext(v -> {
assertEquals(0, s);
});
}
@Override
public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload) {
int s = status++;
Buffer recv = Buffer.buffer(payload.copy());
vertx.runOnContext(v -> {
assertEquals(1, s);
assertEquals(12, frameType);
assertEquals(134, flags.value());
assertEquals(expectedRecv, recv);
});
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int len = data.readableBytes();
int s = status++;
vertx.runOnContext(v -> {
assertEquals(2, s);
assertEquals(0, len);
assertTrue(endOfStream);
testComplete();
});
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.encoder.writeFrame(request.context, (byte) 10, id, new Http2Flags((short) 253), expectedSend.getByteBuf(), request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.netty.channel.ChannelHandlerContext in project MinecraftForge by MinecraftForge.
the class PacketLoggingHandler method register.
public static void register(NetworkManager manager) {
ChannelPipeline pipeline = manager.channel().pipeline();
final EnumPacketDirection direction = manager.getDirection();
if (manager.isLocalChannel()) {
pipeline.addBefore("packet_handler", "splitter", new SimpleChannelInboundHandler<Packet<?>>() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");
@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) throws Exception {
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
msg.writePacketData(buf);
FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
ctx.fireChannelRead(msg);
}
});
pipeline.addBefore("splitter", "prepender", new ChannelOutboundHandlerAdapter() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof Packet<?>) {
PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
((Packet<?>) msg).writePacketData(buf);
FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
}
ctx.write(msg, promise);
}
});
} else {
pipeline.replace("splitter", "splitter", new NettyVarint21FrameDecoder() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");
@Override
protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception {
super.decode(context, input, output);
Iterator<Object> itr = output.iterator();
while (itr.hasNext()) {
ByteBuf pkt = (ByteBuf) itr.next();
pkt.markReaderIndex();
FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(pkt));
pkt.resetReaderIndex();
}
}
});
pipeline.replace("prepender", "prepender", new NettyVarint21FrameEncoder() {
String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");
@Override
protected void encode(ChannelHandlerContext context, ByteBuf input, ByteBuf output) throws Exception {
input.markReaderIndex();
FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(input));
input.resetReaderIndex();
super.encode(context, input, output);
}
});
}
}
use of io.netty.channel.ChannelHandlerContext in project failsafe by jhalterman.
the class NettyExample method createBootstrap.
static Bootstrap createBootstrap(EventLoopGroup group) {
return new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
});
}
});
}
use of io.netty.channel.ChannelHandlerContext in project pinot by linkedin.
the class ScheduledRequestHandlerTest method setupTestMethod.
@BeforeTest
public void setupTestMethod() {
serverMetrics = new ServerMetrics(new MetricsRegistry());
channelHandlerContext = mock(ChannelHandlerContext.class, RETURNS_DEEP_STUBS);
when(channelHandlerContext.channel().remoteAddress()).thenAnswer(new Answer<InetSocketAddress>() {
@Override
public InetSocketAddress answer(InvocationOnMock invocationOnMock) throws Throwable {
return new InetSocketAddress("localhost", 60000);
}
});
queryScheduler = mock(QueryScheduler.class);
queryExecutor = new ServerQueryExecutorV1Impl();
}
Aggregations