Skip to content

Commit

Permalink
Websocket commands optimized in sense of memory consumption.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAndi committed Nov 2, 2023
1 parent 309795a commit 2ca3b35
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 39 deletions.
20 changes: 15 additions & 5 deletions src/Web/WsCommand/WsCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ const char* WsCmd::NACK = "NACK";
* Protected Methods
*****************************************************************************/

void WsCmd::sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const String& msg)
void WsCmd::sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const char* msg)
{
if ((nullptr != server) &&
(nullptr != client))
{
String rsp = ACK;

if (false == msg.isEmpty())
if ((nullptr != msg) &&
('\0' != msg[0U]))
{
rsp += DELIMITER;
rsp += msg;
Expand All @@ -90,18 +91,27 @@ void WsCmd::sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* c

void WsCmd::sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* client)
{
sendPositiveResponse(server, client, "");
sendPositiveResponse(server, client, nullptr);
}

void WsCmd::sendNegativeResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const String& msg)
void WsCmd::sendNegativeResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const char* msg)
{
if ((nullptr != server) &&
(nullptr != client))
{
String rsp = NACK;

rsp += DELIMITER;
rsp += msg;

if ((nullptr != msg) &&
('\0' != msg[0U]))
{
rsp += msg;
}
else
{
rsp += "\"Unknown.\"";
}

server->text(client->id(), rsp);
}
Expand Down
40 changes: 38 additions & 2 deletions src/Web/WsCommand/WsCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,50 @@ class WsCmd
/** Negative response code. */
static const char* NACK;

/**
* Prepare a positive response message.
* The last added element will always be a delimiter.
*
* @param[out] msg Message to prepare
*/
void preparePositiveResponse(String& msg)
{
msg = ACK;
msg += DELIMITER;
}

/**
* Prepare a negative response message.
* The last added element will always be a delimiter.
*
* @param[out] msg Message to prepare
*/
void prepareNegativeResponse(String& msg)
{
msg = NACK;
msg += DELIMITER;
}

/**
* Send a response to the client.
*
* @param[in] server Websocket server which is used to send a message to the client.
* @param[in] client The client the message belongs to.
* @param[in] msg The response messsage.
*/
void sendResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const String& msg)
{
server->text(client->id(), msg);
}

/**
* Send positive response to the client.
*
* @param[in] server Websocket server which is used to send a message to the client.
* @param[in] client The client the message belongs to.
* @param[in] msg The negative response messsage.
*/
void sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const String& msg);
void sendPositiveResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const char* msg);

/**
* Send negative response to the client.
Expand All @@ -138,7 +174,7 @@ class WsCmd
* @param[in] client The client the message belongs to.
* @param[in] msg The negative response messsage.
*/
void sendNegativeResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const String& msg);
void sendNegativeResponse(AsyncWebSocket* server, AsyncWebSocketClient* client, const char* msg);

private:

Expand Down
6 changes: 4 additions & 2 deletions src/Web/WsCommand/WsCmdAlias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ void WsCmdAlias::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
(void)DisplayMgr::getInstance().setPluginAliasName(m_pluginUid, m_alias);
}

msg = "\"";
preparePositiveResponse(msg);

msg += "\"";
msg += DisplayMgr::getInstance().getPluginAliasName(m_pluginUid);
msg += "\"";

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}

m_isError = false;
Expand Down
6 changes: 4 additions & 2 deletions src/Web/WsCommand/WsCmdBrightness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ void WsCmdBrightness::execute(AsyncWebSocket* server, AsyncWebSocketClient* clie
;
}

msg = DisplayMgr::getInstance().getBrightness();
preparePositiveResponse(msg);

msg += DisplayMgr::getInstance().getBrightness();
msg += DELIMITER;
msg += (true == DisplayMgr::getInstance().getAutoBrightnessAdjustment()) ? 1 : 0;

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}

m_isError = false;
Expand Down
6 changes: 4 additions & 2 deletions src/Web/WsCommand/WsCmdEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ void WsCmdEffect::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
DisplayMgr::getInstance().activateNextFadeEffect(static_cast<DisplayMgr::FadeEffect>(m_fadeEffect));
}

msg = DisplayMgr::getInstance().getFadeEffect();
preparePositiveResponse(msg);

sendPositiveResponse(server, client, msg);
msg += DisplayMgr::getInstance().getFadeEffect();

sendResponse(server, client, msg);
}

