in httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java [460:534]
private void updateEventMask() {
this.session.getLock().lock();
try {
// Graceful session termination
if (this.status == Status.ACTIVE
&& (this.endOfStream || this.sslEngine.isInboundDone())) {
this.status = Status.CLOSING;
final FutureCallback<SSLSession> resultCallback = handshakeCallbackRef.getAndSet(null);
if (resultCallback != null) {
resultCallback.failed(new SSLHandshakeException("TLS handshake failed"));
}
}
if (this.status == Status.CLOSING && !this.outEncrypted.hasData()) {
this.sslEngine.closeOutbound();
this.outboundClosedCount.incrementAndGet();
}
final HandshakeStatus handshakeStatus = this.sslEngine.getHandshakeStatus();
if (this.status == Status.CLOSING
&& (handshakeStatus == HandshakeStatus.NOT_HANDSHAKING || handshakeStatus == HandshakeStatus.FINISHED)
&& !this.outEncrypted.hasData()
&& this.sslEngine.isOutboundDone()
&& (this.endOfStream || this.sslEngine.isInboundDone())
&& appClosed) {
this.status = Status.CLOSED;
}
// Abnormal session termination
if (this.status.compareTo(Status.CLOSING) <= 0 && this.endOfStream
&& handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
this.status = Status.CLOSED;
}
if (this.status == Status.CLOSED) {
this.session.close();
if (sessionEndCallback != null) {
sessionEndCallback.execute(this);
}
return;
}
// Is there a task pending?
if (handshakeStatus == HandshakeStatus.NEED_TASK) {
doRunTask();
}
// Need to toggle the event mask for this channel?
final int oldMask = this.session.getEventMask();
int newMask = oldMask;
switch (this.sslEngine.getHandshakeStatus()) {
case NEED_WRAP:
newMask = EventMask.READ_WRITE;
break;
case NEED_UNWRAP:
newMask = EventMask.READ;
break;
case NOT_HANDSHAKING:
newMask = this.appEventMask;
break;
}
if (this.endOfStream && !this.inPlain.hasData()) {
newMask = newMask & ~EventMask.READ;
} else if (this.status == Status.CLOSING) {
newMask = newMask | EventMask.READ;
}
// Do we have encrypted data ready to be sent?
if (this.outEncrypted.hasData()) {
newMask = newMask | EventMask.WRITE;
}
// Update the mask if necessary
if (oldMask != newMask) {
this.session.setEventMask(newMask);
}
} finally {
this.session.getLock().unlock();
}
}