本文整理汇总了Java中io.netty.channel.ChannelFuture.channel方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelFuture.channel方法的具体用法?Java ChannelFuture.channel怎么用?Java ChannelFuture.channel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.ChannelFuture
的用法示例。
在下文中一共展示了ChannelFuture.channel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeObject
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@Override
public Channel makeObject() throws Exception {
ChannelFuture connectFuture =
bootstrap.connect(loc.getIp(), loc.getPort());
int ticks = 10000;
while (ticks-- > 0) {
if (connectFuture.isDone()) {
return connectFuture.channel();
}
Thread.sleep(10);
}
if (!connectFuture.isDone()) {
throw new TimeoutException("connect " + loc + " timeout");
} else {
return connectFuture.channel();
}
}
示例2: handleOperationComplete
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
protected synchronized void handleOperationComplete ( final SettableFuture<Void> result, final ChannelFuture future )
{
if ( this.connectFuture != result )
{
// this should never happen
return;
}
this.connectFuture = null;
try
{
future.get ();
this.channel = future.channel ();
fireConnected ( this.channel );
result.set ( null );
}
catch ( final InterruptedException | ExecutionException e )
{
fireDisconnected ( e );
result.setException ( e );
}
}
示例3: bind
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
/**
* 启动端口绑定
* @param local
* @return
*/
protected final boolean bind(InetSocketAddress local)
{
boolean isBind=false;
try {
log.debug(getName()+"端口绑定中……"+local.toString());
ChannelFuture cf=doBind(local);
isBind=cf.channel()!=null && cf.channel().isActive();
if(isBind)
{
log.debug(getName()+"端口绑定成功!"+cf.channel());
serverCahnel=(ServerSocketChannel) cf.channel();
}else
{
log.debug(getName()+"端口绑定失败!"+cf.channel());
}
} catch (Exception e) {
log.error(e.getMessage(),e);
throw e;
}
return isBind;
}
示例4: bootstrap
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public boolean bootstrap(HostAndPort seed,
Node localNode) throws SyncException {
this.localNode = localNode;
succeeded = false;
SocketAddress sa =
new InetSocketAddress(seed.getHostText(), seed.getPort());
ChannelFuture future = bootstrap.connect(sa);
future.awaitUninterruptibly();
if (!future.isSuccess()) {
logger.debug("Could not connect to " + seed, future.cause());
return false;
}
Channel channel = future.channel();
logger.debug("[{}] Connected to {}",
localNode != null ? localNode.getNodeId() : null,
seed);
try {
channel.closeFuture().await();
} catch (InterruptedException e) {
logger.debug("Interrupted while waiting for bootstrap");
return succeeded;
}
return succeeded;
}
示例5: connect
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
protected boolean connect(String hostname, int port) {
ready = false;
if (channel == null || !channel.isActive()) {
SocketAddress sa =
new InetSocketAddress(hostname, port);
ChannelFuture future = clientBootstrap.connect(sa);
future.awaitUninterruptibly();
if (!future.isSuccess()) {
logger.error("Could not connect to " + hostname +
":" + port, future.cause());
return false;
}
channel = future.channel();
}
while (!ready && channel != null && channel.isActive()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) { }
}
if (!ready || channel == null || !channel.isActive()) {
logger.warn("Timed out connecting to {}:{}", hostname, port);
return false;
}
logger.debug("Connected to {}:{}", hostname, port);
return true;
}
示例6: connect
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@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);
}
}
示例7: notifyConnect
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
private static void notifyConnect(ChannelFuture future, Promise<Channel> promise)
{
if (future.isSuccess()) {
Channel channel = future.channel();
if (!promise.trySuccess(channel)) {
// Promise was completed in the meantime (likely cancelled), just release the channel again
channel.close();
}
}
else {
promise.tryFailure(future.cause());
}
}
示例8: start
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public void start() throws InterruptedException, CertificateException, UnrecoverableKeyException,
NoSuchAlgorithmException, KeyStoreException, KeyManagementException,
IOException {
ChannelFuture channelFuture = bindToPlainSocket();
plainServerChannel = channelFuture.channel();
if (configuration.getSsl().isEnabled()) {
ChannelFuture sslSocketFuture = bindToSslSocket();
sslServerChannel = sslSocketFuture.channel();
}
}
示例9: start
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public ChannelFuture start() throws InterruptedException {
workerGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioDatagramChannel.class).handler(new ServerChannelInitializer());
ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(port)).syncUninterruptibly();
channel = channelFuture.channel();
return channelFuture;
}
示例10: start
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@Override
public boolean start() {
boolean result = false;
do {
TcpRouteDefinition route = definition.getRoute();
if(null == route) {
break;
}
if(route.getAddress() == null || route.getAddress().isEmpty()) {
break;
}
if(route.getPort() == -1) {
break;
}
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(inbound.getClass());
bootstrap.group(inbound.eventLoop());
bootstrap.handler(new TcpProxyClientChannelInitializer(definition, this));
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.AUTO_READ, false);
ChannelFuture future = bootstrap.connect(route.getAddress(), route.getPort());
//forwarder = future.sync().channel();
outbound = future.channel();
future.addListener(listener);
} catch (Exception e) {
log.error("Failed starting tcp proxy client.", e);
outbound = null;
break;
}
result = true;
} while (false);
return result;
}
示例11: start
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@Override
public boolean start() {
boolean result = false;
do {
List<HttpRouteDefinition> routes = definition.getRoutes();
for(HttpRouteDefinition route: routes) {
if (null == route) {
break;
}
if (route.getInputUrl() == null || route.getInputUrl().isEmpty()) {
break;
}
if (route.getScheme().equals(null)) {
break;
}
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(inbound.getClass());
bootstrap.group(inbound.eventLoop());
bootstrap.handler(new HttpProxyClientChannelInitializer(definition, this));
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.AUTO_READ, false);
ChannelFuture future = bootstrap.connect(route.getHost(), route.getPort());
//forwarder = future.sync().channel();
outbound = future.channel();
future.addListener(listener);
} catch (Exception e) {
log.error("Failed starting http proxy client.", e);
outbound = null;
break;
}
}
result = true;
} while (false);
return result;
}
示例12: Bootstrap
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
@Override
public void channelRead0
(final ChannelHandlerContext ctx, final HttpRequest req) {
uri = req.getUri();
final Channel client = ctx.channel();
Bootstrap proxiedServer = new Bootstrap()
.group(client.eventLoop())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpRequestEncoder(), new Forwarder(uri, client));
}
});
ChannelFuture f = proxiedServer.connect(host);
proxiedChannel = f.channel();
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
ctx.channel().pipeline().remove(HttpResponseEncoder.class);
HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1,
req.getMethod(), req.getUri());
newReq.headers().add(req.headers());
newReq.headers().set(CONNECTION, Values.CLOSE);
future.channel().writeAndFlush(newReq);
} else {
DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1,
INTERNAL_SERVER_ERROR);
resp.headers().set(CONNECTION, Values.CLOSE);
LOG.info("Proxy " + uri + " failed. Cause: ", future.cause());
ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
client.close();
}
}
});
}
示例13: open
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
/**
* Opens the connection to the given serial port.
*
* @param address The serial port, must not be null.
* @throws IOException if opening of the channel fails.
*/
public void open(RxtxDeviceAddress address) throws IOException {
requireNonNull(address);
lock.lock();
try {
checkIfChannelIsClose();
Log.debug(SERIAL, "Opening channel at serial port '" + address.value() + "'.");
ChannelFuture future = bootstrap.connect(address).syncUninterruptibly();
if (!future.isSuccess()) {
fireOnError();
throw new IOException("Serial channel couldn't be opened!");
}
Log.debug(SERIAL, "Serial channel was successfully opened.");
currentChannel = future.channel();
} catch (Exception e) {
throw new IOException("Can not connect to '"+address.value()+"'!", e);
} finally {
lock.unlock();
}
}
示例14: run
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public void run() {
ServerBootstrap b = new ServerBootstrap();// 引导辅助程序
bossGroup = new NioEventLoopGroup(BIZGROUPSIZE);
workerGroup = new NioEventLoopGroup(BIZTHREADSIZE);
try {
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);// 设置nio类型的channel
b.childHandler(new ChannelInitializer<SocketChannel>() {// 有连接到达时会创建一个channel
protected void initChannel(SocketChannel ch) throws Exception {
logger.debug("客户端:{} 初始化", ch.remoteAddress());
// pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
ch.pipeline().addLast("frameDecoder",
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4));
ch.pipeline().addLast("decoder", msgPackDecode);
ch.pipeline().addLast("encoder", msgPackEncode);
ch.pipeline().addLast(serverHandler);
}
});
b.option(ChannelOption.SO_BACKLOG, 128);
b.childOption(ChannelOption.SO_KEEPALIVE, true);
logger.info("server start : {}", port);
ChannelFuture f = b.bind(port).sync();// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
channel = f.channel();
f.channel().closeFuture().sync();// 应用程序会一直等待,直到channel关闭
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: start
import io.netty.channel.ChannelFuture; //导入方法依赖的package包/类
public void start() throws Exception {
try {
logger.info("[info] >>> start netty server.");
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.handler(new LoggingHandler(LogLevel.INFO));
bootstrap.childHandler(initializer);
// bootstrap.option(ChannelOption.SO_BACKLOG, 128);
logger.info("[info] >>> set netty server params");
bootstrap.option(ChannelOption.SO_BACKLOG, 65535);
// 保持长连接状态
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);// 关键是这句
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
ChannelFuture cf = bootstrap.bind(port).sync();
logger.info("[info] >>> netty server bind port:[" + port + "]");
channel = cf.channel();
logger.info("[info] >>> netty server running...");
channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
logger.error("[info] >>> netty server start fail.");
} finally {
destroy();
}
}