public void doSend()

in client/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLSender.java [161:287]


    public void doSend()
    {
        if (closed.get() && !_sslStatus.getSslErrorFlag())
        {
            throw new SenderException("SSL Sender is closed");
        }

        HandshakeStatus handshakeStatus;
        Status status;

        while(!_pending.isEmpty() && !_sslStatus.getSslErrorFlag())
        {
            int read = 0;
            try
            {
                SSLEngineResult result = ByteBufferUtils.encryptSSL(engine, _pending, netData);

                while(!_pending.isEmpty())
                {
                    ByteBuffer buf = _pending.peek();
                    if (buf.hasRemaining())
                    {
                        break;
                    }
                    _pending.poll();
                }

                read   = result.bytesProduced();
                status = result.getStatus();
                handshakeStatus = result.getHandshakeStatus();
            }
            catch(SSLException e)
            {
                // Should this set _sslError??
                throw new SenderException("SSL, Error occurred while encrypting data",e);
            }

            if(read > 0)
            {
                int limit = netData.limit();
                netData.limit(netData.position());
                netData.position(netData.position() - read);

                ByteBuffer data = netData.slice();

                netData.limit(limit);
                netData.position(netData.position() + read);

                delegate.send(data);
            }

            switch(status)
            {
                case CLOSED:
                    throw new SenderException("SSLEngine is closed");

                case BUFFER_OVERFLOW:
                    netData.clear();
                    continue;

                case OK:
                    break; // do nothing

                default:
                    throw new IllegalStateException("SSLReceiver: Invalid State " + status);
            }

            switch (handshakeStatus)
            {
                case NEED_WRAP:
                    if (netData.hasRemaining())
                    {
                        continue;
                    }
                    break;
                case NEED_TASK:
                    doTasks();
                    break;

                case NEED_UNWRAP:
                    delegate.flush();
                    synchronized(_sslStatus.getSslLock())
                    {
                        if (_sslStatus.getSslErrorFlag())
                        {
                            break;
                        }

                        switch (engine.getHandshakeStatus())
                        {
                            case NEED_UNWRAP:
                                final long start = System.currentTimeMillis();
                                try
                                {
                                    _sslStatus.getSslLock().wait(timeout);
                                }
                                catch(InterruptedException e)
                                {
                                    // pass
                                }

                                if (!_sslStatus.getSslErrorFlag() && System.currentTimeMillis() - start >= timeout)
                                {
                                    throw new SenderException(
                                            "SSL Engine timed out after waiting " + timeout + "ms. for a response." +
                                            "To get more info,run with -Djavax.net.debug=ssl");
                                }
                                break;
                        }
                    }
                    break;

                case FINISHED:
                    if (_hostname != null)
                    {
                        SSLUtil.verifyHostname(engine, _hostname);
                    }
                    break;
                case NOT_HANDSHAKING:
                    break; //do  nothing

                default:
                    throw new IllegalStateException("SSLSender: Invalid State " + status);
            }

        }
    }