Search in sources :

Example 61 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project SMSGate by Lihuanghe.

the class CmppTerminateRequestMessageHandler method channelRead0.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jboss.netty.channel.SimpleChannelUpstreamHandler#messageReceived(
	 * org.jboss.netty.channel.ChannelHandlerContext,
	 * org.jboss.netty.channel.MessageEvent)
	 */
@Override
public void channelRead0(final ChannelHandlerContext ctx, CmppTerminateRequestMessage e) throws Exception {
    CmppTerminateResponseMessage responseMessage = new CmppTerminateResponseMessage(e.getHeader().getSequenceId());
    ChannelFuture future = ctx.channel().writeAndFlush(responseMessage);
    final ChannelHandlerContext finalctx = ctx;
    future.addListeners(new GenericFutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            ctx.executor().schedule(new Runnable() {

                @Override
                public void run() {
                    finalctx.channel().close();
                }
            }, 500, TimeUnit.MILLISECONDS);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) CmppTerminateResponseMessage(com.zx.sms.codec.cmpp.msg.CmppTerminateResponseMessage)

Example 62 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project SMSGate by Lihuanghe.

the class MessageLogHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    final Object finalmsg = msg;
    ctx.write(msg, promise);
    promise.addListener(new GenericFutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            // 如果发送消息失败,记录失败日志
            if (!future.isSuccess()) {
                logger.error("ErrSend:{},cause by", finalmsg, future.cause());
            } else {
                logger.debug("Send:{}", finalmsg);
            }
        }
    });
}
Also used : Future(io.netty.util.concurrent.Future) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 63 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project SMSGate by Lihuanghe.

the class SMGPExitMessageHandler method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, SMGPExitMessage msg) throws Exception {
    SMGPExitRespMessage resp = new SMGPExitRespMessage();
    resp.setSequenceNo(msg.getSequenceNo());
    ChannelFuture future = ctx.channel().writeAndFlush(resp);
    final ChannelHandlerContext finalctx = ctx;
    future.addListeners(new GenericFutureListener() {

        @Override
        public void operationComplete(Future future) throws Exception {
            ctx.executor().schedule(new Runnable() {

                @Override
                public void run() {
                    finalctx.channel().close();
                }
            }, 500, TimeUnit.MILLISECONDS);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SMGPExitRespMessage(com.zx.sms.codec.smgp.msg.SMGPExitRespMessage) Future(io.netty.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Example 64 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project DuoDuo by qiunet.

the class DSession method connect.

/**
 * 连接
 */
private void connect() {
    Preconditions.checkNotNull(connectParam);
    if (!connecting.compareAndSet(false, true)) {
        return;
    }
    GenericFutureListener<ChannelFuture> listener = f -> {
        if (!f.isSuccess()) {
            throw new CustomException("Tcp Connect fail!");
        }
        try {
            sessionLock.lock();
            DMessageContentFuture msg = queue.poll();
            if (msg != null) {
                // 第一个协议一般是鉴权协议. 先发送. 等发送成功再发送后面的协议.
                IDSessionFuture future = this.doSendMessage(msg.getMessage(), true);
                future.addListener(f0 -> {
                    if (f0.isSuccess()) {
                        msg.complete(f0);
                        DMessageContentFuture msg0;
                        while ((msg0 = queue.poll()) != null) {
                            if (msg0.isCanceled()) {
                                continue;
                            }
                            IDSessionFuture future1 = this.doSendMessage(msg0.getMessage(), false);
                            DMessageContentFuture finalMsg = msg0;
                            future1.addListener(f1 -> {
                                if (f1.isSuccess()) {
                                    finalMsg.complete(f1);
                                }
                            });
                        }
                    }
                    this.flush0();
                });
            }
            connecting.set(false);
        } finally {
            sessionLock.unlock();
        }
    };
    ChannelFuture connectFuture = connectParam.connect();
    connectFuture.channel().attr(ServerConstants.SESSION_KEY).set(this);
    this.setChannel(connectFuture.channel());
    connectFuture.addListener(listener);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AttributeKey(io.netty.util.AttributeKey) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) DChannelFutureWrapper(org.qiunet.flash.handler.context.session.future.DChannelFutureWrapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LoggerType(org.qiunet.utils.logger.LoggerType) IChannelMessageSender(org.qiunet.flash.handler.context.sender.IChannelMessageSender) CloseCause(org.qiunet.flash.handler.netty.server.constants.CloseCause) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DMessageContentFuture(org.qiunet.flash.handler.context.session.future.DMessageContentFuture) IDSessionFuture(org.qiunet.flash.handler.context.session.future.IDSessionFuture) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DSessionConfig(org.qiunet.flash.handler.context.session.config.DSessionConfig) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) DSessionConnectParam(org.qiunet.flash.handler.context.session.config.DSessionConnectParam) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IChannelMessage(org.qiunet.flash.handler.context.response.push.IChannelMessage) StringJoiner(java.util.StringJoiner) Preconditions(com.google.common.base.Preconditions) ServerConstants(org.qiunet.flash.handler.netty.server.constants.ServerConstants) ChannelUtil(org.qiunet.flash.handler.util.ChannelUtil) CustomException(org.qiunet.utils.exceptions.CustomException) IMessageActor(org.qiunet.flash.handler.common.player.IMessageActor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CustomException(org.qiunet.utils.exceptions.CustomException) IDSessionFuture(org.qiunet.flash.handler.context.session.future.IDSessionFuture) DMessageContentFuture(org.qiunet.flash.handler.context.session.future.DMessageContentFuture)

Example 65 with GenericFutureListener

use of io.netty.util.concurrent.GenericFutureListener in project qpid-protonj2 by apache.

the class WebSocketTransport method write.

@Override
public WebSocketTransport write(ProtonBuffer output, Runnable onComplete) throws IOException {
    checkConnected();
    int length = output.getReadableBytes();
    if (length == 0) {
        return this;
    }
    LOG.trace("Attempted write of: {} bytes", length);
    if (onComplete == null) {
        channel.write(new BinaryWebSocketFrame(toOutputBuffer(output)), channel.voidPromise());
    } else {
        channel.write(new BinaryWebSocketFrame(toOutputBuffer(output)), channel.newPromise().addListener(new GenericFutureListener<Future<? super Void>>() {

            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                if (future.isSuccess()) {
                    onComplete.run();
                }
            }
        }));
    }
    return this;
}
Also used : BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) Future(io.netty.util.concurrent.Future) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener)

Aggregations

GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)70 Future (io.netty.util.concurrent.Future)44 ChannelFuture (io.netty.channel.ChannelFuture)32 Channel (io.netty.channel.Channel)19 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)18 IOException (java.io.IOException)18 List (java.util.List)15 InetSocketAddress (java.net.InetSocketAddress)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)12 ChannelOption (io.netty.channel.ChannelOption)10 Future (io.vertx.core.Future)10 Handler (io.vertx.core.Handler)10 ContextInternal (io.vertx.core.impl.ContextInternal)10 VertxInternal (io.vertx.core.impl.VertxInternal)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 AsyncResult (io.vertx.core.AsyncResult)8