正在显示
1 个修改的文件
包含
81 行增加
和
0 行删除
src/server/dmpservicenativeloader.cpp
0 → 100644
1 | +/* | ||
2 | + * @Author: your name | ||
3 | + * @Date: 2021-04-26 11:49:54 | ||
4 | + * @LastEditTime: 2021-04-26 11:53:35 | ||
5 | + * @LastEditors: Please set LastEditors | ||
6 | + * @Description: In User Settings Edit | ||
7 | + * @FilePath: /dmapserver/src/server/dmpservicenativeloader.cpp | ||
8 | + */ | ||
9 | +/************************************************************************** | ||
10 | +* file: dmpservicenativeloader.cpp | ||
11 | + | ||
12 | +* Author: wanzhongping | ||
13 | +* Date: 2020-12-24 16:01:21 | ||
14 | +* Email: zhongpingw@chinadci.com | ||
15 | +* copyright: 广州城市信息研究所有限公司 | ||
16 | +***************************************************************************/ | ||
17 | +#include "dmpservicenativeloader.h" | ||
18 | +#include "dmpservicemodule.h" | ||
19 | +#include <boost/dll/import.hpp> | ||
20 | +#include <dlfcn.h> | ||
21 | +#include "dmpserverutils.h" | ||
22 | + | ||
23 | +std::shared_ptr<DmpServiceModule> DmpServiceNativeLoader::LoadNativeModule(const std::string &location) | ||
24 | +{ | ||
25 | + std::shared_ptr<DmpServiceModule> sp_module = FindModule(location); | ||
26 | + if (sp_module) | ||
27 | + { | ||
28 | + return sp_module; | ||
29 | + } | ||
30 | + | ||
31 | + boost::dll::fs::error_code error; | ||
32 | + std::shared_ptr<boost::dll::shared_library> lib = std::make_shared<boost::dll::shared_library>(location, error); | ||
33 | + if (error) | ||
34 | + { | ||
35 | + return nullptr; | ||
36 | + } | ||
37 | + if (lib->has("service_module")) | ||
38 | + { | ||
39 | + sp_module = boost::move(lib)->get_alias<std::shared_ptr<DmpServiceModule>()>("service_module")(); | ||
40 | + if (sp_module) | ||
41 | + { | ||
42 | + //保存lib的引用,否则sp_module会被释放掉 | ||
43 | + sp_module->set_lib(boost::move(lib)); | ||
44 | + modules_[location] = sp_module; | ||
45 | + return sp_module; | ||
46 | + } | ||
47 | + } | ||
48 | + return nullptr; | ||
49 | +} | ||
50 | + | ||
51 | +void DmpServiceNativeLoader::LoadModules(const boost::filesystem::path &module_path, | ||
52 | + DmpServiceRegistry ®istrar) | ||
53 | +{ | ||
54 | + std::vector<boost::filesystem::path> locations; | ||
55 | + DmpServerUtils::GetLibraryPaths(module_path, locations); | ||
56 | + | ||
57 | + std::vector<boost::filesystem::path>::iterator iter; | ||
58 | + for (iter = locations.begin(); iter != locations.end(); iter++) | ||
59 | + { | ||
60 | + std::shared_ptr<DmpServiceModule> sp_module = LoadNativeModule(iter->string()); | ||
61 | + if (sp_module) | ||
62 | + { | ||
63 | + sp_module->RegisterSelf(registrar); | ||
64 | + } | ||
65 | + } | ||
66 | +} | ||
67 | + | ||
68 | +std::shared_ptr<DmpServiceModule> DmpServiceNativeLoader::FindModule(const std::string &location) | ||
69 | +{ | ||
70 | + ModuleMap::iterator item = modules_.find(location); | ||
71 | + if (item != modules_.end()) | ||
72 | + { | ||
73 | + return item->second; | ||
74 | + } | ||
75 | + return nullptr; | ||
76 | +} | ||
77 | + | ||
78 | +void DmpServiceNativeLoader::UnloadModules() | ||
79 | +{ | ||
80 | + modules_.clear(); | ||
81 | +} |
请
注册
或
登录
后发表评论