Changed code in SocketContext.writeClientBoundSslMessage() to sync it up with the master branch (they are now identical (or nearly).
This commit is contained in:
@@ -897,22 +897,31 @@ private boolean writeClientBoundSslMessage(SocketChannel channel, MessageBuffer
|
||||
}//while//
|
||||
|
||||
if(sendMore && currentOutboundMessage != null && !currentOutboundMessage.isClosed()) {
|
||||
//Check to see if the outbound message is prepared to send more content. For chunked transfers the outbound message may be waiting for additional content from another stream and we should return later.//
|
||||
if(!currentOutboundMessage.getBuffer().hasRemaining()) {
|
||||
if(!currentOutboundMessage.loadBuffer()) {
|
||||
sendMore = false;
|
||||
//Initialize the outbound message.//
|
||||
if(!currentOutboundMessage.initialize()) {
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Couldn't initialize the currentOutboundMessage.");
|
||||
}//if//
|
||||
|
||||
if(currentOutboundMessage.getBuffer() == null) {
|
||||
currentOutboundMessage = null;
|
||||
close();
|
||||
}//if//
|
||||
else {
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Loading the current outbound message's buffer.");
|
||||
}//if//
|
||||
|
||||
//Check to see if the outbound message is prepared to send more content. For chunked transfers the outbound message may be waiting for additional content from another stream and we should return later.//
|
||||
if(!currentOutboundMessage.getBuffer().hasRemaining()) {
|
||||
//Load the buffer for the current outbound message so it is full of bytes to be sent.//
|
||||
currentOutboundMessage.loadBuffer();
|
||||
}//if//
|
||||
|
||||
//If we have an application response pending then send it now.//
|
||||
if(sendMore && currentOutboundMessage.getBuffer().hasRemaining()) {
|
||||
if(!currentOutboundMessage.isClosed()) { //currentOutboundMessage.getBuffer().hasRemaining()
|
||||
//Keep sending encrypted frames until the output buffer is full, or we run out of message to send.//
|
||||
while(key.channel().isOpen() && sendMore && (currentOutboundMessage != null) && currentOutboundMessage.getBuffer().hasRemaining()) {
|
||||
while(channel.isOpen() && sendMore && !currentOutboundMessage.isClosed() && currentOutboundMessage.getBuffer().hasRemaining()) {
|
||||
SSLEngineResult encryptResult;
|
||||
int remaining = currentOutboundMessage.getBuffer().remaining();
|
||||
|
||||
//Reset the encrypted write buffer.//
|
||||
encryptedWriteBuffer.compact();
|
||||
@@ -920,6 +929,10 @@ private boolean writeClientBoundSslMessage(SocketChannel channel, MessageBuffer
|
||||
encryptResult = sslEngine.wrap(currentOutboundMessage.getBuffer(), encryptedWriteBuffer);
|
||||
encryptedWriteBuffer.flip();
|
||||
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Encrypted " + (remaining - currentOutboundMessage.getBuffer().remaining()) + " bytes; " + currentOutboundMessage.getBuffer().remaining() + " bytes left to encrypt; Encrypted bytes available to be written: " + (encryptedWriteBuffer.remaining()) + ".");
|
||||
}//if//
|
||||
|
||||
if(encryptResult.getStatus() == Status.BUFFER_OVERFLOW) {
|
||||
//Should never happen.//
|
||||
Debug.log(new RuntimeException("Unexpected ssl engine buffer overflow."));
|
||||
@@ -938,7 +951,13 @@ private boolean writeClientBoundSslMessage(SocketChannel channel, MessageBuffer
|
||||
else if(encryptResult.getStatus() == Status.OK) {
|
||||
//Write the bytes to the stream.//
|
||||
try {
|
||||
channel.write(encryptedWriteBuffer);
|
||||
int writeCount = 0;
|
||||
|
||||
writeCount = channel.write(encryptedWriteBuffer);
|
||||
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Sent " + writeCount + " encrypted bytes.");
|
||||
}//if//
|
||||
}//try//
|
||||
catch(IOException e) {
|
||||
//Caught if the channel is forcably closed by the client. We will ignore it.//
|
||||
@@ -948,6 +967,10 @@ private boolean writeClientBoundSslMessage(SocketChannel channel, MessageBuffer
|
||||
if(encryptedWriteBuffer.hasRemaining()) {
|
||||
//Leave the data in the encrypted write buffer for the writing operation to send it.//
|
||||
sendMore = false;
|
||||
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Couldn't send all the encrypted bytes in the packet, will wait for another write event in the listener.");
|
||||
}//if//
|
||||
}//if//
|
||||
}//else if//
|
||||
else {
|
||||
@@ -957,19 +980,21 @@ private boolean writeClientBoundSslMessage(SocketChannel channel, MessageBuffer
|
||||
|
||||
//Add more content to the buffer.//
|
||||
//Note: Do this even if the last encrypted write buffer could not be fully sent - so that when it is sent there will be outbound message content.//
|
||||
if(channel.isOpen() && currentOutboundMessage != null) {
|
||||
if(!currentOutboundMessage.loadBuffer()) {
|
||||
//Wait until additional message bytes are available.//
|
||||
sendMore = false;
|
||||
if(sendMore && channel.isOpen() && !currentOutboundMessage.isClosed()) {
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|Loading the next buffer of data to be sent.");
|
||||
}//if//
|
||||
|
||||
//If the message end has been reached then the buffer will be null.//
|
||||
if(currentOutboundMessage.getBuffer() == null) {
|
||||
currentOutboundMessage = null;
|
||||
currentOutboundMessage.loadBuffer();
|
||||
sendMore = !currentOutboundMessage.isClosed() && currentOutboundMessage.getBuffer().hasRemaining();
|
||||
|
||||
if(getWebServer().debug()) {
|
||||
Debug.log(this.getId() + "|" + System.nanoTime() + "|" + (currentOutboundMessage.isClosed() ? "The current message is closed." : "Remaining bytes on the current outbound message: " + currentOutboundMessage.getBuffer().remaining() + "."));
|
||||
}//if//
|
||||
}//if//
|
||||
}//while//
|
||||
}//if//
|
||||
}//else//
|
||||
}//if//
|
||||
}//try//
|
||||
catch(ClosedChannelException e) {
|
||||
|
||||
Reference in New Issue
Block a user