Search in sources :

Example 1 with Packet

use of org.apache.zookeeper.ClientCnxn.Packet in project zookeeper by apache.

the class ClientCnxnSocketNIO method doIO.

/**
     * @return true if a packet was received
     * @throws InterruptedException
     * @throws IOException
     */
void doIO(List<Packet> pendingQueue, ClientCnxn cnxn) throws InterruptedException, IOException {
    SocketChannel sock = (SocketChannel) sockKey.channel();
    if (sock == null) {
        throw new IOException("Socket is null!");
    }
    if (sockKey.isReadable()) {
        int rc = sock.read(incomingBuffer);
        if (rc < 0) {
            throw new EndOfStreamException("Unable to read additional data from server sessionid 0x" + Long.toHexString(sessionId) + ", likely server has closed socket");
        }
        if (!incomingBuffer.hasRemaining()) {
            incomingBuffer.flip();
            if (incomingBuffer == lenBuffer) {
                recvCount++;
                readLength();
            } else if (!initialized) {
                readConnectResult();
                enableRead();
                if (findSendablePacket(outgoingQueue, sendThread.tunnelAuthInProgress()) != null) {
                    // Since SASL authentication has completed (if client is configured to do so),
                    // outgoing packets waiting in the outgoingQueue can now be sent.
                    enableWrite();
                }
                lenBuffer.clear();
                incomingBuffer = lenBuffer;
                updateLastHeard();
                initialized = true;
            } else {
                sendThread.readResponse(incomingBuffer);
                lenBuffer.clear();
                incomingBuffer = lenBuffer;
                updateLastHeard();
            }
        }
    }
    if (sockKey.isWritable()) {
        Packet p = findSendablePacket(outgoingQueue, sendThread.tunnelAuthInProgress());
        if (p != null) {
            updateLastSend();
            // If we already started writing p, p.bb will already exist
            if (p.bb == null) {
                if ((p.requestHeader != null) && (p.requestHeader.getType() != OpCode.ping) && (p.requestHeader.getType() != OpCode.auth)) {
                    p.requestHeader.setXid(cnxn.getXid());
                }
                p.createBB();
            }
            sock.write(p.bb);
            if (!p.bb.hasRemaining()) {
                sentCount++;
                outgoingQueue.removeFirstOccurrence(p);
                if (p.requestHeader != null && p.requestHeader.getType() != OpCode.ping && p.requestHeader.getType() != OpCode.auth) {
                    synchronized (pendingQueue) {
                        pendingQueue.add(p);
                    }
                }
            }
        }
        if (outgoingQueue.isEmpty()) {
            // No more packets to send: turn off write interest flag.
            // Will be turned on later by a later call to enableWrite(),
            // from within ZooKeeperSaslClient (if client is configured
            // to attempt SASL authentication), or in either doIO() or
            // in doTransport() if not.
            disableWrite();
        } else if (!initialized && p != null && !p.bb.hasRemaining()) {
            // On initial connection, write the complete connect request
            // packet, but then disable further writes until after
            // receiving a successful connection response.  If the
            // session is expired, then the server sends the expiration
            // response and immediately closes its end of the socket.  If
            // the client is simultaneously writing on its end, then the
            // TCP stack may choose to abort with RST, in which case the
            // client would never receive the session expired event.  See
            // http://docs.oracle.com/javase/6/docs/technotes/guides/net/articles/connection_release.html
            disableWrite();
        } else {
            // Just in case
            enableWrite();
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Packet(org.apache.zookeeper.ClientCnxn.Packet) EndOfStreamException(org.apache.zookeeper.ClientCnxn.EndOfStreamException) IOException(java.io.IOException)

Example 2 with Packet

use of org.apache.zookeeper.ClientCnxn.Packet in project zookeeper by apache.

the class ClientCnxnSocketNetty method cleanup.

@Override
void cleanup() {
    connectLock.lock();
    try {
        if (connectFuture != null) {
            connectFuture.cancel();
            connectFuture = null;
        }
        if (channel != null) {
            channel.close().awaitUninterruptibly();
            channel = null;
        }
    } finally {
        connectLock.unlock();
    }
    Iterator<Packet> iter = outgoingQueue.iterator();
    while (iter.hasNext()) {
        Packet p = iter.next();
        if (p == WakeupPacket.getInstance()) {
            iter.remove();
        }
    }
}
Also used : Packet(org.apache.zookeeper.ClientCnxn.Packet)

Example 3 with Packet

use of org.apache.zookeeper.ClientCnxn.Packet in project zookeeper by apache.

the class ClientCnxnSocketNIO method findSendablePacket.

private Packet findSendablePacket(LinkedBlockingDeque<Packet> outgoingQueue, boolean tunneledAuthInProgres) {
    if (outgoingQueue.isEmpty()) {
        return null;
    }
    // If we've already starting sending the first packet, we better finish
    if (outgoingQueue.getFirst().bb != null || !tunneledAuthInProgres) {
        return outgoingQueue.getFirst();
    }
    // Since client's authentication with server is in progress,
    // send only the null-header packet queued by primeConnection().
    // This packet must be sent so that the SASL authentication process
    // can proceed, but all other packets should wait until
    // SASL authentication completes.
    Iterator<Packet> iter = outgoingQueue.iterator();
    while (iter.hasNext()) {
        Packet p = iter.next();
        if (p.requestHeader == null) {
            // We've found the priming-packet. Move it to the beginning of the queue.
            iter.remove();
            outgoingQueue.addFirst(p);
            return p;
        } else {
            // Non-priming packet: defer it until later, leaving it in the queue
            // until authentication completes.
            LOG.debug("deferring non-priming packet {} until SASL authentation completes.", p);
        }
    }
    return null;
}
Also used : Packet(org.apache.zookeeper.ClientCnxn.Packet)

Aggregations

Packet (org.apache.zookeeper.ClientCnxn.Packet)3 IOException (java.io.IOException)1 SocketChannel (java.nio.channels.SocketChannel)1 EndOfStreamException (org.apache.zookeeper.ClientCnxn.EndOfStreamException)1