本文整理汇总了Java中io.netty.channel.ChannelInboundHandlerAdapter类的典型用法代码示例。如果您正苦于以下问题:Java ChannelInboundHandlerAdapter类的具体用法?Java ChannelInboundHandlerAdapter怎么用?Java ChannelInboundHandlerAdapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ChannelInboundHandlerAdapter类属于io.netty.channel包,在下文中一共展示了ChannelInboundHandlerAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MockClient
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
MockClient(EventLoopGroup elg, FrameCodec<ByteBuf> frameCodec) {
// Set up so written Frames are encoded into bytes, received bytes are encoded into Frames put
// on queue.
cb.group(elg)
.channel(LocalChannel.class)
.handler(
new ChannelInitializer<LocalChannel>() {
@Override
protected void initChannel(LocalChannel ch) throws Exception {
ch.pipeline()
.addLast(new FrameEncoder(frameCodec))
.addLast(new TestFrameDecoder(frameCodec))
.addLast(
new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
responses.offer((Frame) msg);
}
});
}
});
}
示例2: shouldFireOutboundChannelClosedEvent
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Test
public void shouldFireOutboundChannelClosedEvent() throws InterruptedException {
inboundChannel.pipeline().addLast(handler);
List<Object> events = new ArrayList<>(1);
outboundChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
events.add(evt);
}
});
inboundChannel.close().sync();
assertFalse(events.isEmpty());
assertEquals(1, events.size());
assertTrue(events.get(0) instanceof OutboundChannelClosedEvent);
}
示例3: serverBootStrapWithOptionsTest
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Test
public void serverBootStrapWithOptionsTest() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
LinkedHashMap<String, Object> channelHandlerOptions = new LinkedHashMap<String, Object>();
channelHandlerOptions.put("lineFrame", new LineBasedFrameDecoder(2000));
channelHandlerOptions.put("decoder", new StringDecoder());
channelHandlerOptions.put("encoder", new StringEncoder());
channelHandlerOptions.put("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("Message Received and forward to ConsumerProcessor. Msg -> {}", msg);
}
});
Server server = BootStrap.builder()
.port(5252)
.options(channelHandlerOptions)
.messageConsumer(msg -> log.info(msg))
.build();
assertNotNull(server);
}
示例4: newNonSslHandler
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Override
protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
return new ChannelInboundHandlerAdapter() {
private HttpResponseEncoder encoder = new HttpResponseEncoder();
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
LOG.trace("Received non-SSL request, returning redirect");
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.MOVED_PERMANENTLY, Unpooled.EMPTY_BUFFER);
response.headers().set(Names.LOCATION, redirectAddress);
LOG.trace(Constants.LOG_RETURNING_RESPONSE, response);
encoder.write(ctx, response, ctx.voidPromise());
ctx.flush();
}
};
}
示例5: initialize
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
/**
* Initializes this socket and binds its internal udp socket to a free port.
* If the socket is already initialized any invocation of this method will
* result in an IllegalStateException.
*
* @throws SocketException Thrown in case the socket could not be initialized
*/
public void initialize() throws SocketException {
if ( this.isInitialized() ) {
throw new IllegalStateException( "Cannot re-initialized ClientSocket" );
}
this.udpSocket = new Bootstrap();
this.udpSocket.group( Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup() );
this.udpSocket.channel( Epoll.isAvailable() ? EpollDatagramChannel.class : NioDatagramChannel.class );
this.udpSocket.handler( new ChannelInboundHandlerAdapter() {
@Override
public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception {
io.netty.channel.socket.DatagramPacket packet = (io.netty.channel.socket.DatagramPacket) msg;
PacketBuffer content = new PacketBuffer( packet.content() );
InetSocketAddress sender = packet.sender();
if ( !receiveDatagram( sender, content ) ) {
// Push datagram to update queue:
handleDatagram( sender, content, System.currentTimeMillis() );
}
}
} );
try {
this.channel = this.udpSocket.bind( ThreadLocalRandom.current().nextInt( 45000, 65000 ) ).sync().channel();
} catch ( InterruptedException e ) {
SocketException exception = new SocketException( "Could not bind to socket" );
exception.initCause( e );
throw exception;
}
this.afterInitialize();
}
示例6: run
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
public void run() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("Hello DiscardServer.", CharsetUtil.UTF_8));
}
});
}
})
.option(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.connect().sync();
//
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
示例7: disconnect
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
private void disconnect(ClientSecureChannel secureChannel, CompletableFuture<Unit> disconnected) {
RequestHeader requestHeader = new RequestHeader(
NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, uint(0), null);
secureChannel.getChannel().pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.debug("channelInactive(), disconnect complete");
disconnected.complete(Unit.VALUE);
super.channelInactive(ctx);
}
});
logger.debug("Sending CloseSecureChannelRequest...");
CloseSecureChannelRequest request = new CloseSecureChannelRequest(requestHeader);
secureChannel.getChannel().pipeline().fireUserEventTriggered(request);
client.getConfig().getWheelTimer().newTimeout(
timeout -> disconnected.completeExceptionally(new UaException(StatusCodes.Bad_Timeout)),
5,
TimeUnit.SECONDS
);
}
示例8: main
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new PurchaseDataDecoder());
p.addLast(new PurchaseDataEncoder());
p.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx,
Object data) throws Exception {
System.out.println("processed Purchase " + data);
PurchaseData processed = new PurchaseData(data, true);
ctx.writeAndFlush(processed);
}
});
}
};
BootstrapTemplate.newServerBootstrap(HOST, PORT, initializer);
}
示例9: main
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new StringEncoder());
p.addLast(new StringDecoder());
p.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg);
ctx.close();
}
});
}
};
BootstrapTemplate.newServerBootstrap(HOST, PORT, initializer);
}
示例10: TestConstantStringServer
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
public TestConstantStringServer(int port, final String constant) throws InterruptedException {
channel = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(TestConstantStringServer.class, LogLevel.DEBUG))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(TestConstantStringServer.class, LogLevel.DEBUG));
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.write(msg);
ctx.writeAndFlush(Unpooled.copiedBuffer(constant, CharsetUtil.UTF_8))
.addListener(ChannelFutureListener.CLOSE);
}
});
}
})
.bind(LOCALHOST, port)
.sync()
.channel();
}
示例11: init
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@BeforeClass
public static void init() {
// Configure a test server
group = new LocalEventLoopGroup();
ServerBootstrap sb = new ServerBootstrap();
sb.group(group)
.channel(LocalServerChannel.class)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Discard
ReferenceCountUtil.release(msg);
}
});
}
});
localAddr = (LocalAddress) sb.bind(LocalAddress.ANY).syncUninterruptibly().channel().localAddress();
}
示例12: init
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@BeforeClass
public static void init() {
// Configure a test server
group = new LocalEventLoopGroup();
ServerBootstrap sb = new ServerBootstrap();
sb.group(group)
.channel(LocalServerChannel.class)
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Discard
ReferenceCountUtil.release(msg);
}
});
}
});
localAddr = (LocalAddress) sb.bind(LocalAddress.ANY).syncUninterruptibly().channel().localAddress();
}
示例13: testTooManyAcceptedChannels
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Test
public void testTooManyAcceptedChannels() throws Exception {
EventLoopGroup g = new OioEventLoopGroup(1);
ServerBootstrap sb = new ServerBootstrap();
sb.channel(OioServerSocketChannel.class);
sb.group(g);
sb.childHandler(new ChannelInboundHandlerAdapter());
ChannelFuture f1 = sb.bind(0);
f1.sync();
Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
assertThat(s.getInputStream().read(), is(-1));
s.close();
g.shutdownGracefully();
}
示例14: testTcpInfo
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Test
public void testTcpInfo() throws Exception {
EventLoopGroup group = new EpollEventLoopGroup(1);
try {
Bootstrap bootstrap = new Bootstrap();
EpollSocketChannel ch = (EpollSocketChannel) bootstrap.group(group)
.channel(EpollSocketChannel.class)
.handler(new ChannelInboundHandlerAdapter())
.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
EpollTcpInfo info = ch.tcpInfo();
assertTcpInfo0(info);
ch.close().syncUninterruptibly();
} finally {
group.shutdownGracefully();
}
}
示例15: testTcpInfoReuse
import io.netty.channel.ChannelInboundHandlerAdapter; //导入依赖的package包/类
@Test
public void testTcpInfoReuse() throws Exception {
EventLoopGroup group = new EpollEventLoopGroup(1);
try {
Bootstrap bootstrap = new Bootstrap();
EpollSocketChannel ch = (EpollSocketChannel) bootstrap.group(group)
.channel(EpollSocketChannel.class)
.handler(new ChannelInboundHandlerAdapter())
.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
EpollTcpInfo info = new EpollTcpInfo();
ch.tcpInfo(info);
assertTcpInfo0(info);
ch.close().syncUninterruptibly();
} finally {
group.shutdownGracefully();
}
}