/**
* 本地爬虫服务,长连接
*
* @param action
*/
public Client(@Nonnull final Action action){
isLongConnection = true;
final Client self = this;
this.action = action;
channelInitializer = new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new ProtobufDecoder(ProcessData.getDefaultInstance()));
ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast(new ProtobufEncoder());
ch.pipeline().addLast(new ReadTimeoutHandler(60));
ch.pipeline().addLast(new LoginAuthReqHandler(channel));
ch.pipeline().addLast(new LocalCrawlerHandler(action));
ch.pipeline().addLast(new HeartBeatReqHandler(self, closeLongConnection));
}
};
}
java类io.netty.channel.ChannelInitializer的实例源码
Client.java 文件源码
项目:Cobweb
阅读 33
收藏 0
点赞 0
评论 0
MockClient.java 文件源码
项目:simulacron
阅读 29
收藏 0
点赞 0
评论 0
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);
}
});
}
});
}
StarServerProtocol.java 文件源码
项目:star-map
阅读 23
收藏 0
点赞 0
评论 0
@Override
public void openServer(URL url) throws Exception{
EventLoopGroup eventLoop = new NioEventLoopGroup();
EventLoopGroup workLoop = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(eventLoop, workLoop);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader()))) // in 1
.addLast("handler", new ServerHandler()) // in 2
.addLast("encoder", new ObjectEncoder()); // out 3
}
});
serverChannel = serverBootstrap.bind(url.getPort()).sync().sync().channel();
logger.info("start server at:" + url.getPort());
}
RemotingNettyClient.java 文件源码
项目:TakinRPC
阅读 31
收藏 0
点赞 0
评论 0
public void start() {
bootstrap.group(group).channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// ch.pipeline().addLast(new IdleStateHandler(1, 1, 5));
ch.pipeline().addLast(new KyroMsgDecoder());
ch.pipeline().addLast(new KyroMsgEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});
new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
scanResponseTable(3000);
}
}, 1000, 1000, TimeUnit.MILLISECONDS);
}
NettyAcceptor.java 文件源码
项目:PetiteRPC
阅读 28
收藏 0
点赞 0
评论 0
@Override
public void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(8888))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new Encoder(serializer), new Decoder(serializer), new ProviderHandler());
}
});
bootstrap.bind(port);
}
SimpleServer.java 文件源码
项目:upgradeToy
阅读 30
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, true)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new SimpleServerHandler());
}
});
b.bind(8090).sync().channel().closeFuture().sync();
}
SimpleClient.java 文件源码
项目:upgradeToy
阅读 27
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws IOException, InterruptedException {
Bootstrap b = new Bootstrap();
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
}
});
b.connect("localhost", 8090).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
future.channel().write(Unpooled.buffer().writeBytes("123".getBytes()));
future.channel().flush();
future.channel().close();
}
}
});
}
AbstractNettyServer.java 文件源码
项目:Limitart
阅读 31
收藏 0
点赞 0
评论 0
protected AbstractNettyServer(String serverName) {
this.serverName = Objects.requireNonNull(serverName, "server name");
bootstrap = new ServerBootstrap();
if (Epoll.isAvailable()) {
bootstrap.option(ChannelOption.SO_BACKLOG, 1024).channel(EpollServerSocketChannel.class)
.childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
log.info(serverName + " epoll init");
} else {
bootstrap.channel(NioServerSocketChannel.class);
log.info(serverName + " nio init");
}
bootstrap.group(bossGroup, workerGroup).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
initPipeline(ch.pipeline());
}
});
}
ChannelSupplier.java 文件源码
项目:ndbc
阅读 28
收藏 0
点赞 0
评论 0
private final Future<Void> bootstrap(final NettyChannel channel) {
final Promise<Void> p = Promise.apply();
new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.AUTO_READ, false)
.handler(new ChannelInitializer<io.netty.channel.Channel>() {
@Override
protected void initChannel(final io.netty.channel.Channel ch) throws Exception {
ch.pipeline().addLast(new MessageDecoder(), new MessageEncoder(),
new FlowControlHandler(), channel);
}
})
.connect(new InetSocketAddress(host, port))
.addListener(future -> p.become(Future.VOID));
return p;
}
NettyServer.java 文件源码
项目:webapp-tyust
阅读 28
收藏 0
点赞 0
评论 0
private NettyServer(){
pGroup = new NioEventLoopGroup();
cGroup = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
//设置日志
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
sc.pipeline().addLast(new ReadTimeoutHandler(60));
sc.pipeline().addLast(new NettyServerHandler());
}
});
}
EchoClient.java 文件源码
项目:im
阅读 28
收藏 0
点赞 0
评论 0
public void run() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
EchoServer.java 文件源码
项目:im
阅读 25
收藏 0
点赞 0
评论 0
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
NetworkSystem.java 文件源码
项目:Backmemed
阅读 36
收藏 0
点赞 0
评论 0
/**
* Adds a channel that listens locally
*/
public SocketAddress addLocalEndpoint()
{
ChannelFuture channelfuture;
synchronized (this.endpoints)
{
channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
NetworkSystem.this.networkManagers.add(networkmanager);
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
}).group((EventLoopGroup)SERVER_NIO_EVENTLOOP.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
this.endpoints.add(channelfuture);
}
return channelfuture.channel().localAddress();
}
NetworkSystem.java 文件源码
项目:DecompiledMinecraft
阅读 35
收藏 0
点赞 0
评论 0
/**
* Adds a channel that listens locally
*/
public SocketAddress addLocalEndpoint()
{
ChannelFuture channelfuture;
synchronized (this.endpoints)
{
channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
NetworkSystem.this.networkManagers.add(networkmanager);
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
}).group((EventLoopGroup)eventLoops.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
this.endpoints.add(channelfuture);
}
return channelfuture.channel().localAddress();
}
NetworkSystem.java 文件源码
项目:CustomWorldGen
阅读 30
收藏 0
点赞 0
评论 0
/**
* Adds a channel that listens locally
*/
@SideOnly(Side.CLIENT)
public SocketAddress addLocalEndpoint()
{
ChannelFuture channelfuture;
synchronized (this.endpoints)
{
channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
NetworkSystem.this.networkManagers.add(networkmanager);
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
}).group((EventLoopGroup)SERVER_NIO_EVENTLOOP.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
this.endpoints.add(channelfuture);
}
return channelfuture.channel().localAddress();
}
EchoClient.java 文件源码
项目:AlphaLibary
阅读 31
收藏 0
点赞 0
评论 0
public EchoClient(String host, int port) {
EventLoopGroup worker = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(worker)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast(new StringDecoder())
.addLast(new StringEncoder())
.addLast(ech);
}
});
b.connect(host, port);
}
Client.java 文件源码
项目:push
阅读 35
收藏 0
点赞 0
评论 0
public void run() {
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 {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4));
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("decoder", new MsgPackDecode());
pipeline.addLast("encoder", new MsgPackEncode());
pipeline.addLast(new ClientHandler());
}
});
channel = b.connect(clientProperties.getServerHost(), clientProperties.getServerPort()).sync().channel();
status = Status.START;
channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
status = Status.STOP;
}
NetworkSystem.java 文件源码
项目:BaseClient
阅读 29
收藏 0
点赞 0
评论 0
/**
* Adds a channel that listens locally
*/
public SocketAddress addLocalEndpoint()
{
ChannelFuture channelfuture;
synchronized (this.endpoints)
{
channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
NetworkSystem.this.networkManagers.add(networkmanager);
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
}).group((EventLoopGroup)eventLoops.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
this.endpoints.add(channelfuture);
}
return channelfuture.channel().localAddress();
}
NetworkSystem.java 文件源码
项目:BaseClient
阅读 25
收藏 0
点赞 0
评论 0
/**
* Adds a channel that listens locally
*/
public SocketAddress addLocalEndpoint()
{
ChannelFuture channelfuture;
synchronized (this.endpoints)
{
channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
NetworkSystem.this.networkManagers.add(networkmanager);
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
}).group((EventLoopGroup)eventLoops.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
this.endpoints.add(channelfuture);
}
return channelfuture.channel().localAddress();
}
SdkServer.java 文件源码
项目:DistributedID
阅读 27
收藏 0
点赞 0
评论 0
@Override
public void init() {
super.init();
b.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_BACKLOG, 1024)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(defLoopGroup,
new SdkServerDecoder(12), // 自定义解码器
new SdkServerEncoder(), // 自定义编码器
new SdkServerHandler(snowFlake) // 自定义处理器
);
}
});
}
TF2UdpServer.java 文件源码
项目:Mods
阅读 31
收藏 0
点赞 0
评论 0
public void run() {
try {
Bootstrap boot = new Bootstrap();
boot.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
channel = ch;
ch.pipeline().addLast(new UdpChannelHandlerServer(TF2UdpServer.this));
}
});
boot.bind(port).sync().channel().closeFuture();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
NetworkEngine.java 文件源码
项目:candlelight
阅读 27
收藏 0
点赞 0
评论 0
public NetworkDispatcher connectToLocal(SocketAddress address)
{
NetworkDispatcher dispatch = new NetworkDispatcher(this, NetworkSide.CLIENT);
final EventLoopGroup boss = new DefaultEventLoopGroup();
final Bootstrap b = new Bootstrap()
.group(boss)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception
{
ch.pipeline().addLast(dispatch);
}
})
.channel(LocalChannel.class);
//Connect and wait until done
b.connect(address).syncUninterruptibly();
return dispatch;
}
Server.java 文件源码
项目:qonduit
阅读 32
收藏 0
点赞 0
评论 0
protected ChannelHandler setupWSChannel(SslContext sslCtx, Configuration conf, DataStore datastore) {
return new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("ssl", sslCtx.newHandler(ch.alloc()));
ch.pipeline().addLast("httpServer", new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(8192));
ch.pipeline().addLast("sessionExtractor", new WebSocketHttpCookieHandler(config));
ch.pipeline().addLast("idle-handler", new IdleStateHandler(conf.getWebsocket().getTimeout(), 0, 0));
ch.pipeline().addLast("ws-protocol", new WebSocketServerProtocolHandler(WS_PATH, null, true));
ch.pipeline().addLast("wsDecoder", new WebSocketRequestDecoder(datastore, config));
ch.pipeline().addLast("error", new WSExceptionHandler());
}
};
}
TestAsyncIPC.java 文件源码
项目:ditb
阅读 36
收藏 0
点赞 0
评论 0
@Override
protected AsyncRpcClient createRpcClientRTEDuringConnectionSetup(Configuration conf) {
setConf(conf);
return new AsyncRpcClient(conf, new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
throws Exception {
promise.setFailure(new RuntimeException("Injected fault"));
}
});
}
});
}
TestClient.java 文件源码
项目:JPRE
阅读 33
收藏 0
点赞 0
评论 0
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("connected server...");
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ByteArrayDecoder());
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture cf = b.connect().sync();
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
Application.java 文件源码
项目:iot-platform
阅读 29
收藏 0
点赞 0
评论 0
public void start(int port) throws Exception {
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 ch) throws Exception {
ch.pipeline().addLast(new MqttDecoder());
ch.pipeline().addLast(MqttEncoder.INSTANCE);
ch.pipeline().addLast(new MqttInBoundHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future = bootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
ProxyClientChannelInitializerFactory.java 文件源码
项目:heimdall-proxy
阅读 20
收藏 0
点赞 0
评论 0
public static ChannelInitializer<SocketChannel> getChannelInitializer(ProxyDefinition definition, ProxyClient client) {
ChannelInitializer<SocketChannel> result = null;
if(definition instanceof TcpProxyDefinition && client instanceof TcpProxyClient) {
result = new TcpProxyClientChannelInitializer(
(TcpProxyDefinition) definition,
(TcpProxyClient) client);
}
if(definition instanceof HttpProxyDefinition && client instanceof HttpProxyClient) {
result = new HttpProxyClientChannelInitializer(
(HttpProxyDefinition) definition,
(HttpProxyClient) client);
}
return result;
}
NettyWebSocketClient.java 文件源码
项目:firebase-admin-java
阅读 35
收藏 0
点赞 0
评论 0
@Override
public void connect() {
checkState(channel == null, "channel already initialized");
try {
TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustFactory.init((KeyStore) null);
final SslContext sslContext = SslContextBuilder.forClient()
.trustManager(trustFactory).build();
Bootstrap bootstrap = new Bootstrap();
final int port = uri.getPort() != -1 ? uri.getPort() : DEFAULT_WSS_PORT;
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(sslContext.newHandler(ch.alloc(), uri.getHost(), port));
p.addLast(
new HttpClientCodec(),
// Set the max size for the HTTP responses. This only applies to the WebSocket
// handshake response from the server.
new HttpObjectAggregator(32 * 1024),
channelHandler);
}
});
ChannelFuture channelFuture = bootstrap.connect(uri.getHost(), port);
this.channel = channelFuture.channel();
channelFuture.addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
eventHandler.onError(future.cause());
}
}
}
);
} catch (Exception e) {
eventHandler.onError(e);
}
}
NetworkExecutor.java 文件源码
项目:Quavo
阅读 25
收藏 0
点赞 0
评论 0
/**
* Starts the network for a {@link Server}.
*
* @param server The {@link Server} to use for building the network.
* @return <True> If the network started successfully.
*/
public static void start() {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new ConnectionDecoder());
pipeline.addLast("encoder", new ConnectionEncoder());
pipeline.addLast("adapter", new NetworkMessageHandler());
}
});
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
try {
bootstrap.bind(Constants.HOST_NAME, Constants.HOST_PORT).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Server successfully bootstrapped on port " + Constants.HOST_PORT + " and address " + Constants.HOST_NAME + ".");
}
NetworkManager.java 文件源码
项目:CustomWorldGen
阅读 32
收藏 0
点赞 0
评论 0
/**
* Prepares a clientside NetworkManager: establishes a connection to the socket supplied and configures the channel
* pipeline. Returns the newly created instance.
*/
@SideOnly(Side.CLIENT)
public static NetworkManager provideLocalClient(SocketAddress address)
{
final NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.CLIENTBOUND);
((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group((EventLoopGroup)CLIENT_LOCAL_EVENTLOOP.getValue())).handler(new ChannelInitializer<Channel>()
{
protected void initChannel(Channel p_initChannel_1_) throws Exception
{
p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
}
})).channel(LocalChannel.class)).connect(address).syncUninterruptibly();
return networkmanager;
}