forked from kbengine/kbengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.cpp
More file actions
783 lines (656 loc) · 22 KB
/
Copy pathproxy.cpp
File metadata and controls
783 lines (656 loc) · 22 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
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 "baseapp.h"
#include "proxy.h"
#include "proxy_forwarder.h"
#include "profile.h"
#include "data_download.h"
#include "client_lib/client_interface.h"
#include "network/fixed_messages.h"
#include "network/channel.h"
#include "../../server/cellapp/cellapp_interface.h"
#include "../../server/dbmgr/dbmgr_interface.h"
#ifndef CODE_INLINE
#include "proxy.inl"
#endif
namespace KBEngine{
SCRIPT_METHOD_DECLARE_BEGIN(Proxy)
SCRIPT_METHOD_DECLARE("giveClientTo", pyGiveClientTo, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("getClientType", pyGetClientType, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("getClientDatas", pyGetClientDatas, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("streamStringToClient", pyStreamStringToClient, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE("streamFileToClient", pyStreamFileToClient, METH_VARARGS, 0)
SCRIPT_METHOD_DECLARE_END()
SCRIPT_MEMBER_DECLARE_BEGIN(Proxy)
SCRIPT_MEMBER_DECLARE_END()
SCRIPT_GETSET_DECLARE_BEGIN(Proxy)
SCRIPT_GET_DECLARE("roundTripTime", pyGetRoundTripTime, 0, 0)
SCRIPT_GET_DECLARE("timeSinceHeardFromClient", pyGetTimeSinceHeardFromClient, 0, 0)
SCRIPT_GET_DECLARE("clientAddr", pyClientAddr, 0, 0)
SCRIPT_GET_DECLARE("hasClient", pyHasClient, 0, 0)
SCRIPT_GET_DECLARE("entitiesEnabled", pyGetEntitiesEnabled, 0, 0)
SCRIPT_GETSET_DECLARE_END()
BASE_SCRIPT_INIT(Proxy, 0, 0, 0, 0, 0)
//-------------------------------------------------------------------------------------
Proxy::Proxy(ENTITY_ID id, const ScriptDefModule* pScriptModule):
Base(id, pScriptModule, getScriptType(), true),
rndUUID_(KBEngine::genUUID64()),
addr_(Network::Address::NONE),
dataDownloads_(),
entitiesEnabled_(false),
bandwidthPerSecond_(0),
encryptionKey(),
pProxyForwarder_(NULL),
clientComponentType_(UNKNOWN_CLIENT_COMPONENT_TYPE),
clientDatas_()
{
Baseapp::getSingleton().incProxicesCount();
pProxyForwarder_ = new ProxyForwarder(this);
}
//-------------------------------------------------------------------------------------
Proxy::~Proxy()
{
Baseapp::getSingleton().decProxicesCount();
kick();
SAFE_RELEASE(pProxyForwarder_);
}
//-------------------------------------------------------------------------------------
void Proxy::kick()
{
// 如果被销毁频道仍然存活则将其关闭
Network::Channel* pChannel = Baseapp::getSingleton().networkInterface().findChannel(addr_);
if(pChannel && !pChannel->isDestroyed())
{
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(ClientInterface::onKicked);
ClientInterface::onKickedArgs1::staticAddToBundle(*pBundle, SERVER_ERR_PROXY_DESTROYED);
//pBundle->send(Baseapp::getSingleton().networkInterface(), pChannel);
this->sendToClient(ClientInterface::onKicked, pBundle);
this->sendToClient();
pChannel->condemn();
}
}
//-------------------------------------------------------------------------------------
void Proxy::initClientBasePropertys()
{
if(clientMailbox() == NULL)
return;
MemoryStream* s1 = MemoryStream::createPoolObject();
addClientDataToStream(s1);
if(s1->wpos() > 0)
{
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(ClientInterface::onUpdatePropertys);
(*pBundle) << this->id();
(*pBundle).append(*s1);
sendToClient(ClientInterface::onUpdatePropertys, pBundle);
//clientMailbox()->postMail((*pBundle));
}
MemoryStream::reclaimPoolObject(s1);
}
//-------------------------------------------------------------------------------------
void Proxy::initClientCellPropertys()
{
if(clientMailbox() == NULL)
return;
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(ClientInterface::onUpdatePropertys);
(*pBundle) << this->id();
ENTITY_PROPERTY_UID spaceuid = ENTITY_BASE_PROPERTY_UTYPE_SPACEID;
Network::FixedMessages::MSGInfo* msgInfo =
Network::FixedMessages::getSingleton().isFixed("Property::spaceID");
if(msgInfo != NULL)
{
spaceuid = msgInfo->msgid;
}
if(pScriptModule()->usePropertyDescrAlias())
{
uint8 aliasID = ENTITY_BASE_PROPERTY_ALIASID_SPACEID;
(*pBundle) << aliasID << this->spaceID();
}
else
{
(*pBundle) << spaceuid << this->spaceID();
}
MemoryStream* s = MemoryStream::createPoolObject();
// celldata获取客户端感兴趣的数据初始化客户端 如:ALL_CLIENTS
addCellDataToStream(ED_FLAG_ALL_CLIENTS|ED_FLAG_CELL_PUBLIC_AND_OWN|ED_FLAG_OWN_CLIENT, s, true);
(*pBundle).append(*s);
MemoryStream::reclaimPoolObject(s);
//clientMailbox()->postMail((*pBundle));
sendToClient(ClientInterface::onUpdatePropertys, pBundle);
}
//-------------------------------------------------------------------------------------
void Proxy::onEntitiesEnabled(void)
{
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
entitiesEnabled_ = true;
SCRIPT_OBJECT_CALL_ARGS0(this, const_cast<char*>("onEntitiesEnabled"));
}
//-------------------------------------------------------------------------------------
int32 Proxy::onLogOnAttempt(const char* addr, uint32 port, const char* password)
{
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
PyObject* pyResult = PyObject_CallMethod(this,
const_cast<char*>("onLogOnAttempt"), const_cast<char*>("sks"),
addr,
port,
password
);
int32 ret = LOG_ON_REJECT;
if(pyResult != NULL)
{
ret = PyLong_AsLong(pyResult);
SCRIPT_ERROR_CHECK();
Py_DECREF(pyResult);
}
else
SCRIPT_ERROR_CHECK();
return ret;
}
//-------------------------------------------------------------------------------------
void Proxy::onClientDeath(void)
{
if(clientMailbox() == NULL)
{
ERROR_MSG(fmt::format("{}::onClientDeath: {}, channel is null!\n",
this->scriptName(), this->id()));
return;
}
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
DEBUG_MSG(fmt::format("{}::onClientDeath: {}.\n",
this->scriptName(), this->id()));
Py_DECREF(clientMailbox());
clientMailbox(NULL);
addr(Network::Address::NONE);
entitiesEnabled_ = false;
SCRIPT_OBJECT_CALL_ARGS0(this, const_cast<char*>("onClientDeath"));
}
//-------------------------------------------------------------------------------------
void Proxy::onClientGetCell(Network::Channel* pChannel, COMPONENT_ID componentID)
{
// 回调给脚本,获得了cell
if(cellMailbox_ == NULL)
cellMailbox_ = new EntityMailbox(pScriptModule_, NULL, componentID, id_, MAILBOX_TYPE_CELL);
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
SCRIPT_OBJECT_CALL_ARGS0(this, const_cast<char*>("onClientGetCell"));
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGetClientType()
{
return PyLong_FromLong((long)getClientType());
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGetClientDatas()
{
const std::string& datas = this->getClientDatas();
return PyBytes_FromStringAndSize(datas.data(), datas.size());
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGiveClientTo(PyObject* pyOterProxy)
{
if(this->isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
// 如果为None 则设置为NULL
Proxy* oterProxy = NULL;
if(pyOterProxy != Py_None)
oterProxy = static_cast<Proxy*>(pyOterProxy);
giveClientTo(oterProxy);
S_Return;
}
//-------------------------------------------------------------------------------------
void Proxy::onGiveClientToFailure()
{
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
SCRIPT_OBJECT_CALL_ARGS0(this, const_cast<char*>("onGiveClientToFailure"));
}
//-------------------------------------------------------------------------------------
void Proxy::giveClientTo(Proxy* proxy)
{
if(isDestroyed())
{
char err[255];
kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: %d is destroyed.",
scriptName(), id());
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
onGiveClientToFailure();
return;
}
if(clientMailbox_ == NULL || clientMailbox_->getChannel() == NULL)
{
char err[255];
kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: no has client.", scriptName());
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
onGiveClientToFailure();
return;
}
Network::Channel* lpChannel = clientMailbox_->getChannel();
if(proxy)
{
if(proxy->isDestroyed())
{
char err[255];
kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is destroyed.",
scriptName(), proxy->id());
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
onGiveClientToFailure();
return;
}
if(proxy->id() == this->id())
{
char err[255];
kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is self.",
scriptName(), proxy->id());
PyErr_SetString(PyExc_TypeError, err);
PyErr_PrintEx(0);
onGiveClientToFailure();
return;
}
EntityMailbox* mb = proxy->clientMailbox();
if(mb != NULL)
{
ERROR_MSG(fmt::format("Proxy::giveClientTo: {}[{}] give client to {}[{}], {} has clientMailbox.\n",
scriptName(),
id(),
proxy->scriptName(),
proxy->id(),
proxy->scriptName()));
onGiveClientToFailure();
return;
}
if(cellMailbox())
{
// 当前这个entity如果有cell,说明已经绑定了witness, 那么既然我们将控制权
// 交换给了另一个entity, 这个entity需要解绑定witness。
// 通知cell丢失witness
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(CellappInterface::onLoseWitness);
(*pBundle) << this->id();
sendToCellapp(pBundle);
}
// 既然客户端失去对其的控制, 那么通知client销毁这个entity
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(ClientInterface::onEntityDestroyed);
(*pBundle) << this->id();
sendToClient(ClientInterface::onEntityDestroyed, pBundle);
// 将控制权交换
entitiesEnabled_ = false;
clientMailbox()->addr(Network::Address::NONE);
Py_DECREF(clientMailbox());
proxy->setClientType(this->getClientType());
proxy->setClientDatas(this->getClientDatas());
this->setClientType(UNKNOWN_CLIENT_COMPONENT_TYPE);
this->setClientDatas("");
clientMailbox(NULL);
proxy->onGiveClientTo(lpChannel);
addr(Network::Address::NONE);
}
}
//-------------------------------------------------------------------------------------
void Proxy::onGiveClientTo(Network::Channel* lpChannel)
{
clientMailbox(new EntityMailbox(this->pScriptModule_,
&lpChannel->addr(), 0, id_, MAILBOX_TYPE_CLIENT));
addr(lpChannel->addr());
Baseapp::getSingleton().createClientProxies(this);
// 如果有cell, 需要通知其获得witness, 因为这个客户端刚刚绑定到这个proxy
// 此时这个entity即使有cell正常情况必须是没有witness的。
onGetWitness();
}
//-------------------------------------------------------------------------------------
void Proxy::onGetWitness()
{
if(cellMailbox())
{
// 通知cell获得客户端
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(CellappInterface::onGetWitnessFromBase);
(*pBundle) << this->id();
sendToCellapp(pBundle);
}
}
//-------------------------------------------------------------------------------------
double Proxy::getRoundTripTime() const
{
if(clientMailbox() == NULL || clientMailbox()->getChannel() == NULL ||
clientMailbox()->getChannel()->pEndPoint() == NULL)
return 0.0;
return double(clientMailbox()->getChannel()->pEndPoint()->getRTT()) / 1000000.0;
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGetRoundTripTime()
{
if(isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
return PyFloat_FromDouble(this->getRoundTripTime());
}
//-------------------------------------------------------------------------------------
double Proxy::getTimeSinceHeardFromClient() const
{
if(clientMailbox() == NULL || clientMailbox()->getChannel() == NULL ||
clientMailbox()->getChannel()->pEndPoint() == NULL)
return DBL_MAX;
return double(timestamp() - clientMailbox()->getChannel()->lastReceivedTime()) / stampsPerSecondD();
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGetTimeSinceHeardFromClient()
{
if(isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
return PyFloat_FromDouble(this->getTimeSinceHeardFromClient());
}
//-------------------------------------------------------------------------------------
bool Proxy::hasClient() const
{
if(clientMailbox() == NULL || clientMailbox()->getChannel() == NULL ||
clientMailbox()->getChannel()->pEndPoint() == NULL)
return false;
return true;
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyHasClient()
{
if(isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
if(this->hasClient())
{
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyClientAddr()
{
if(isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
PyObject* pyobj = PyTuple_New(2);
if(clientMailbox() == NULL || clientMailbox()->getChannel() == NULL ||
clientMailbox()->getChannel()->pEndPoint() == NULL)
{
PyTuple_SetItem(pyobj, 0, PyLong_FromLong(0));
PyTuple_SetItem(pyobj, 1, PyLong_FromLong(0));
}
else
{
const Network::Address& addr = clientMailbox()->getChannel()->pEndPoint()->addr();
PyTuple_SetItem(pyobj, 0, PyLong_FromUnsignedLong(addr.ip));
PyTuple_SetItem(pyobj, 1, PyLong_FromUnsignedLong(addr.port));
}
return pyobj;
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::pyGetEntitiesEnabled()
{
if(isDestroyed())
{
PyErr_Format(PyExc_AssertionError, "%s: %d is destroyed!\n",
scriptName(), id());
PyErr_PrintEx(0);
return 0;
}
if(this->entitiesEnabled())
{
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::__py_pyStreamFileToClient(PyObject* self, PyObject* args)
{
uint16 currargsSize = PyTuple_Size(args);
Proxy* pobj = static_cast<Proxy*>(self);
if(pobj->clientMailbox() == NULL)
{
PyErr_Format(PyExc_AssertionError,
"Proxy::streamStringToClient: has no client.");
PyErr_PrintEx(0);
return NULL;
}
if(currargsSize > 3 || currargsSize == 0)
{
PyErr_Format(PyExc_AssertionError,
"Proxy::streamFileToClient: args max require 3, gived %d! is script[%s].\n",
currargsSize, pobj->scriptName());
PyErr_PrintEx(0);
return NULL;
}
PyObject* pyResourceName = NULL;
PyObject* pyDesc = NULL;
int16 id = -1;
if(currargsSize == 1)
{
if(PyArg_ParseTuple(args, "O", &pyResourceName) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamFileToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
else if(currargsSize == 2)
{
if(PyArg_ParseTuple(args, "O|O", &pyResourceName, &pyDesc) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamFileToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
else if(currargsSize == 3)
{
if(PyArg_ParseTuple(args, "O|O|H", &pyResourceName, &pyDesc, &id) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamFileToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
char* pDescr = NULL;
if(pDescr != NULL)
{
wchar_t* PyUnicode_AsWideCharStringRet1 = PyUnicode_AsWideCharString(pyDesc, NULL);
pDescr = strutil::wchar2char(PyUnicode_AsWideCharStringRet1);
PyMem_Free(PyUnicode_AsWideCharStringRet1);
}
if(pDescr && strlen(pDescr) > 255)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamFileToClient: the descr-size(%d > 255)!",
strlen(pDescr));
PyErr_PrintEx(0);
free(pDescr);
return NULL;
}
int16 rid = pobj->streamFileToClient(pyResourceName,
(pDescr == NULL ? "" : pDescr),
id);
if(pDescr)
free(pDescr);
return PyLong_FromLong(rid);
}
//-------------------------------------------------------------------------------------
int16 Proxy::streamFileToClient(PyObjectPtr objptr,
const std::string& descr, int16 id)
{
DataDownload* pDataDownload = DataDownloadFactory::create(
DataDownloadFactory::DATA_DOWNLOAD_STREAM_FILE, objptr, descr, id);
pDataDownload->entityID(this->id());
return dataDownloads_.pushDownload(pDataDownload);
}
//-------------------------------------------------------------------------------------
PyObject* Proxy::__py_pyStreamStringToClient(PyObject* self, PyObject* args)
{
uint16 currargsSize = PyTuple_Size(args);
Proxy* pobj = static_cast<Proxy*>(self);
if(pobj->clientMailbox() == NULL)
{
PyErr_Format(PyExc_AssertionError,
"Proxy::streamStringToClient: has no client.");
PyErr_PrintEx(0);
return NULL;
}
if(currargsSize > 3 || currargsSize == 0)
{
PyErr_Format(PyExc_AssertionError,
"Proxy::streamStringToClient: args max require 3, gived %d! is script[%s].\n",
currargsSize, pobj->scriptName());
PyErr_PrintEx(0);
return NULL;
}
PyObject* pyData = NULL;
PyObject* pyDesc = NULL;
int16 id = -1;
if(currargsSize == 1)
{
if(PyArg_ParseTuple(args, "O", &pyData) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamStringToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
else if(currargsSize == 2)
{
if(PyArg_ParseTuple(args, "O|O", &pyData, &pyDesc) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamStringToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
else if(currargsSize == 3)
{
if(PyArg_ParseTuple(args, "O|O|H", &pyData, &pyDesc, &id) == -1)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamStringToClient: args is error!");
PyErr_PrintEx(0);
return NULL;
}
}
char* pDescr = NULL;
if(pDescr != NULL)
{
wchar_t* PyUnicode_AsWideCharStringRet1 = PyUnicode_AsWideCharString(pyDesc, NULL);
pDescr = strutil::wchar2char(PyUnicode_AsWideCharStringRet1);
PyMem_Free(PyUnicode_AsWideCharStringRet1);
}
if(pDescr && strlen(pDescr) > 255)
{
PyErr_Format(PyExc_TypeError, "Proxy::streamFileToClient: the descr-size(%d > 255)!",
strlen(pDescr));
PyErr_PrintEx(0);
free(pDescr);
return NULL;
}
int16 rid = pobj->streamStringToClient(pyData,
(pDescr == NULL ? "" : pDescr),
id);
if(pDescr)
free(pDescr);
return PyLong_FromLong(rid);
}
//-------------------------------------------------------------------------------------
int16 Proxy::streamStringToClient(PyObjectPtr objptr,
const std::string& descr, int16 id)
{
DataDownload* pDataDownload = DataDownloadFactory::create(
DataDownloadFactory::DATA_DOWNLOAD_STREAM_STRING, objptr, descr, id);
pDataDownload->entityID(this->id());
return dataDownloads_.pushDownload(pDataDownload);
}
//-------------------------------------------------------------------------------------
bool Proxy::pushBundle(Network::Bundle* pBundle)
{
if(!clientMailbox())
return false;
Network::Channel* pChannel = clientMailbox()->getChannel();
if(!pChannel)
return false;
pChannel->send(pBundle);
return true;
}
//-------------------------------------------------------------------------------------
bool Proxy::sendToClient(const Network::MessageHandler& msgHandler, Network::Bundle* pBundle)
{
return sendToClient(pBundle);
}
//-------------------------------------------------------------------------------------
bool Proxy::sendToClient(Network::Bundle* pBundle)
{
if(pushBundle(pBundle))
return true;
ERROR_MSG(fmt::format("Proxy::sendToClient: {} pBundles is NULL, not found channel.\n", id()));
Network::Bundle::reclaimPoolObject(pBundle);
return false;
}
//-------------------------------------------------------------------------------------
bool Proxy::sendToClient(bool expectData)
{
if(!clientMailbox())
return false;
Network::Channel* pChannel = clientMailbox()->getChannel();
if(!pChannel)
return false;
if(expectData)
{
if(pChannel->bundles().size() == 0)
{
WARNING_MSG("Proxy::sendToClient: no data!\n");
return false;
}
}
{
// 如果数据大量阻塞发不出去将会报警
AUTO_SCOPED_PROFILE("sendToClient");
pChannel->send();
}
return true;
}
//-------------------------------------------------------------------------------------
void Proxy::onStreamComplete(int16 id, bool success)
{
SCOPED_PROFILE(SCRIPTCALL_PROFILE);
SCRIPT_OBJECT_CALL_ARGS2(this, const_cast<char*>("onStreamComplete"),
const_cast<char*>("hO"), id, success ? Py_True : Py_False);
}
//-------------------------------------------------------------------------------------
}