Code example for SocketChannel

Methods: close, isOpen, socket, write

0
 
    private boolean write0() throws IOException { 
        // 检查是否有遗留数据未写出 
        ByteBuffer buffer = writeQueue.attachment();
        if (buffer != null) {
            int written = channel.write(buffer);
            if (written > 0) {
                netOutBytes += written;
                processor.addNetOutBytes(written);
            } 
            lastWriteTime = TimeUtil.currentTimeMillis();
            if (buffer.hasRemaining()) {
                writeAttempts++;
                return false; 
            } else { 
                writeQueue.attach(null);
                processor.getBufferPool().recycle(buffer);
            } 
        } 
        // 写出发送队列中的数据块 
        if ((buffer = writeQueue.poll()) != null) {
            // 如果是一块未使用过的buffer,则执行关闭连接。 
            if (buffer.position() == 0) {
                processor.getBufferPool().recycle(buffer);
                close(); 
                return true; 
            } 
            buffer.flip();
            int written = channel.write(buffer);
            if (written > 0) {
                netOutBytes += written;
                processor.addNetOutBytes(written);
            } 
            lastWriteTime = TimeUtil.currentTimeMillis();
            if (buffer.hasRemaining()) {
                writeQueue.attach(buffer);
                writeAttempts++;
                return false; 
            } else { 
                processor.getBufferPool().recycle(buffer);
            } 
        } 
        return true; 
    } 
 
    /** 
     * 打开写事件 
     */ 
    private void enableWrite() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
            key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
        } finally { 
            lock.unlock();
        } 
        processKey.selector().wakeup();
    } 
 
    /** 
     * 关闭写事件 
     */ 
    private void disableWrite() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
            key.interestOps(key.interestOps() & OP_NOT_WRITE);
        } finally { 
            lock.unlock();
        } 
    } 
 
    private void clearSelectionKey() { 
        final Lock lock = this.keyLock;
        lock.lock();
        try { 
            SelectionKey key = this.processKey;
            if (key != null && key.isValid()) {
                key.attach(null);
                key.cancel();
            } 
        } finally { 
            lock.unlock();
        } 
    } 
 
    private boolean closeSocket() { 
        clearSelectionKey(); 
        SocketChannel channel = this.channel;
        if (channel != null) {
            boolean isSocketClosed = true;
            Socket socket = channel.socket();
            if (socket != null) {
                try { 
                    socket.close();
                } catch (Throwable e) {
                } 
                isSocketClosed = socket.isClosed();
            } 
            try { 
                channel.close();
            } catch (Throwable e) {
            } 
            return isSocketClosed && (!channel.isOpen());
        } else { 
            return true; 
        } 
    }