-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestsServer.cpp
More file actions
78 lines (70 loc) · 2.16 KB
/
RequestsServer.cpp
File metadata and controls
78 lines (70 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "RequestsServer.h"
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "Utils.h"
namespace SA
{
RequestsServer::RequestsServer() = default;
RequestsServer::~RequestsServer()
{
if (m_server)
{
m_server->end();
}
}
void RequestsServer::Init()
{
m_server = std::make_unique<AsyncWebServer>(80);
m_server->on(
"/api",
HTTP_POST,
[](AsyncWebServerRequest *request){},
nullptr,
[this](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total)
{
if (index == 0)
{
request->_tempObject = new String();
}
String* buffer = static_cast<String*>(request->_tempObject);
*buffer += String((char*)data).substring(0, len);
if (index + len == total)
{
{
const std::string_view bufferView(buffer->c_str(), buffer->length());
SCOPE_LOCK(m_listenersMutex);
for (const auto& listener : m_listeners)
{
const int listenerNameLength = listener.m_name.length();
if (bufferView.starts_with(listener.m_name.c_str()) && bufferView.size() > listenerNameLength && bufferView[listenerNameLength] == ':')
{
listener.m_callback(buffer->c_str() + listenerNameLength + 1);
}
}
}
request->send(200, "text/plain", "OK");
request->_tempObject = nullptr;
delete buffer;
}
}
);
m_server->begin();
}
[[nodiscard]] RequestsServer::ListenerHandle RequestsServer::RegisterListener(String name, std::function<void(const char*)> callback)
{
SCOPE_LOCK(m_listenersMutex);
m_listeners.push_back({std::move(name), std::move(callback)});
return m_listeners.back().m_handle;
}
bool RequestsServer::UnregisterListener(ListenerHandle handle)
{
SCOPE_LOCK(m_listenersMutex);
auto it = std::find_if(m_listeners.begin(), m_listeners.end(), [&](const Listener& listener) { return listener.m_handle == handle; });
if (it != m_listeners.end())
{
m_listeners.erase(it);
return true;
}
return false;
}
}