-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.cpp
More file actions
191 lines (166 loc) · 3.38 KB
/
worker.cpp
File metadata and controls
191 lines (166 loc) · 3.38 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
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
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include "msgdefine.h"
#include "capi.h"
#include "lnode.h"
#include "worker.h"
#define MAX_NODE_COUNT 0x00FFFFFF
Worker::Worker(Scheduler* sched, unsigned int id)
: sched_(sched)
, id_(id)
, thread_(nullptr)
, quit_(false)
, nids_(id + 1, MAX_NODE_COUNT)
, tm_(id)
{
}
Worker::~Worker()
{
}
bool Worker::Create()
{
quit_ = false;
thread_ = new std::thread(std::bind(&Worker::Run, this));
return true;
}
void Worker::Run()
{
const int timeout = 50;
while (!quit_)
{
Message msg;
if (msgs_.Take(msg, timeout))
{
DispatchMsg(msg);
free(msg.content);
}
tm_.OnTick();
}
}
void Worker::Destroy()
{
if (thread_)
{
Message msgquit;
msgquit.type = MSG_TYPE_WORKER_QUIT;
msgquit.size = 0;
msgquit.content = nullptr;
msgquit.to = id_;
SendMsg(msgquit);
thread_->join();
delete thread_;
thread_ = nullptr;
}
for (LnodeMap::iterator it = nodes_.begin();
it != nodes_.end(); ++it)
{
Lnode* node = it->second;
node->Destroy();
delete node;
}
}
void Worker::DispatchMsg(const Message& msg)
{
if (msg.to == id_)
{
ProcessMsg(msg);
}
else
{
LnodeMap::iterator it = nodes_.find(msg.to);
if (it == nodes_.end())
return;
Lnode* node = it->second;
if (node == nullptr)
return;
node->ProcessMsg(msg);
}
}
void Worker::ProcessMsg(const Message& msg)
{
switch (msg.type)
{
case MSG_TYPE_WORKER_QUIT:
quit_ = true;
break;
case MSG_TYPE_WORKER_CREATENODE:
{
CreateNodeST* ptr = (CreateNodeST*)msg.content;
nodes_.insert(std::make_pair(ptr->id, (Lnode*)ptr->node));
}
break;
case MSG_TYPE_WORKER_DESTROYNODE:
{
DestroyNodeST* ptr = (DestroyNodeST*)msg.content;
unsigned int nid = ptr->id;
LnodeMap::iterator it = nodes_.find(nid);
if (it == nodes_.end())
return;
Lnode* node = it->second;
nodes_.erase(it);
node->Destroy();
delete node;
idmutex_.lock();
nids_.Recycle(nid);
idmutex_.unlock();
}
break;
default:
break;
}
}
unsigned int Worker::SpawnNode(
const char* name,
const char* config)
{
unsigned int nid = 0;
idmutex_.lock();
nid = nids_.Assign();
idmutex_.unlock();
if (nid == 0)
return 0;
std::string class_name(name);
std::string file_name(name);
transform(class_name.begin(), class_name.end(),
file_name.begin(), tolower);
file_name += ".lua";
Lnode* node = new Lnode(this, nid);
if (!node->Create(file_name.c_str(), class_name.c_str(), config))
{
delete node;
idmutex_.lock();
nids_.Recycle(nid);
idmutex_.unlock();
return 0;
}
// send create_node message
Message msg_create_node;
msg_create_node.to = id_; // to worker
msg_create_node.type = MSG_TYPE_WORKER_CREATENODE;
msg_create_node.size = sizeof(CreateNodeST);
CreateNodeST* ptr = (CreateNodeST*)malloc(sizeof(CreateNodeST));
ptr->id = nid;
ptr->node = node;
msg_create_node.content = (char*)ptr;
SendMsg(msg_create_node);
return nid;
}
void Worker::CloseNode(unsigned int nid)
{
Message msg_destroy_node;
msg_destroy_node.type = MSG_TYPE_WORKER_DESTROYNODE;
msg_destroy_node.size = sizeof(DestroyNodeST);
DestroyNodeST* ptr = (DestroyNodeST*)malloc(sizeof(DestroyNodeST));
ptr->id = nid;
msg_destroy_node.content = (char*)ptr;
msg_destroy_node.to = id_;
SendMsg(msg_destroy_node);
}
void Worker::SendMsg(const Message& msg)
{
msgs_.Add(msg);
}