-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpcobject.cpp
More file actions
178 lines (152 loc) · 4.5 KB
/
Copy pathrpcobject.cpp
File metadata and controls
178 lines (152 loc) · 4.5 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
#include "rpcobject.h"
#include "superrpc.h"
#include "rpcobjectmanager.h"
namespace superrpc
{
RPCStream & operator<<( RPCStream& os,const RPCObject & c)
{
os << c.m_objectID;
os << c.m_clientID ;
os << c.m_className;
return os;
}
RPCStream & operator>>( RPCStream & is,RPCObject & c)
{
is >> c.m_objectID;
is >> c.m_clientID;
is >> c.m_className;
return is;
}
RPCStream & operator<<( RPCStream & os,const RPCObjectCall & c)
{
const RPCObject *pObject = &c;
os << *pObject ;
os << c.m_strCallAddress;
return os;
}
RPCStream & operator>>( RPCStream & is,RPCObjectCall & c)
{
RPCObject *pObject = &c;
is >> (*pObject);
is >> c.m_strCallAddress;
return is;
}
RPCObject::RPCObject(/* args */)
{
m_bNetObject = false;
m_funcindex = 0;
m_objectID = 0;
}
RPCObject::~RPCObject()
{
}
void RPCObject::init()
{
for(auto &iter : m_arrInit){
iter();
}
}
void RPCObject::onNetFunc(NetFunc *pFunc)
{
if(pFunc->bCall){
char* str;
long i = strtol(pFunc->strName.c_str(), &str, 16);
std::function<void(std::vector<char>&)> *pCall = (std::function<void(std::vector<char>&)> *)i;
(*pCall)(pFunc->data);
}
else{
auto func = m_mapNetfunc.find(pFunc->strName);
if(func == m_mapNetfunc.end()){
return;
}
func->second(pFunc);
}
}
void RPCObject::onNetReturn(NetFunc *pFunc)
{
auto func = m_mapReturnFunc.find(pFunc->index);
if(func == m_mapReturnFunc.end()){
return;
}
func->second(pFunc);
m_mapReturnFunc.erase(func);
}
std::int64_t RPCObject::getNewFuncIndex()
{
m_funcindex++;
return m_funcindex;
}
void RPCObject::sendData(const char* name, NETFUNC func,std::vector<char>& arg)
{
NetFunc funcinfo;
funcinfo.objectID = m_objectID;
funcinfo.index = getNewFuncIndex();
funcinfo.strName = name;
funcinfo.clientID = m_clientID;
funcinfo.data = arg;
funcinfo.dataSize = arg.size();
m_mapReturnFunc[funcinfo.index] = func;
//SendFuncCall(&funcinfo);
m_pManager->sendFuncCall(&funcinfo);
}
void RPCObject::sendReturnData(std::int64_t index,std::vector<char>& arg)
{
NetFunc funcinfo;
funcinfo.objectID = m_objectID;
funcinfo.index = index;
funcinfo.clientID = m_clientID;
funcinfo.data = arg;
funcinfo.strName = "return";
funcinfo.dataSize = arg.size();
//SendFuncReturn(&funcinfo);
m_pManager->sendFuncReturn(&funcinfo);
}
void RPCObject::sendCallData(std::string callAddress,std::vector<char>& arg)
{
NetFunc funcinfo;
funcinfo.objectID = m_objectID;
funcinfo.index = 0;
funcinfo.clientID = m_clientID;
funcinfo.data = arg;
funcinfo.strName = callAddress;
funcinfo.dataSize = arg.size();
funcinfo.bCall = true;
//SendFuncReturn(&funcinfo);
m_pManager->sendFuncCall(&funcinfo);
}
static std::int64_t g_objectID = 0;
RPCObject* InitRPCObject(RPCObject *pObject,std::string strClientID)
{
g_objectID ++;
pObject->setClientID(strClientID);
pObject->setObjectID(g_objectID);
//bool bServer = ObjectManager::getInstance()->getBServer();
//pObject->m_bNetObject = bServer;
//RegisterNetObject(pObject);
return pObject;
}
static std::shared_ptr<std::unordered_map<std::string,CREATEFUNC>> g_mapCreate = nullptr;
void AddClassTemplate(std::string strClass,superrpc::CREATEFUNC func)
{
if(g_mapCreate == nullptr){
g_mapCreate = std::make_shared<std::unordered_map<std::string,CREATEFUNC>>();
}
g_mapCreate->insert(std::make_pair(strClass,func));
}
PTR_RPCObject CreateRPCObjectByName(std::string className)
{
auto findIter = g_mapCreate->find(className);
if(findIter != g_mapCreate->end()){
return findIter->second();
}
return nullptr;
}
void RPCObjectCall::call(std::string& data)
{
if(this->m_bNetObject){
//this->sendData(__func__,[](superrpc::NetFunc *pArg){},data);;
}
else{
}
}
};