-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCocosNet.cpp
285 lines (269 loc) · 6.25 KB
/
CocosNet.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
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
//
// CocosNet.cpp
// Dragon
//
// Created by GameDeveloper on 13-10-20.
//
//
#include "CocosNet.h"
#include "mythread.h"
#if (0)
#include "CCLuaEngine.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "tolua++.h"
#include "tolua_fix.h"
}
#endif
class WorkThread:public thread::Thread{
public:
WorkThread():thread::Thread("WORKTHREAD"){
net = NULL;
}
void run()
{
net->run();
}
CocosNet *net;
};
void IO::start(CocosNet *net)
{
WorkThread * work = new WorkThread;
work->net = net;
work->start();
}
using namespace cocos2d;
using namespace mynet;
void CocosNet::newClient(int index,const char *ip,unsigned short port)
{
if (index < clients.size())
{
mynet::Client *client = clients[index];
if (client)
{
if (!client->checkValid())
{
client->reconnect();
pool->bindEvent(client,IN_EVT|OUT_EVT);
}
return;
}
}
mynet::Client *client = new mynet::Client(ip,port);
pool->bindEvent(client,IN_EVT|OUT_EVT);
if (index >= clients.size())
{
clients.resize(index + 1);
}
clients[index] = client;
}
void CocosNet::updateClient(int index)
{
if (index < clients.size())
{
mynet::Client *client = clients[index];
if (client)
{
if (!client->checkValid())
{
client->reconnect();
pool->bindEvent(client,IN_EVT|OUT_EVT);
}
return;
}
}
}
void CocosNet::updateTimer(float dt)
{
popCommands();
}
void CocosNet::init()
{
if (valid) return;
if (pool) return;
valid = true;
pool = new EventPool();
pool->init();
cocos2d::CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(CocosNet::updateTimer), this, 1.0f/15.0f, false);
io.start(this);
}
void CocosNet::sendCmd(int index,void *cmd,unsigned int len)
{
if (index < clients.size())
{
mynet::Client *client = clients[index];
if (client)
client->sendCmd(cmd,len);
}
}
void CocosNet::stop()
{
cocos2d::CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CocosNet::updateTimer),this);
valid = false;
}
/**
*
*/
void CocosNet::run()
{
#if (1)
while (valid) //
{
EventBase *evt = pool->pullEvent();
if (evt)
{
if (evt->isErr()) //
{
Connection *conn = (Connection*) evt->target;
if (conn)
{
conn->destroy();
//delete conn;
}
continue;
}
if (evt->isOut()) //
{
Connection *connEvt = (Connection*) evt->target;
if (evt->target)
{
connEvt->doSend(evt);
}
}
if (evt->isIn()) //
{
Connection *connEvt = (Connection*) evt->target;
if (evt->target)
{
connEvt->doRead(evt,this);
}
}
}
}
#endif
}
void CocosNet::sendtoServer(int index,LuaReqData *reqData)
{
this->updateClient(index);
protoStringCmd cmd;
cmd.reqID = reqData->reqId;
setReqData(reqData);
cmd.content = reqData->content;
reqData->retain();
sendObject(index, &cmd);
}
static long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
void CocosNet::handle(void *cmd, unsigned int len)
{
protoStringCmd stringCmd;
stringCmd.parsecmd(cmd, len);
if (stringCmd.__msg__id__ == 1)
{
printf("%s",stringCmd.content.c_str());
}
if (*(int*) cmd == 8)
{
stSendFileInfo rev;
rev.parsecmd(cmd,len);
FILE * stream = fopen(rev.fileName.c_str(),"a+");
if (stream)
{
if (filesize(stream) != rev.offset)
{
// printf("写入文件失败\n");
}
///else
{
fwrite(&rev.buffer[0],rev.buffer.size(),1,stream);
}
fclose(stream);
printf("%s 文件写入成功 %u\n",rev.fileName.c_str(),rev.offset);
}
else printf("文件不存在 %s",rev.fileName.c_str());
}
#if (0)
// 处理回调
protoStringCmd stringCmd;
stringCmd.parsecmd(cmd, len);
if (stringCmd.__msg__id__ == 1)
{
LuaReqData *reqData = (LuaReqData*)getReqData(stringCmd.reqID);
if (reqData)
{
// lua 处理方式
lua_getglobal(CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState(), "doLuaNetCallback");
if (!lua_isfunction(CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState(), -1))
{
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", "doLuaNetCallback");
lua_pop(CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState(), 1);
return;
}
tolua_pushusertype(CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState(), reqData, "LuaReqData");
CCLuaEngine::defaultEngine()->getLuaStack()->pushString(stringCmd.content.c_str());
int ret = CCLuaEngine::defaultEngine()->getLuaStack()->executeFunction(2);
CCLuaEngine::defaultEngine()->getLuaStack()->clean();
removeReqData(reqData);
reqData->release();
}
}
#endif
}
/*
* 确定文件大小 和 起始值
*/
void CocosNet::sendFile(int index,const char *fileName)
{
stWaitSendFileInfo* info = new stWaitSendFileInfo();
info->fileName = fileName;
std::string realpath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName);
info->netIndex = index;
FILE *file = fopen(realpath.c_str(),"rb");
if (!file)
{
delete info;
return;
}
info->fileSize = filesize(file);
fclose(file);
files.write(info);
}
void CocosNet::doSendFile()
{
stWaitSendFileInfo* info;
if (files.readOnly(info))
{
// 构建8192 大小的数据块
if (info->offset >= info->fileSize)
{
files.pop();
delete info;
}
else
{
int left = info->fileSize - info->offset;
if (left >= 8192) left = 8192;
// 拷贝left 数据
std::string realpath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(info->fileName.c_str());
FILE *file = fopen(realpath.c_str(),"rb");
if (!file) return;
fseek(file,info->offset,SEEK_CUR);
stSendFileInfo cmd;
cmd.fileName = info->fileName;
cmd.buffer.resize(left);
cmd.offset = info->offset;
fread(&cmd.buffer[0],left,1,file);
sendObject(info->netIndex,&cmd);
fclose(file);
info->offset += left;
}
}
}