Skip to content

Commit

Permalink
Simplified the HttpClient and HttpServer statement machines.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed May 15, 2018
1 parent 843bdd7 commit bf9caa6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 75 deletions.
87 changes: 40 additions & 47 deletions Sming/SmingCore/Network/Http/HttpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,72 +404,65 @@ int HttpConnection::staticOnChunkComplete(http_parser* parser)

void HttpConnection::onReadyToSendData(TcpConnectionEvent sourceEvent)
{

debug_d("HttpConnection::onReadyToSendData: waitingQueue.count: %d", waitingQueue->count());

do {
if(state == eHCS_Sent) {
state = eHCS_Ready;
REENTER:
switch(state) {
case eHCS_Ready: {
HttpRequest* request = waitingQueue->peek();
if(request == NULL) {
debug_d("Nothing in the waiting queue");
outgoingRequest = NULL;
break;
}

if(state == eHCS_Ready) {
HttpRequest* request = waitingQueue->peek();
if(request == NULL) {
debug_d("Nothing in the waiting queue");
outgoingRequest = NULL;
// if the executionQueue is not empty then we have to check if we can pipeline that request
if(executionQueue.count()) {
if(!(request->method == HTTP_GET || request->method == HTTP_HEAD)) {
// if the current request cannot be pipelined -> break;
break;
}

// if the executionQueue is not empty then we have to check if we can pipeline that request
if(executionQueue.count()) {
if(!(request->method == HTTP_GET || request->method == HTTP_HEAD)) {
// if the current request cannot be pipelined -> break;
// if we have previous request
if(outgoingRequest != NULL) {
if(!(outgoingRequest->method == HTTP_GET || outgoingRequest->method == HTTP_HEAD)) {
// the outgoing request does not allow pipelining
break;
}

// if we have previous request
if(outgoingRequest != NULL) {
if(!(outgoingRequest->method == HTTP_GET || outgoingRequest->method == HTTP_HEAD)) {
// the outgoing request does not allow pipelining
break;
}
}
} // executionQueue.count()

if(!executionQueue.enqueue(request)) {
debug_e("The working queue is full at the moment");
break;
}
} // executionQueue.count()

waitingQueue->dequeue();

outgoingRequest = request;
state = eHCS_SendingHeaders;
sendRequestHeaders(request);

if(!executionQueue.enqueue(request)) {
debug_e("The working queue is full at the moment");
break;
}

if(state >= eHCS_StartSending && state < eHCS_Sent) {
if(state == eHCS_SendingHeaders) {
if(stream != NULL && !stream->isFinished()) {
break;
}
waitingQueue->dequeue();

state = eHCS_StartBody;
}
outgoingRequest = request;
sendRequestHeaders(request);

if(sendRequestBody(outgoingRequest)) {
state = eHCS_Sent;
delete stream;
stream = NULL;
continue;
}
state = eHCS_SendingHeaders;
}

case eHCS_SendingHeaders: {
if(stream != NULL && !stream->isFinished()) {
break;
}

break;
state = eHCS_StartBody;
}

} while(true);
case eHCS_StartBody:
case eHCS_SendingBody: {
if(sendRequestBody(outgoingRequest)) {
state = eHCS_Ready;
delete stream;
stream = NULL;
goto REENTER;
}
}
} // switch(state)

TcpClient::onReadyToSendData(sourceEvent);
}
Expand Down
52 changes: 24 additions & 28 deletions Sming/SmingCore/Network/Http/HttpServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "HttpServer.h"
#include "TcpServer.h"
#include "../../Services/cWebsocket/websocket.h"
#include "WebConstants.h"
#include "../../Data/Stream/ChunkedStream.h"

Expand Down Expand Up @@ -370,48 +369,45 @@ err_t HttpServerConnection::onReceive(pbuf *buf)

void HttpServerConnection::onReadyToSendData(TcpConnectionEvent sourceEvent)
{
if(state == eHCS_Sent) {
state = eHCS_Ready;
switch(state) {
case eHCS_StartSending: {
sendResponseHeaders(&response);
state = eHCS_SendingHeaders;
}

do {

if(!(state >= eHCS_StartSending && state < eHCS_Sent)) {
case eHCS_SendingHeaders: {
if (stream != NULL && !stream->isFinished()) {
break;
}

if(state == eHCS_StartSending) {
sendResponseHeaders(&response);
state = eHCS_SendingHeaders;
state = eHCS_StartBody;
}

case eHCS_StartBody:
case eHCS_SendingBody: {
if(!sendResponseBody(&response)) {
break;
}

if(state == eHCS_SendingHeaders) {
if(stream != NULL && !stream->isFinished()) {
break;
}

state = eHCS_StartBody;
}
delete stream;
stream = NULL;
state = eHCS_Sent;
}

if(sendResponseBody(&response)) {
delete stream;
stream = NULL;
state = eHCS_Sent;
case eHCS_Sent: {
if(response.headers["Connection"] == "close") {
setTimeOut(1); // decrease the timeout to 1 tick
}

break;
response.reset();
request.reset();

} while(false);
state = eHCS_Ready;

if(state == eHCS_Sent && response.headers["Connection"] == "close") {
setTimeOut(1); // decrease the timeout to 1 tick
break;
}

if(state == eHCS_Sent) {
response.reset();
request.reset();
}
} /* switch(state) */

TcpClient::onReadyToSendData(sourceEvent);
}
Expand Down

0 comments on commit bf9caa6

Please sign in to comment.