Search in sources :

Example 1 with DMessageContentFuture

use of org.qiunet.flash.handler.context.session.future.DMessageContentFuture in project DuoDuo by qiunet.

the class DSession method sendMessage.

@Override
public IDSessionFuture sendMessage(IChannelMessage<?> message, boolean flush) {
    if (connecting.get()) {
        try {
            sessionLock.lock();
            if (connecting.get()) {
                DMessageContentFuture contentFuture = new DMessageContentFuture(channel, message);
                this.queue.add(contentFuture);
                return contentFuture;
            }
        } finally {
            sessionLock.unlock();
        }
    }
    return this.doSendMessage(message, flush);
}
Also used : DMessageContentFuture(org.qiunet.flash.handler.context.session.future.DMessageContentFuture)

Example 2 with DMessageContentFuture

use of org.qiunet.flash.handler.context.session.future.DMessageContentFuture 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)

Aggregations

DMessageContentFuture (org.qiunet.flash.handler.context.session.future.DMessageContentFuture)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 AttributeKey (io.netty.util.AttributeKey)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)1 List (java.util.List)1 StringJoiner (java.util.StringJoiner)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 IMessageActor (org.qiunet.flash.handler.common.player.IMessageActor)1 IChannelMessage (org.qiunet.flash.handler.context.response.push.IChannelMessage)1 IChannelMessageSender (org.qiunet.flash.handler.context.sender.IChannelMessageSender)1 DSessionConfig (org.qiunet.flash.handler.context.session.config.DSessionConfig)1 DSessionConnectParam (org.qiunet.flash.handler.context.session.config.DSessionConnectParam)1