Fixed bugs in writeClientBoundMessage.

This commit is contained in:
wcrisman
2014-12-10 17:07:47 -08:00
parent 29ade9b7b0
commit a30fcb4002

View File

@@ -737,6 +737,7 @@ public class WebServer {
public boolean initialize() {
boolean result = true;
//Only initialize if we have not yet initialized! If we have already initialized then just return a positive result (true).//
if(response != null && getBuffer() == null) {
Request request = (Request) response.getRequest();
byte[] headerBytes = null;
@@ -1652,7 +1653,7 @@ public class WebServer {
*/
protected void writeOutgoingMessages() throws IOException {
Debug.log(id + "," + System.nanoTime() + "," + "Writing outgoing messages.");
if(key.channel().isOpen() && currentOutboundMessage != null) {
if(key.channel().isOpen() && currentOutboundMessage != null && currentOutboundMessage.isClosed()) {
writeClientBoundMessage();
}//if//
else {
@@ -1970,30 +1971,23 @@ public class WebServer {
// debugBuffer.append("End Handshaking SSL\n");
// }//if//
}//if//
if(sendMore && currentOutboundMessage != null) {
//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()) {
if(currentOutboundMessage.getBuffer() == null && currentOutboundMessage.getNext() != null) {
currentOutboundMessage = currentOutboundMessage.getNext();
}//if//
else {
sendMore = false;
}//else//
}//if//
if(currentOutboundMessage.getBuffer() == null) {
currentOutboundMessage = null;
lastOutboundMessage = null;
}//if//
//If we can send more data and we have an outbound message then initialize and send it!//
if(sendMore && currentOutboundMessage != null && !currentOutboundMessage.isClosed()) {
//Initialize the outbound message (if it is already initialized it will return true).//
if(!currentOutboundMessage.initialize()) {
//Force the channel to close due to initialization error.//
try {key.channel().close();} catch(Throwable e2) {}
}//if//
else {
currentOutboundMessage.loadBuffer();
}//else//
//If we have an application response pending then send it now.//
if(sendMore && currentOutboundMessage.getBuffer().hasRemaining()) {
if(sendMore && !currentOutboundMessage.isClosed()) {
if(sslEngine != null) {
//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(key.channel().isOpen() && sendMore && (currentOutboundMessage != null) && !currentOutboundMessage.isClosed() && currentOutboundMessage.getBuffer().hasRemaining()) {
SSLEngineResult encryptResult;
// int offset = pendingOutboundMessage.getBuffer().position();
//TODO: Comment me.
@@ -2058,58 +2052,44 @@ public class WebServer {
//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(key.channel().isOpen() && currentOutboundMessage != null) {
if(!currentOutboundMessage.loadBuffer()) {
//Load the next pending outbound message in the chain. This is currently only used for content being passed through to another process via a second socket.//
if(currentOutboundMessage.getBuffer() == null && currentOutboundMessage.getNext() != null) {
currentOutboundMessage = currentOutboundMessage.getNext();
}//if//
else {
//Wait until additional message bytes are available.//
sendMore = false;
}//else//
}//if//
//If the message end has been reached then the buffer will be null.//
if(currentOutboundMessage.getBuffer() == null) {
currentOutboundMessage = null;
lastOutboundMessage = null;
}//if//
if(key.channel().isOpen() && !currentOutboundMessage.isClosed() && sendMore) {
currentOutboundMessage.loadBuffer();
sendMore = !currentOutboundMessage.isClosed() && currentOutboundMessage.getBuffer().hasRemaining();
}//if//
// //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(key.channel().isOpen() && currentOutboundMessage != null) {
// if(!currentOutboundMessage.loadBuffer()) {
// //Load the next pending outbound message in the chain. This is currently only used for content being passed through to another process via a second socket.//
// if(currentOutboundMessage.getBuffer() == null && currentOutboundMessage.getNext() != null) {
// currentOutboundMessage = currentOutboundMessage.getNext();
// }//if//
// else {
// //Wait until additional message bytes are available.//
// sendMore = false;
// }//else//
// }//if//
//
// //If the message end has been reached then the buffer will be null.//
// if(currentOutboundMessage.getBuffer() == null) {
// currentOutboundMessage = null;
// lastOutboundMessage = null;
// }//if//
// }//if//
}//while//
}//if//
else {
//Keep sending encrypted frames until the output buffer is full, or we run out of message to send.//
while(sendMore && (currentOutboundMessage != null) && currentOutboundMessage.getBuffer().hasRemaining()) {
//Keep sending unencrypted frames until the output buffer is full, or we run out of message to send.//
while(sendMore && !currentOutboundMessage.isClosed()) {
//Write the bytes to the stream.//
((SocketChannel) key.channel()).write(currentOutboundMessage.getBuffer());
// if(debug) {
// sentBytes += pendingOutboundMessage.position();
// debugBuffer.append("Wrote " + pendingOutboundMessage.position() + " bytes to the client. Total sent: " + sentBytes + "\n");
// }//if//
//If not all the bytes could be written then we will need to wait until we can write more.//
if(currentOutboundMessage.getBuffer().hasRemaining()) {
sendMore = false;
}//if//
else {
if(!currentOutboundMessage.loadBuffer()) {
//Load the next pending outbound message in the chain. This is currently only used for content being passed through to another process via a second socket.//
if(currentOutboundMessage.getBuffer() == null && currentOutboundMessage.getNext() != null) {
currentOutboundMessage = currentOutboundMessage.getNext();
}//if//
else {
//Wait until additional message bytes are available.//
sendMore = false;
}//else//
}//if//
//If the message end has been reached then the buffer will be null.//
if(currentOutboundMessage.getBuffer() == null) {
currentOutboundMessage = null;
lastOutboundMessage = null;
}//if//
sendMore = currentOutboundMessage.loadBuffer();
}//else//
}//while//
}//else//