Repository navigation
Expand file tree
/
Copy pathentity_mailbox.cpp
More file actions
307 lines (257 loc) · 9.33 KB
/
Copy pathentity_mailbox.cpp
File metadata and controls
307 lines (257 loc) · 9.33 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine 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.
KBEngine 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 KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "entity_mailbox.h"
#include "scriptdef_module.h"
#include "helper/debug_helper.h"
#include "network/channel.h"
#include "pyscript/pickler.h"
#include "pyscript/py_gc.h"
#include "entitydef/method.h"
#include "remote_entity_method.h"
#include "entitydef/entitydef.h"
namespace KBEngine
{
// 获得某个entity的函数地址
EntityMailbox::GetEntityFunc EntityMailbox::__getEntityFunc;
EntityMailbox::FindChannelFunc EntityMailbox::__findChannelFunc;
EntityMailbox::MailboxCallHookFunc* EntityMailbox::__hookCallFuncPtr = NULL;
std::vector<EntityMailbox*> EntityMailbox::mailboxs;
SCRIPT_METHOD_DECLARE_BEGIN(EntityMailbox)
SCRIPT_METHOD_DECLARE_END()
SCRIPT_MEMBER_DECLARE_BEGIN(EntityMailbox)
SCRIPT_MEMBER_DECLARE_END()
SCRIPT_GETSET_DECLARE_BEGIN(EntityMailbox)
SCRIPT_GETSET_DECLARE_END()
SCRIPT_INIT(EntityMailbox, 0, 0, 0, 0, 0)
//-------------------------------------------------------------------------------------
EntityMailbox::EntityMailbox(ScriptDefModule* pScriptModule,
const Network::Address* pAddr,
COMPONENT_ID componentID,
ENTITY_ID eid, ENTITY_MAILBOX_TYPE type):
EntityMailboxAbstract(getScriptType(),
pAddr,
componentID,
eid, pScriptModule->getUType(),
type),
scriptModuleName_(pScriptModule->getName()),
pScriptModule_(pScriptModule),
atIdx_(MAILBOXS::size_type(-1))
{
atIdx_ = EntityMailbox::mailboxs.size();
EntityMailbox::mailboxs.push_back(this);
script::PyGC::incTracing("EntityMailbox");
}
//-------------------------------------------------------------------------------------
EntityMailbox::~EntityMailbox()
{
//char s[1024];
//c_str(s, 1024);
//DEBUG_MSG(fmt::format("EntityMailbox::~EntityMailbox(): {}.\n", s));
KBE_ASSERT(atIdx_ < EntityMailbox::mailboxs.size());
KBE_ASSERT(EntityMailbox::mailboxs[ atIdx_ ] == this);
// 如果有2个或以上的Mailbox则将最后一个Mailbox移至删除的这个Mailbox所在位置
EntityMailbox* pBack = EntityMailbox::mailboxs.back();
pBack->_setATIdx(atIdx_);
EntityMailbox::mailboxs[atIdx_] = pBack;
atIdx_ = MAILBOXS::size_type(-1);
EntityMailbox::mailboxs.pop_back();
script::PyGC::decTracing("EntityMailbox");
}
//-------------------------------------------------------------------------------------
RemoteEntityMethod* EntityMailbox::createRemoteMethod(MethodDescription* pMethodDescription)
{
if(__hookCallFuncPtr != NULL)
{
return (*__hookCallFuncPtr)(pMethodDescription, this);
}
return new RemoteEntityMethod(pMethodDescription, this);
}
//-------------------------------------------------------------------------------------
PyObject* EntityMailbox::onScriptGetAttribute(PyObject* attr)
{
wchar_t* PyUnicode_AsWideCharStringRet0 = PyUnicode_AsWideCharString(attr, NULL);
char* ccattr = strutil::wchar2char(PyUnicode_AsWideCharStringRet0);
PyMem_Free(PyUnicode_AsWideCharStringRet0);
MethodDescription* pMethodDescription = NULL;
switch(type_)
{
case MAILBOX_TYPE_CELL:
pMethodDescription = pScriptModule_->findCellMethodDescription(ccattr);
break;
case MAILBOX_TYPE_BASE:
pMethodDescription = pScriptModule_->findBaseMethodDescription(ccattr);
break;
case MAILBOX_TYPE_CLIENT:
pMethodDescription = pScriptModule_->findClientMethodDescription(ccattr);
break;
case MAILBOX_TYPE_CELL_VIA_BASE:
pMethodDescription = pScriptModule_->findCellMethodDescription(ccattr);
break;
case MAILBOX_TYPE_BASE_VIA_CELL:
pMethodDescription = pScriptModule_->findBaseMethodDescription(ccattr);
break;
case MAILBOX_TYPE_CLIENT_VIA_CELL:
pMethodDescription = pScriptModule_->findClientMethodDescription(ccattr);
break;
case MAILBOX_TYPE_CLIENT_VIA_BASE:
pMethodDescription = pScriptModule_->findClientMethodDescription(ccattr);
break;
default:
break;
};
if(pMethodDescription != NULL)
{
free(ccattr);
if(g_componentType == CLIENT_TYPE || g_componentType == BOTS_TYPE)
{
if(!pMethodDescription->isExposed())
return ScriptObject::onScriptGetAttribute(attr);
}
return createRemoteMethod(pMethodDescription);
}
// 首先要求名称不能为自己 比如:自身是一个cell, 不能使用cell.cell
if(strcmp(ccattr, ENTITY_MAILBOX_TYPE_TO_NAME_TABLE[type_]) != 0)
{
int8 mbtype = -1;
if(strcmp(ccattr, "cell") == 0)
{
if(type_ == MAILBOX_TYPE_BASE_VIA_CELL)
mbtype = MAILBOX_TYPE_CELL;
else
mbtype = MAILBOX_TYPE_CELL_VIA_BASE;
}
else if(strcmp(ccattr, "base") == 0)
{
if(type_ == MAILBOX_TYPE_CELL_VIA_BASE)
mbtype = MAILBOX_TYPE_BASE;
else
mbtype = MAILBOX_TYPE_BASE_VIA_CELL;
}
else if(strcmp(ccattr, "client") == 0)
{
if(type_ == MAILBOX_TYPE_BASE)
mbtype = MAILBOX_TYPE_CLIENT_VIA_BASE;
else if(type_ == MAILBOX_TYPE_CELL)
mbtype = MAILBOX_TYPE_CLIENT_VIA_CELL;
}
if(mbtype != -1)
{
free(ccattr);
if(g_componentType != CLIENT_TYPE && g_componentType != BOTS_TYPE)
{
return new EntityMailbox(pScriptModule_, &addr_, componentID_,
id_, (ENTITY_MAILBOX_TYPE)mbtype);
}
else
{
Py_INCREF(this);
return this;
}
}
}
free(ccattr);
return ScriptObject::onScriptGetAttribute(attr);
}
//-------------------------------------------------------------------------------------
PyObject* EntityMailbox::tp_repr()
{
char s[1024];
c_str(s, 1024);
return PyUnicode_FromString(s);
}
//-------------------------------------------------------------------------------------
void EntityMailbox::c_str(char* s, size_t size)
{
const char * mailboxName =
(type_ == MAILBOX_TYPE_CELL) ? "Cell" :
(type_ == MAILBOX_TYPE_BASE) ? "Base" :
(type_ == MAILBOX_TYPE_CLIENT) ? "Client" :
(type_ == MAILBOX_TYPE_BASE_VIA_CELL) ? "BaseViaCell" :
(type_ == MAILBOX_TYPE_CLIENT_VIA_CELL) ? "ClientViaCell" :
(type_ == MAILBOX_TYPE_CELL_VIA_BASE) ? "CellViaBase" :
(type_ == MAILBOX_TYPE_CLIENT_VIA_BASE) ? "ClientViaBase" : "???";
Network::Channel* pChannel = getChannel();
kbe_snprintf(s, size, "%s id:%d, utype:%u, component=%s[%" PRIu64 "], addr: %s.",
mailboxName, id_, utype_,
COMPONENT_NAME_EX(ENTITY_MAILBOX_COMPONENT_TYPE_MAPPING[type_]),
componentID_, (pChannel && pChannel->pEndPoint()) ? pChannel->addr().c_str() : "None");
}
//-------------------------------------------------------------------------------------
PyObject* EntityMailbox::tp_str()
{
return tp_repr();
}
//-------------------------------------------------------------------------------------
PyObject* EntityMailbox::tryGetEntity(COMPONENT_ID componentID, ENTITY_ID entityID)
{
return __getEntityFunc(componentID, entityID);
}
//-------------------------------------------------------------------------------------
PyObject* EntityMailbox::__unpickle__(PyObject* self, PyObject* args)
{
ENTITY_ID eid = 0;
COMPONENT_ID componentID = 0;
ENTITY_SCRIPT_UID utype = 0;
int16 type = 0;
Py_ssize_t size = PyTuple_Size(args);
if(size != 4)
{
ERROR_MSG("EntityMailbox::__unpickle__: args is error! size != 4.\n");
S_Return;
}
if(!PyArg_ParseTuple(args, "iKHh", &eid, &componentID, &utype, &type))
{
ERROR_MSG("EntityMailbox::__unpickle__: args is error!\n");
S_Return;
}
ScriptDefModule* sm = EntityDef::findScriptModule(utype);
if(sm == NULL)
{
ERROR_MSG(fmt::format("EntityMailbox::__unpickle__: not found utype {}!\n", utype));
S_Return;
}
// COMPONENT_TYPE componentType = ENTITY_MAILBOX_COMPONENT_TYPE_MAPPING[(ENTITY_MAILBOX_TYPE)type];
PyObject* entity = tryGetEntity(componentID, eid);
if(entity != NULL)
{
Py_INCREF(entity);
return entity;
}
return new EntityMailbox(sm, NULL, componentID, eid, (ENTITY_MAILBOX_TYPE)type);
}
//-------------------------------------------------------------------------------------
void EntityMailbox::onInstallScript(PyObject* mod)
{
static PyMethodDef __unpickle__Method =
{"Mailbox", (PyCFunction)&EntityMailbox::__unpickle__, METH_VARARGS, 0};
PyObject* pyFunc = PyCFunction_New(&__unpickle__Method, NULL);
script::Pickler::registerUnpickleFunc(pyFunc, "Mailbox");
Py_DECREF(pyFunc);
}
//-------------------------------------------------------------------------------------
Network::Channel* EntityMailbox::getChannel(void)
{
if(__findChannelFunc == NULL)
return NULL;
return __findChannelFunc(*this);
}
//-------------------------------------------------------------------------------------
void EntityMailbox::reload()
{
pScriptModule_ = EntityDef::findScriptModule(scriptModuleName_.c_str());
}
//-------------------------------------------------------------------------------------
}