-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathRpcEngine.cpp
More file actions
133 lines (109 loc) · 3.01 KB
/
RpcEngine.cpp
File metadata and controls
133 lines (109 loc) · 3.01 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma once
#include "compat.h"
#include "RpcEngine.h"
#include "IRpcFunction.h"
#include "Macros.h"
#include "Types.h"
#include "IServer.h"
#include "IMessage.h"
#include "Macros.h"
#include "Log.h"
#include <list>
#include <Tracy.hpp>
const char *frameName = "RPCEngine";
//
// Entrant worker, weee
//
acre::Result CRpcEngine::exProcessItem(ACRE_RPCDATA *data) {
tracy::SetThreadName("RPCEngine");
FrameMarkStart(frameName);
if (data->function != nullptr) {
data->function->call(data->server, data->message);
}
delete data->message;
free(data);
FrameMarkEnd(frameName);
return acre::Result::ok;
}
//
// Proc functions
//
acre::Result CRpcEngine::addProcedure(IRpcFunction *const cmd) {
ZoneScoped;
LOCK(this);
this->m_FunctionList.insert(std::pair<std::string, IRpcFunction *>(std::string(cmd->getName()), cmd));
UNLOCK(this);
return acre::Result::ok;
}
acre::Result CRpcEngine::removeProcedure(IRpcFunction *const cmd) {
ZoneScoped;
LOCK(this);
this->m_FunctionList.erase(cmd->getName());
UNLOCK(this);
return acre::Result::ok;
}
acre::Result CRpcEngine::removeProcedure(char *const cmd) {
ZoneScoped;
LOCK(this);
this->m_FunctionList.erase(cmd);
UNLOCK(this);
return acre::Result::ok;
}
IRpcFunction *CRpcEngine::findProcedure(char *const cmd) {
ZoneScoped;
if (this->getShuttingDown()) {
return nullptr;
}
auto it = this->m_FunctionList.find(std::string((char *)cmd));
if (it != this->m_FunctionList.end()) {
return (IRpcFunction *)it->second;
}
return nullptr;
}
acre::Result CRpcEngine::runProcedure(IServer *const serverInstance, IMessage *msg) {
ZoneScoped;
return this->runProcedure(serverInstance, msg, TRUE);
}
acre::Result CRpcEngine::runProcedure(IServer *const serverInstance, IMessage *msg, const bool entrant) {
ZoneScoped;
if (msg == nullptr) {
return acre::Result::error;
} else if (msg->getProcedureName() == nullptr) {
delete msg;
return acre::Result::error;
}
IRpcFunction *const ptr = this->findProcedure((char *) msg->getProcedureName());
if (ptr != nullptr) {
if (!entrant) {
LOCK(this);
ptr->call(serverInstance, msg);
delete msg;
UNLOCK(this);
} else {
if (!this->getRunning()) {
this->startWorker();
}
ACRE_RPCDATA *const data = (ACRE_RPCDATA *)malloc(sizeof(ACRE_RPCDATA));
data->function = ptr;
data->server = serverInstance;
data->message = msg;
LOCK(this);
this->m_processQueue.push(data);
UNLOCK(this);
}
} else {
// No procedure, delete the message to stop memory leak
delete msg;
}
return acre::Result::ok;
}
//
// Constructor // Destructor
//
CRpcEngine::CRpcEngine() {
// start our entrant worker, weee
this->startWorker();
}
CRpcEngine::~CRpcEngine() {
this->stopWorker();
}