public ChannelInitializer<SocketChannel> newChannelInitializer() {
return new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new NettyDecoder());
ch.pipeline().addLast("encoder", new NettyEncoder());
ch.pipeline().addLast("processor", new NettyProcessorHandler(processor));
}
};
}
java类io.netty.channel.ChannelInitializer的实例源码
NettyServer.java 文件源码
项目:rpc
阅读 19
收藏 0
点赞 0
评论 0
XClient.java 文件源码
项目:AgentX
阅读 22
收藏 0
点赞 0
评论 0
public void start() {
Configuration config = Configuration.INSTANCE;
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline()
.addLast("logging", new LoggingHandler(LogLevel.DEBUG))
.addLast(new SocksInitRequestDecoder())
.addLast(new SocksMessageEncoder())
.addLast(new Socks5Handler())
.addLast(Status.TRAFFIC_HANDLER);
}
});
log.info("\tStartup {}-{}-client [{}{}]", Constants.APP_NAME, Constants.APP_VERSION, config.getMode(), config.getMode().equals("socks5") ? "" : ":" + config.getProtocol());
new Thread(() -> new UdpServer().start()).start();
ChannelFuture future = bootstrap.bind(config.getLocalHost(), config.getLocalPort()).sync();
future.addListener(future1 -> log.info("\tTCP listening at {}:{}...", config.getLocalHost(), config.getLocalPort()));
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("\tSocket bind failure ({})", e.getMessage());
} finally {
log.info("\tShutting down");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
XServer.java 文件源码
项目:AgentX
阅读 21
收藏 0
点赞 0
评论 0
public void start() {
Configuration config = Configuration.INSTANCE;
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline()
.addLast("logging", new LoggingHandler(LogLevel.DEBUG))
.addLast(new XConnectHandler());
if (config.getReadLimit() != 0 || config.getWriteLimit() != 0) {
socketChannel.pipeline().addLast(
new GlobalTrafficShapingHandler(Executors.newScheduledThreadPool(1), config.getWriteLimit(), config.getReadLimit())
);
}
}
});
log.info("\tStartup {}-{}-server [{}]", Constants.APP_NAME, Constants.APP_VERSION, config.getProtocol());
new Thread(() -> new UdpServer().start()).start();
ChannelFuture future = bootstrap.bind(config.getHost(), config.getPort()).sync();
future.addListener(future1 -> log.info("\tTCP listening at {}:{}...", config.getHost(), config.getPort()));
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("\tSocket bind failure ({})", e.getMessage());
} finally {
log.info("\tShutting down and recycling...");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
Configuration.shutdownRelays();
}
System.exit(0);
}
NettyConnector.java 文件源码
项目:PetiteRPC
阅读 25
收藏 0
点赞 0
评论 0
@Override
public Connection connect(Address address, Consumer<TransportChannel> successEvent) {
Bootstrap bootstrap = bootstrap();
final SocketAddress socketAddress = InetSocketAddress.createUnresolved(address.getHost(), address.getPort());
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline()
.addLast(new Encoder(serializer))
.addLast(new Decoder(serializer))
.addLast(new ConsumerHandler());
}
});
ChannelFuture connectChannelFuture = bootstrap.connect(socketAddress);
connectChannelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
TransportChannel transportChannel = NettyChannel.getInstance(future.channel());
successEvent.accept(transportChannel);
}
}
});
return new NettyConnection(connectChannelFuture);
}
NetworkManager.java 文件源码
项目:CustomWorldGen
阅读 28
收藏 0
点赞 0
评论 0
/**
* Create a new NetworkManager from the server host and connect it to the server
*/
@SideOnly(Side.CLIENT)
public static NetworkManager createNetworkManagerAndConnect(InetAddress address, int serverPort, boolean useNativeTransport)
{
final NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.CLIENTBOUND);
Class <? extends SocketChannel > oclass;
LazyLoadBase <? extends EventLoopGroup > lazyloadbase;
if (Epoll.isAvailable() && useNativeTransport)
{
oclass = EpollSocketChannel.class;
lazyloadbase = CLIENT_EPOLL_EVENTLOOP;
}
else
{
oclass = NioSocketChannel.class;
lazyloadbase = CLIENT_NIO_EVENTLOOP;
}
((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group((EventLoopGroup)lazyloadbase.getValue())).handler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
try
{
p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true));
}
catch (ChannelException var3)
{
;
}
p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"splitter", (ChannelHandler)(new NettyVarint21FrameDecoder())).addLast((String)"decoder", (ChannelHandler)(new NettyPacketDecoder(EnumPacketDirection.CLIENTBOUND))).addLast((String)"prepender", (ChannelHandler)(new NettyVarint21FrameEncoder())).addLast((String)"encoder", (ChannelHandler)(new NettyPacketEncoder(EnumPacketDirection.SERVERBOUND))).addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
})).channel(oclass)).connect(address, serverPort).syncUninterruptibly();
return networkmanager;
}
HttpProxyServer.java 文件源码
项目:proxyee
阅读 21
收藏 0
点赞 0
评论 0
public void start(int port) {
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
// .option(ChannelOption.SO_BACKLOG, 100)
// .handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("httpCodec", new HttpServerCodec());
ch.pipeline().addLast("serverHandle",
new HttpProxyServerHandle(serverConfig, proxyInterceptInitializer, proxyConfig,
httpProxyExceptionHandle));
}
});
ChannelFuture f = b
.bind(port)
.sync();
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
SocksProxyHandler.java 文件源码
项目:nitmproxy
阅读 26
收藏 0
点赞 0
评论 0
private ChannelFuture createServerChannel(ChannelHandlerContext ctx, Address serverAddr) {
ConnectionInfo newConnectionInfo = new ConnectionInfo(
connectionInfo.getClientAddr(), new Address(serverAddr.getHost(), serverAddr.getPort()));
return master.connect(ctx, newConnectionInfo, new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(master.handler(Handler.TLS_BACKEND, newConnectionInfo, ctx.channel()));
}
});
}
NettyClient.java 文件源码
项目:commelina
阅读 24
收藏 0
点赞 0
评论 0
public static void start(MemberEventLoop loop) throws InterruptedException {
String host = "127.0.0.1";
int port = 9005;
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new ProtobufDecoder(SocketMessage.getDefaultInstance()));
ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast(new ProtobufEncoder());
ch.pipeline().addLast(new IdleStateHandler(0, 5, 10, TimeUnit.SECONDS));
ch.pipeline().addLast(new BusinessRouterHandler(loop));
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
NettyServerServiceImpl.java 文件源码
项目:tx-lcn
阅读 22
收藏 0
点赞 0
评论 0
@Override
public void start() {
int heartTime = transaction_netty_heart_time+10;
txCoreServerHandler = new TxCoreServerHandler(mqTxManagerService);
bossGroup = new NioEventLoopGroup(50); // (1)
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("timeout", new IdleStateHandler(heartTime, heartTime, heartTime, TimeUnit.SECONDS));
ch.pipeline().addLast(new LengthFieldPrepender(4, false));
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast(txCoreServerHandler);
}
});
// Start the server.
b.bind(Constants.socketPort);
logger.info("Socket started on port(s): " + Constants.socketPort + " (socket)");
} catch (Exception e) {
// Shut down all event loops to terminate all threads.
e.printStackTrace();
}
}
Server.java 文件源码
项目:neoscada
阅读 22
收藏 0
点赞 0
评论 0
public Server ( final SocketAddress address, final ProtocolOptions options, final List<ServerModule> modules )
{
this.options = options;
this.manager = new MessageManager ( this.options );
this.bossGroup = new NioEventLoopGroup ();
this.workerGroup = new NioEventLoopGroup ();
this.bootstrap = new ServerBootstrap ();
this.bootstrap.group ( this.bossGroup, this.workerGroup );
this.bootstrap.channel ( NioServerSocketChannel.class );
this.bootstrap.option ( ChannelOption.SO_BACKLOG, 5 );
this.bootstrap.option ( ChannelOption.SO_REUSEADDR, true );
this.bootstrap.childHandler ( new ChannelInitializer<SocketChannel> () {
@Override
protected void initChannel ( final SocketChannel ch ) throws Exception
{
handleInitChannel ( ch );
}
} );
this.modules = modules.toArray ( new ServerModule[modules.size ()] );
for ( final ServerModule module : modules )
{
module.initializeServer ( this, this.manager );
}
this.channel = this.bootstrap.bind ( address ).channel ();
}