From 72fa63028ea519f7ea874691116fadd65254d176 Mon Sep 17 00:00:00 2001 From: wcrisman Date: Fri, 26 Dec 2014 10:16:49 -0800 Subject: [PATCH] Revert "Fixed bugs in writeClientBoundMessage." This reverts commit a30fcb400281c1229f5546c13b211d95271334fb. --- .../com/foundation/web/server/WebServer.java | 102 +++++++++++------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/Foundation Web Core/src/com/foundation/web/server/WebServer.java b/Foundation Web Core/src/com/foundation/web/server/WebServer.java index c9cfb2e..de3f66e 100644 --- a/Foundation Web Core/src/com/foundation/web/server/WebServer.java +++ b/Foundation Web Core/src/com/foundation/web/server/WebServer.java @@ -737,7 +737,6 @@ 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; @@ -1653,7 +1652,7 @@ public class WebServer { */ protected void writeOutgoingMessages() throws IOException { Debug.log(id + "," + System.nanoTime() + "," + "Writing outgoing messages."); - if(key.channel().isOpen() && currentOutboundMessage != null && currentOutboundMessage.isClosed()) { + if(key.channel().isOpen() && currentOutboundMessage != null) { writeClientBoundMessage(); }//if// else { @@ -1971,23 +1970,30 @@ public class WebServer { // debugBuffer.append("End Handshaking SSL\n"); // }//if// }//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(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// - else { - currentOutboundMessage.loadBuffer(); - }//else// //If we have an application response pending then send it now.// - if(sendMore && !currentOutboundMessage.isClosed()) { + if(sendMore && currentOutboundMessage.getBuffer().hasRemaining()) { 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.isClosed() && currentOutboundMessage.getBuffer().hasRemaining()) { + while(key.channel().isOpen() && sendMore && (currentOutboundMessage != null) && currentOutboundMessage.getBuffer().hasRemaining()) { SSLEngineResult encryptResult; // int offset = pendingOutboundMessage.getBuffer().position(); //TODO: Comment me. @@ -2052,44 +2058,58 @@ 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.isClosed() && sendMore) { - currentOutboundMessage.loadBuffer(); - sendMore = !currentOutboundMessage.isClosed() && currentOutboundMessage.getBuffer().hasRemaining(); + 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// -// //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 unencrypted frames until the output buffer is full, or we run out of message to send.// - while(sendMore && !currentOutboundMessage.isClosed()) { + //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()) { //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 { - sendMore = currentOutboundMessage.loadBuffer(); + 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// }//else// }//while// }//else//