m_isError = false;
Expand Down
43 changes: 28 additions & 15 deletions src/Web/WsCommand/WsCmdGetDisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,40 @@ void WsCmdGetDisp::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
}
else
{
uint32_t index = 0U;
String msg;
IDisplay& display = Display::getInstance();
uint32_t framebuffer[display.getWidth() * display.getHeight()];
uint8_t slotId = SlotList::SLOT_ID_INVALID;
size_t fbLength = display.getWidth() * display.getHeight();
uint32_t* framebuffer = new(std::nothrow) uint32_t[fbLength];

DisplayMgr::getInstance().getFBCopy(framebuffer, UTIL_ARRAY_NUM(framebuffer), &slotId);
if (nullptr == framebuffer)
{
m_isError = true;
}
else
{
uint32_t index = 0U;
String msg;
uint8_t slotId = SlotList::SLOT_ID_INVALID;

msg = slotId;
msg += DELIMITER;
msg += display.getWidth();
msg += DELIMITER;
msg += display.getHeight();
DisplayMgr::getInstance().getFBCopy(framebuffer, fbLength, &slotId);

for(index = 0U; index < UTIL_ARRAY_NUM(framebuffer); ++index)
{
preparePositiveResponse(msg);

msg += slotId;
msg += DELIMITER;
msg += display.getWidth();
msg += DELIMITER;
msg += Util::uint32ToHex(framebuffer[index]);
msg += display.getHeight();

for(index = 0U; index < fbLength; ++index)
{
msg += DELIMITER;
msg += Util::uint32ToHex(framebuffer[index]);
}

delete[] framebuffer;

sendResponse(server, client, msg);
}

sendPositiveResponse(server, client, msg);
}

m_isError = false;
Expand Down
8 changes: 5 additions & 3 deletions src/Web/WsCommand/WsCmdInstall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ void WsCmdInstall::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
}
else
{
msg = DisplayMgr::getInstance().getSlotIdByPluginUID(plugin->getUID());
preparePositiveResponse(msg);

msg += DisplayMgr::getInstance().getSlotIdByPluginUID(plugin->getUID());
msg += DELIMITER;
msg += plugin->getUID();

plugin->enable();

/* Save current installed plugins to persistent memory. */
PluginMgr::getInstance().save();
}

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}
}

m_isError = false;
Expand Down
8 changes: 5 additions & 3 deletions src/Web/WsCommand/WsCmdLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ void WsCmdLog::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)

selectedSink = Logging::getInstance().getSelectedSink();

preparePositiveResponse(msg);

if ((nullptr == selectedSink) ||
(selectedSink->getName() != "Websocket"))
{
msg = "0";
msg += "0";
}
else
{
msg = "1";
msg += "1";
}

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}

m_cnt = 0U;
Expand Down
4 changes: 3 additions & 1 deletion src/Web/WsCommand/WsCmdPlugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void WsCmdPlugins::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
uint8_t cnt = 0U;
const char* pluginName = PluginMgr::getInstance().findFirst();

preparePositiveResponse(msg);

while(nullptr != pluginName)
{
if (0 < cnt)
Expand All @@ -96,7 +98,7 @@ void WsCmdPlugins::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
++cnt;
}

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}

m_isError = false;
Expand Down
6 changes: 4 additions & 2 deletions src/Web/WsCommand/WsCmdSlotDuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ void WsCmdSlotDuration::execute(AsyncWebSocket* server, AsyncWebSocketClient* cl
}
}

msg = DisplayMgr::getInstance().getSlotDuration(m_slotId);
preparePositiveResponse(msg);

sendPositiveResponse(server, client, msg);
msg += DisplayMgr::getInstance().getSlotDuration(m_slotId);

sendResponse(server, client, msg);
}

m_isError = false;
Expand Down
6 changes: 4 additions & 2 deletions src/Web/WsCommand/WsCmdSlots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void WsCmdSlots::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
uint8_t slotId = SlotList::SLOT_ID_INVALID;
uint8_t stickySlot = displayMgr.getStickySlot();

msg = displayMgr.getMaxSlots();
preparePositiveResponse(msg);

msg += displayMgr.getMaxSlots();

/* Provides for every slot:
* - Name of plugin.
Expand Down Expand Up @@ -120,7 +122,7 @@ void WsCmdSlots::execute(AsyncWebSocket* server, AsyncWebSocketClient* client)
msg += duration;
}

sendPositiveResponse(server, client, msg);
sendResponse(server, client, msg);
}

m_isError = false;
Expand Down

0 comments on commit 2ca3b35

Please sign in to comment.