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
/
ThreadServer.cpp
112 lines (86 loc) · 2.32 KB
/
ThreadServer.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MDK_EXPORT_FUNCTIONS 1 /*Export the methods*/
#include "ThreadServer.h"
#include "ModuleEntryPoint.h"
#include <uv.h>
void libuv_callback_general_thread_function(void* arg);
MDKDLLAPI CThreadServer::CThreadServer(int defaultport, bool udp)
{
m_bUDP = udp;
m_iDefaultPort = defaultport;
m_lpThread = NULL;
m_ulExitCode = 0;
m_lpDatabase = NULL;
m_port = -1;
m_ip = NULL;
}
MDKDLLAPI CThreadServer::~CThreadServer()
{
Stop();
}
bool MDKDLLAPI CThreadServer::Start(const char* ip, int port, CDatabase* db, ModuleConfigMap cfg)
{
uv_thread_t* thread = (uv_thread_t*)malloc(sizeof(uv_thread_t));
m_ip = (char*)ip;
m_cfg = cfg;
m_lpDatabase = db;
if (port < 0)
m_port = m_iDefaultPort;
else
m_port = port;
if (uv_thread_create(thread, libuv_callback_general_thread_function, this) != 0)
{
free(thread);
return false;
}
m_lpThread = (mdk_thread)thread;
return true;
}
void MDKDLLAPI CThreadServer::Stop()
{
if (!m_lpThread)
return;
StopServer();
uv_thread_join((uv_thread_t*)m_lpThread);
free(m_lpThread);
m_lpThread = NULL;
}
bool MDKDLLAPI CThreadServer::IsRunning()
{
return m_ulExitCode == ERROR_STILL_ALIVE;
}
unsigned long MDKDLLAPI CThreadServer::GetExitCode()
{
return m_ulExitCode;
}
void MDKDLLAPI CThreadServer::Execute()
{
m_ulExitCode = Initialize();
if (m_ulExitCode != ERROR_NONE)
return;
if (!Bind((const char*)m_ip, m_port, m_bUDP))
{
m_ulExitCode = ERROR_BIND_ERROR;
return;
}
m_ulExitCode = ERROR_STILL_ALIVE;
m_ulExitCode = StartServer();
}
int MDKDLLAPI CThreadServer::Initialize() { return ERROR_NONE; }
void libuv_callback_general_thread_function(void* arg)
{
if (!arg)
return;
((CThreadServer*)arg)->Execute();
}