use of com.generallycloud.baseio.ClosedChannelException in project baseio by generallycloud.
the class AioSocketChannel method flush.
private void flush(boolean forceFlushing) {
try {
if (!forceFlushing && flushing) {
return;
}
if (writeFuture == null) {
writeFuture = writeFutures.poll();
}
if (writeFuture == null) {
flushing = false;
return;
}
if (!isOpened()) {
fireClosed(writeFuture, new ClosedChannelException("closed"));
return;
}
flushing = true;
writeFuture.write(this);
} catch (IOException e) {
fireClosed(writeFuture, e);
}
}
use of com.generallycloud.baseio.ClosedChannelException in project baseio by generallycloud.
the class AbstractSocketChannel method releaseFutures.
protected void releaseFutures() {
ReleaseUtil.release(readFuture);
ReleaseUtil.release(sslReadFuture);
ClosedChannelException e = null;
if (writeFuture != null && !writeFuture.isReleased()) {
e = new ClosedChannelException(session.toString());
onFutureException(writeFuture, e);
}
LinkedQueue<ChannelFuture> writeFutures = this.writeFutures;
if (writeFutures.size() == 0) {
return;
}
ChannelFuture f = writeFutures.poll();
UnsafeSocketSession session = this.session;
if (e == null) {
e = new ClosedChannelException(session.toString());
}
for (; f != null; ) {
onFutureException(f, e);
ReleaseUtil.release(f);
f = writeFutures.poll();
}
}
use of com.generallycloud.baseio.ClosedChannelException in project baseio by generallycloud.
the class AbstractSocketChannel method flush.
@Override
public void flush(ChannelFuture future) {
if (future == null || future.flushed()) {
return;
}
future.flush();
if (!isOpened()) {
exceptionCaught(getContext().getIoEventHandleAdaptor(), future, new ClosedChannelException(toString()));
return;
}
try {
ProtocolEncoder encoder = getProtocolEncoder();
// 请勿将future.flush()移到getProtocolEncoder()之前,
// 有些情况下如协议切换的时候可能需要将此future使用
// 切换前的协议flush
encoder.encode(this, future);
doFlush(future);
} catch (Exception e) {
exceptionCaught(getContext().getIoEventHandleAdaptor(), future, e);
}
}
use of com.generallycloud.baseio.ClosedChannelException in project baseio by generallycloud.
the class AbstractSocketChannel method doFlush.
@Override
public void doFlush(ChannelFuture f) {
UnsafeSocketSession session = getSession();
// 这里需要加锁,因为当多个线程同时flush时,判断(writeFutures.size() > 1)
// 会产生误差,导致无法或者延迟触发doFlush操作
ReentrantLock lock = getCloseLock();
lock.lock();
try {
// 因为doFlush与Close互斥
if (!isOpened()) {
onFutureException(f, new ClosedChannelException(session.toString()));
return;
}
writeFutures.offer(f);
int length = writeFutureLength(f.getByteBufLimit());
if (length > 1024 * 1024 * 10) {
// FIXME 该连接写入过多啦
}
// event loop 在判断complete时返回false
if (writeFutures.size() > 1) {
return;
}
doFlush0();
} catch (Exception e) {
onFutureException(f, e);
} finally {
lock.unlock();
}
}
use of com.generallycloud.baseio.ClosedChannelException in project baseio by generallycloud.
the class RTPClient method bindTCPSession.
public void bindTCPSession() throws IOException {
if (connector == null) {
throw new IllegalArgumentException("null udp connector");
}
Authority authority = session.getAuthority();
if (authority == null) {
throw new IllegalArgumentException("not login");
}
JSONObject json = new JSONObject();
json.put("serviceName", RTPServerDPAcceptor.BIND_SESSION);
json.put("username", authority.getUsername());
json.put("password", authority.getPassword());
final DatagramPacket packet = DatagramPacket.createSendPacket(json.toJSONString().getBytes(context.getEncoding()));
final String BIND_SESSION_CALLBACK = RTPServerDPAcceptor.BIND_SESSION_CALLBACK;
final Waiter<Integer> waiter = new Waiter<>();
session.listen(BIND_SESSION_CALLBACK, new OnFuture() {
@Override
public void onResponse(SocketSession session, Future future) {
waiter.setPayload(0);
}
});
final byte[] shortWaiter = new byte[] {};
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
connector.sendDatagramPacket(packet);
} catch (IOException e) {
DebugUtil.debug(e);
}
if (waiter.isDnoe()) {
break;
}
synchronized (shortWaiter) {
try {
shortWaiter.wait(300);
} catch (InterruptedException e) {
break;
}
}
}
}
});
if (waiter.await(3000)) {
CloseUtil.close(connector);
throw new ClosedChannelException("disconnected");
}
}
Aggregations