public WriteRequest enqueueWriteRequest()

in core/src/main/java/org/apache/mina/transport/nio/AbstractNioSession.java [169:251]


    public WriteRequest enqueueWriteRequest(WriteRequest writeRequest) {
        if (IS_DEBUG) {
            LOG.debug("enqueueWriteRequest {}", writeRequest);
        }

        if (isSecured()) {
            // SSL/TLS : we have to encrypt the message
            SslHelper sslHelper = getAttribute(SSL_HELPER, null);

            if (sslHelper == null) {
                throw new IllegalStateException();
            }

            if (!writeRequest.isSecureInternal()) {
                writeRequest = sslHelper.processWrite(this, writeRequest.getMessage(), writeQueue);
            }
        }

        if (writeRequest != null) {
            ByteBuffer message = (ByteBuffer) writeRequest.getMessage();

            if (writeQueue.isEmpty()) {
                // Transfer the buffer in a DirectByteBuffer if it's a HeapByteBuffer and if it's too big
                message = convertToDirectBuffer(writeRequest, false);

                // We don't have anything in the writeQueue, let's try to write the
                // data in the channel immediately if we can
                int written = writeDirect(writeRequest.getMessage());

                if (IS_DEBUG) {
                    LOG.debug("wrote {} bytes to {}", written, this);
                }

                if (written > 0) {
                    incrementWrittenBytes(written);
                }

                // Update the idle status for this session
                idleChecker.sessionWritten(this, System.currentTimeMillis());
                int remaining = message.remaining();

                if ((written < 0) || (remaining > 0)) {
                    // Create a DirectBuffer unconditionally
                    convertToDirectBuffer(writeRequest, true);

                    // We have to push the request on the writeQueue
                    writeQueue.add(writeRequest);

                    // If it wasn't, we register this session as interested to write.
                    // It's done in atomic fashion for avoiding two concurrent registering.
                    if (!registeredForWrite.getAndSet(true)) {
                        flushWriteQueue();
                    }
                } else {
                    // The message has been fully written : update the stats, and signal the handler
                    // generate the message sent event
                    // complete the future if we have one (we should...)
                    final DefaultWriteFuture future = (DefaultWriteFuture) writeRequest.getFuture();

                    if (future != null) {
                        future.complete();
                    }

                    final Object highLevel = ((DefaultWriteRequest) writeRequest).getOriginalMessage();

                    if ((highLevel != null) && writeRequest.isConfirmRequested()) {
                        processMessageSent(highLevel);
                    }
                }
            } else {
                // Transfer the buffer in a DirectByteBuffer if it's a HeapByteBuffer
                message = convertToDirectBuffer(writeRequest, true);

                // We have to push the request on the writeQueue
                writeQueue.add(writeRequest);
                if (!registeredForWrite.getAndSet(true)) {
                    flushWriteQueue();
                }
            }
        }

        return writeRequest;
    }