Skip to content

Commit

Permalink
Decrease the memory usage by sharing the same parser settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
slav-at-attachix committed Jul 17, 2017
1 parent 8f1436e commit e400eb7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
37 changes: 25 additions & 12 deletions Sming/SmingCore/Network/Http/HttpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,37 @@
#include "lwip/tcp_impl.h"
#endif

bool HttpConnection::parserSettingsInitialized = false;

HttpConnection::HttpConnection(RequestQueue* queue): TcpClient(false), mode(eHCM_String) {
this->waitingQueue = queue;

http_parser_init(&parser, HTTP_RESPONSE);
parser.data = (void*)this;

memset(&parserSettings, 0, sizeof(parserSettings));
if(!parserSettingsInitialized) {
memset(&parserSettings, 0, sizeof(parserSettings));

// Notification callbacks: on_message_begin, on_headers_complete, on_message_complete.
parserSettings.on_message_begin = staticOnMessageBegin;
parserSettings.on_headers_complete = staticOnHeadersComplete;
parserSettings.on_message_complete = staticOnMessageComplete;
// Notification callbacks: on_message_begin, on_headers_complete, on_message_complete.
parserSettings.on_message_begin = staticOnMessageBegin;
parserSettings.on_headers_complete = staticOnHeadersComplete;
parserSettings.on_message_complete = staticOnMessageComplete;

parserSettings.on_chunk_header = staticOnChunkHeader;
parserSettings.on_chunk_complete = staticOnChunkComplete;
#ifndef COMPACT_MODE
parserSettings.on_chunk_header = staticOnChunkHeader;
parserSettings.on_chunk_complete = staticOnChunkComplete;
#endif

// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body;
#ifndef COMPACT_MODE
parserSettings.on_status = staticOnStatus;
#endif
parserSettings.on_header_field = staticOnHeaderField;
parserSettings.on_header_value = staticOnHeaderValue;
parserSettings.on_body = staticOnBody;

// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body;
parserSettings.on_status = staticOnStatus;
parserSettings.on_header_field = staticOnHeaderField;
parserSettings.on_header_value = staticOnHeaderValue;
parserSettings.on_body = staticOnBody;
parserSettingsInitialized = true;
}
}

bool HttpConnection::connect(const String& host, int port, bool useSsl /* = false */, uint32_t sslOptions /* = 0 */) {
Expand Down Expand Up @@ -266,9 +275,11 @@ int HttpConnection::staticOnHeadersComplete(http_parser* parser)
return error;
}

#ifndef COMPACT_MODE
int HttpConnection::staticOnStatus(http_parser *parser, const char *at, size_t length) {
return 0;
}
#endif

int HttpConnection::staticOnHeaderField(http_parser *parser, const char *at, size_t length)
{
Expand Down Expand Up @@ -334,6 +345,7 @@ int HttpConnection::staticOnBody(http_parser *parser, const char *at, size_t len
return 0;
}

#ifndef COMPACT_MODE
int HttpConnection::staticOnChunkHeader(http_parser* parser) {
debugf("On chunk header");
return 0;
Expand All @@ -343,6 +355,7 @@ int HttpConnection::staticOnChunkComplete(http_parser* parser) {
debugf("On chunk complete");
return 0;
}
#endif

err_t HttpConnection::onConnected(err_t err) {
if (err == ERR_OK) {
Expand Down
7 changes: 6 additions & 1 deletion Sming/SmingCore/Network/Http/HttpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ class HttpConnection : protected TcpClient {

private:
static int IRAM_ATTR staticOnMessageBegin(http_parser* parser);
#ifndef COMPACT_MODE
static int IRAM_ATTR staticOnStatus(http_parser *parser, const char *at, size_t length);
#endif
static int IRAM_ATTR staticOnHeadersComplete(http_parser* parser);
static int IRAM_ATTR staticOnHeaderField(http_parser *parser, const char *at, size_t length);
static int IRAM_ATTR staticOnHeaderValue(http_parser *parser, const char *at, size_t length);
static int IRAM_ATTR staticOnBody(http_parser *parser, const char *at, size_t length);
#ifndef COMPACT_MODE
static int IRAM_ATTR staticOnChunkHeader(http_parser* parser);
static int IRAM_ATTR staticOnChunkComplete(http_parser* parser);
#endif
static int IRAM_ATTR staticOnMessageComplete(http_parser* parser);

protected:
Expand All @@ -105,7 +109,8 @@ class HttpConnection : protected TcpClient {
RequestQueue* waitingQueue;
RequestQueue executionQueue;
http_parser parser;
http_parser_settings parserSettings;
static http_parser_settings parserSettings;
static bool parserSettingsInitialized;
HttpHeaders responseHeaders;

int code = 0;
Expand Down
27 changes: 16 additions & 11 deletions Sming/SmingCore/Network/Http/HttpServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@
#include "../../Services/cWebsocket/websocket.h"
#include "WebConstants.h"

bool HttpServerConnection::parserSettingsInitialized = false;

HttpServerConnection::HttpServerConnection(tcp_pcb *clientTcp)
: TcpClient(clientTcp, 0, 0), state(eHCS_Ready)
{
// create parser ...
http_parser_init(&parser, HTTP_REQUEST);
parser.data = (void*)this;

memset(&parserSettings, 0, sizeof(parserSettings));
// Notification callbacks: on_message_begin, on_headers_complete, on_message_complete.
parserSettings.on_message_begin = staticOnMessageBegin;
parserSettings.on_headers_complete = staticOnHeadersComplete;
parserSettings.on_message_complete = staticOnMessageComplete;

// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body;
parserSettings.on_url = staticOnPath;
parserSettings.on_header_field = staticOnHeaderField;
parserSettings.on_header_value = staticOnHeaderValue;
parserSettings.on_body = staticOnBody;
if(!parserSettingsInitialized) {
memset(&parserSettings, 0, sizeof(parserSettings));
// Notification callbacks: on_message_begin, on_headers_complete, on_message_complete.
parserSettings.on_message_begin = staticOnMessageBegin;
parserSettings.on_headers_complete = staticOnHeadersComplete;
parserSettings.on_message_complete = staticOnMessageComplete;

// Data callbacks: on_url, (common) on_header_field, on_header_value, on_body;
parserSettings.on_url = staticOnPath;
parserSettings.on_header_field = staticOnHeaderField;
parserSettings.on_header_value = staticOnHeaderValue;
parserSettings.on_body = staticOnBody;
parserSettingsInitialized = true;
}
}

HttpServerConnection::~HttpServerConnection()
Expand Down
3 changes: 2 additions & 1 deletion Sming/SmingCore/Network/Http/HttpServerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class HttpServerConnection: public TcpClient
HttpConnectionState state;

http_parser parser;
http_parser_settings parserSettings;
static http_parser_settings parserSettings;
static bool parserSettingsInitialized;

ResourceTree* resourceTree = NULL;
HttpResource* resource = NULL;
Expand Down

0 comments on commit e400eb7

Please sign in to comment.