Repository navigation
Expand file tree
/
Copy pathPlugins.cpp
More file actions
157 lines (145 loc) · 4.87 KB
/
Copy pathPlugins.cpp
File metadata and controls
157 lines (145 loc) · 4.87 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Plugins.h"
#include <dlfcn.h>
#include <dirent.h>
#include <string>
#include <vector>
#include <map>
#include <sys/stat.h>
#include <sys/types.h>
#include "HttpRequestPacket.h"
#include "HttpResponsePacket.h"
#include "Logger.h"
using namespace std;
// 声明so中的函数原型
typedef bool (*InitFunction)(const char *configFile);
typedef void (*ShutdownFunction)();
typedef bool (*ClientRequestFunction)(HttpRequestPacket *packet);
typedef bool (*ServerResponseFunction)(HttpResponsePacket *packet);
map<string, void *> pluginsList; //插件列表
Logger pluginsLogger;
// 装载所有插件
void LoadPlugins(const char *pluginDir, const char* configFilePath)
{
pluginsLogger.setPrefix("PluginsLoader");
// 遍历pluginDir目录
struct dirent *ent;
DIR *pDir = opendir(pluginDir);
if(pDir == NULL)
{
// 目录不存在,创建一个
mkdir(pluginDir, 0777);
pluginsLogger.info("Plugins dir no found. Created as %s.", pluginDir);
}
else
{
while ((ent = readdir(pDir)) != NULL)
{
if (ent->d_type & DT_DIR)
{
// 是目录,不管
;
}
else
{
// 是文件,尝试加载
string fileName = ent->d_name;
if (EndsWith(fileName, ".so"))
{
// 加载so
pluginsLogger.info("Find plugin %s, try loading...", fileName.c_str());
string soPath = string(pluginDir) + "/" + fileName;
void *soHandle = dlopen(soPath.c_str(), RTLD_LAZY);
if (!soHandle)
{
// 加载失败
pluginsLogger.warn("Fail to load so.");
continue;
}
InitFunction initFunc = (InitFunction)dlsym(soHandle, "Init");
if (dlerror() != NULL)
{
// 查找函数失败
pluginsLogger.warn("Fail to call Init function.");
dlclose(soHandle);
continue;
}
// 调用Init函数
if(!initFunc(configFilePath))
{
// Init未成功
pluginsLogger.warn("Plugin init failed.");
dlclose(soHandle);
continue;
}
pluginsList[fileName] = soHandle;
pluginsLogger.info("Plugin <%s> loaded.", fileName.c_str());
}
}
}
pluginsLogger.info("%d plugins loaded in all.", pluginsList.size());
}
}
// 卸载所有插件
void UnloadPlugins()
{
// 遍历每个插件
for(auto &item : pluginsList)
{
const string &name = item.first;
void* soHandle = item.second;
pluginsLogger.debug("Unloading plugin %s...", name.c_str());
ShutdownFunction shutdownFunc = (ShutdownFunction)dlsym(soHandle, "Shutdown");
if (dlerror() == NULL)
{
// 查找函数成功,调用Shutdown
shutdownFunc();
}
else
pluginsLogger.warn("Fail to call Shutdown function");
// 卸载so
dlclose(soHandle);
pluginsLogger.info("Plugin <%s> unloaded.", name.c_str());
}
}
// Client请求到达,呼叫所有插件
void PluginsCallClientRequest(HttpRequestPacket *packet)
{
// 遍历每个插件
for(auto &item : pluginsList)
{
const string &name = item.first;
void* soHandle = item.second;
pluginsLogger.debug("Client request received. Calling plugin %s...", name.c_str());
ClientRequestFunction clientReqFunc = (ClientRequestFunction)dlsym(soHandle, "ClientRequest");
if (dlerror() == NULL)
{
// 查找函数成功,调用
if(!clientReqFunc(packet))
{
pluginsLogger.debug("Plugin <%s> breaks the event calling to continue.", name.c_str());
break;
}
}
}
}
// Server响应到达,呼叫所有插件
void PluginsCallServerResponse(HttpResponsePacket *packet)
{
// 遍历每个插件
for(auto &item : pluginsList)
{
const string &name = item.first;
void* soHandle = item.second;
pluginsLogger.debug("Server response received. Calling plugin %s...", name.c_str());
ServerResponseFunction serverRespFunc = (ServerResponseFunction)dlsym(soHandle, "ServerResponse");
if (dlerror() == NULL)
{
// 查找函数成功,调用
if(!serverRespFunc(packet))
{
pluginsLogger.debug("Plugin <%s> breaks the event calling to continue.", name.c_str());
break;
}
}
}
}