use of org.qiunet.flash.handler.context.session.future.IDSessionFuture 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);
}
Aggregations