-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMsgGenericRequest.h
68 lines (57 loc) · 1.45 KB
/
MsgGenericRequest.h
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
#pragma once
#include "MsgHandlerBase.h"
#include "MsgFrameBuffer.h"
#include "MsgPreferences.h"
#include "Logging.h"
class MsgGenericRequest : public MsgHandlerBase
{
public:
virtual bool Read(Connection* sender, NetIncomingMessage& msg)
{
if (msg.BodyLength() == 0)
return false;
// Read the body as json
json_error_t error;
json_t* root = json_loads(msg.Body(), 0, &error);
if (!root)
{
LOG_ERROR("MsgGenericRequest.Read: error on line " << error.line << ":" << error.text);
json_decref(root);
return false;
}
// m_message
json_t* request = json_object_get(root, "request");
if (!json_is_string(request))
{
LOG_ERROR("MsgGenericRequest.Read: 'request' is not a string");
json_decref(root);
return false;
}
m_requestStr = json_string_value(request);
// Handle it yo!
HandleRequest(sender);
// Cleanup
json_decref(root);
return true;
}
private:
void HandleRequest(Connection* sender)
{
if (m_requestStr == "getframebuffer")
{
LOG_INFO("MsgGenericRequest: handling 'getframebuffer' request");
MsgFrameBuffer response;
sender->Send(response.Write());
return;
}
if (m_requestStr == "getprefs")
{
LOG_INFO("MsgGenericRequest: handling 'getprefs' request");
MsgPreferences response;
sender->Send(response.Write());
return;
}
LOG_WARN("MsgGenericRequest: unhandled request '" << m_requestStr.c_str() << "'");
}
string m_requestStr;
};