This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.cpp
214 lines (173 loc) · 4.18 KB
/
Module.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
This file is part of MasterServer.
MasterServer is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MasterServer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with MasterServer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Module.h"
#include "MSConfig.h"
#include <stdio.h>
#include <string.h>
#include <MDK/Utility.h>
#ifndef _WIN32
#include <dlfcn.h>
#include <pthread.h>
#include <signal.h>
#endif
CModule::CModule()
{
m_szName[0] = '\0';
m_lpServer = NULL;
m_cbMain = NULL;
m_lpDatabase = NULL;
m_lpDLL = NULL;
}
CModule::~CModule()
{
Stop();
// Free Dynamic Library
if (m_lpDLL)
{
#ifdef _WIN32
FreeLibrary(m_lpDLL);
#else
dlclose(m_lpDLL);
#endif
}
}
bool CModule::Load(const char *name, ModuleConfigMap cfg)
{
m_cfg = cfg;
return Load(name);
}
bool CModule::Load(const char *name)
{
std::string dllName = ".";
#ifndef _WIN32
dllName += "/lib";
#else
dllName += "\\";
#endif
dllName += name;
#ifdef _WIN32
dllName += ".dll";
#else
dllName += ".so";
#endif
// Save the name
strncpy(m_szName, name, sizeof(m_szName));
m_szName[sizeof(m_szName) - 1] = '\0';
// Load the Dynamic library
#ifdef _WIN32
m_lpDLL = LoadLibrary(dllName.c_str());
#else
m_lpDLL = dlopen(dllName.c_str(), RTLD_LAZY);
#endif
if (!m_lpDLL)
{
LOG_ERROR("Module", "Cannot find library %s!", m_szName);
return false;
}
// Load the functions
#ifdef _WIN32
m_cbMain = (Module_EntryPoint)GetProcAddress(m_lpDLL, "MDKModule");
#else
m_cbMain = (Module_EntryPoint)dlsym(m_lpDLL, "MDKModule");
#endif
if (!m_cbMain)
{
LOG_ERROR("Module", "Invalid library %s!", m_szName);
return false;
}
m_lpServer = m_cbMain();
return true;
}
bool CModule::IsRunning()
{
if (m_lpServer)
return m_lpServer->IsRunning();
return false;
}
bool CModule::Start(CDatabase* db)
{
int port = -1;
const char *ip = NULL;
// Retrive bind ip and bind port
{
ModuleConfigMap::iterator it = m_cfg.find("Port");
if (it != m_cfg.end())
port = atoi(it->second.c_str());
it = m_cfg.find("BindIP");
if (it == m_cfg.end())
ip = (char*)CConfig::GetDefaultIP();
else
ip = (char*)it->second.c_str();
}
// Connect to MySQL server
if (CConfig::IsDatabaseEnabled())
{
bool database_disabled = false;
ModuleConfigMap::iterator it = m_cfg.find("DisableMySQL");
m_lpDatabase = db;
if (it != m_cfg.end())
{
if (it->second.compare("1") == 0)
{
database_disabled = true;
}
}
if (database_disabled == false && m_lpDatabase == NULL)
{
m_lpDatabase = new CDatabase();
if (CConfig::GetDatabaseType() == DATABASE_TYPE_MARIADB)
{
if (CConfig::GetDatabaseSocket()[0] != '\0')
{
if (!m_lpDatabase->Connect(CConfig::GetDatabaseType(), CConfig::GetDatabaseSocket(), -1, CConfig::GetDatabaseUsername(), CConfig::GetDatabaseName(), CConfig::GetDatabasePassword()))
return false;
}
else
{
if (!m_lpDatabase->Connect(CConfig::GetDatabaseType(), CConfig::GetDatabaseHost(), CConfig::GetDatabasePort(), CConfig::GetDatabaseUsername(), CConfig::GetDatabaseName(), CConfig::GetDatabasePassword()))
return false;
}
}
}
}
return m_lpServer->Start(ip, port, m_lpDatabase, m_cfg);
}
const char *CModule::GetDatabaseStatus()
{
if (!m_lpDatabase)
return "Disabled";
return m_lpDatabase->IsConnected() ? "Connected " : "Disconnected";
}
void CModule::Stop()
{
if (m_lpServer)
{
m_lpServer->Stop();
delete m_lpServer;
m_lpServer = NULL;
}
// Close MySQL connection
if (m_lpDatabase && m_lpDatabase->GetDatabaseType() == DATABASE_TYPE_MARIADB)
{
m_lpDatabase->Disconnect();
delete m_lpDatabase;
}
m_lpDatabase = NULL;
}
int CModule::GetExitCode()
{
if (m_lpServer == NULL)
return ERROR_NONE;
return m_lpServer->GetExitCode();
}