Search in sources :

Example 1 with ClosedChannelException

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);
    }
}
Also used : ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) IOException(java.io.IOException)

Example 2 with ClosedChannelException

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();
    }
}
Also used : DefaultChannelFuture(com.generallycloud.baseio.protocol.DefaultChannelFuture) ChannelFuture(com.generallycloud.baseio.protocol.ChannelFuture) ClosedChannelException(com.generallycloud.baseio.ClosedChannelException)

Example 3 with ClosedChannelException

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);
    }
}
Also used : ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) ProtocolEncoder(com.generallycloud.baseio.protocol.ProtocolEncoder) ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 4 with ClosedChannelException

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();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 5 with ClosedChannelException

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");
    }
}
Also used : ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) Authority(com.generallycloud.baseio.container.authority.Authority) OnFuture(com.generallycloud.baseio.container.OnFuture) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) DatagramPacket(com.generallycloud.baseio.protocol.DatagramPacket) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture) Waiter(com.generallycloud.baseio.concurrent.Waiter)

Aggregations

ClosedChannelException (com.generallycloud.baseio.ClosedChannelException)5 IOException (java.io.IOException)4 SSLException (javax.net.ssl.SSLException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 Waiter (com.generallycloud.baseio.concurrent.Waiter)1 OnFuture (com.generallycloud.baseio.container.OnFuture)1 Authority (com.generallycloud.baseio.container.authority.Authority)1 ChannelFuture (com.generallycloud.baseio.protocol.ChannelFuture)1 DatagramPacket (com.generallycloud.baseio.protocol.DatagramPacket)1 DefaultChannelFuture (com.generallycloud.baseio.protocol.DefaultChannelFuture)1 Future (com.generallycloud.baseio.protocol.Future)1 ProtocolEncoder (com.generallycloud.baseio.protocol.ProtocolEncoder)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1