forked from waldobronchart/AmbientLightServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsgGenericResponse.h
61 lines (50 loc) · 1.2 KB
/
MsgGenericResponse.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
#pragma once
#include "MsgHandlerBase.h"
#include "NetMessageType.h"
#include <cstring>
class MsgGenericResponse : public MsgHandlerBase
{
public:
static NetOutgoingMessage* WriteErrorResponse(string msg)
{
MsgGenericResponse r;
r.m_sevirity = "error";
r.m_msg = msg;
return r.Write();
}
static NetOutgoingMessage* WriteInfoResponse(string msg)
{
MsgGenericResponse r;
r.m_sevirity = "info";
r.m_msg = msg;
return r.Write();
}
static NetOutgoingMessage* WriteWarningResponse(string msg)
{
MsgGenericResponse r;
r.m_sevirity = "warning";
r.m_msg = msg;
return r.Write();
}
virtual NetOutgoingMessage* Write()
{
// Create new empty json object
json_t* root = json_object();
// Values to json types
json_t* jsonResponse = json_string(m_msg.c_str());
// Add to root json object
json_object_set(root, m_sevirity.c_str(), jsonResponse);
// Create outgoing message
NetOutgoingMessage* msg = new NetOutgoingMessage(MSG_GENERIC_REPONSE);
char* dataStr = json_dumps(root, 0);
msg->SetData(dataStr);
delete dataStr; dataStr = 0; delete[] dataStr;
// Cleanup
json_decref(jsonResponse);
json_decref(root);
return msg;
}
private:
string m_sevirity;
string m_msg;
};