-
Notifications
You must be signed in to change notification settings - Fork 5
/
ServerNet.h
134 lines (126 loc) · 2.57 KB
/
ServerNet.h
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
//
// CocosNet.h
// Dragon
//
// Created by GameDeveloper on 13-10-20.
//
//
#include "mynet.h"
#include "cmdobject.h"
class ServerNet;
class IO{
public:
void start(ServerNet *net);
};
class ServerNet:public mynet::Handles,public mynet::stGetPackage{
public:
ServerNet()
{
}
~ServerNet()
{}
void newClient(int index,const char *ip,unsigned short port);
void newServer(unsigned short port);
void updateClient(int index);
void init();
void stop();
static CocosNet * get()
{
return new CocosNet();
}
void go();
void sendtoServer(int index,LuaReqData *reqData);
public:
IO io;
mynet::EventPool *pool;
std::vector<mynet::Client *> clients;
std::vector<mynet::Server *> servers;
static CocosNet & getMe()
{
static CocosNet t;
return t;
}
void updateTimer(float dt);
void bind(unsigned short commandID,mynet::MsgFuncHandler *handler)
{
addHandle(commandID,handler);
}
/**
*
*/
void run();
bool valid;
void sendCmd(int index,void *cmd,unsigned int len);
private:
void handle(void *cmd,unsigned int len);
void doGetCommand(void *cmd,unsigned int len)
{
pushCommands(new mynet::Record(cmd,len));
}
void pushCommands(mynet::Record *record)
{
records.write(record);
}
mynet::MyList<mynet::Record*> records;
/**
* 处理弹出消息
*/
void popCommands()
{
mynet::Record * record = NULL;
if (records.readAndPop(record))
{
handle(record->contents,record->contentSize); //
delete record;
}
doSendFile();
}
public:
/**
* 确定文件大小 和 起始值
*/
void sendFile(int index,const char *fileName);
/**
* 处理文件的发送
*/
mynet::MyList<stWaitSendFileInfo*> files;
/**
* 实际发送文件
**/
void doSendFile();
private:
void sendObject(int index,CmdObject *object)
{
cmd::Stream ss = object->toStream();
sendCmd(index,ss.content(),ss.size());
}
std::vector<stReqData*> reqDatas;
std::set<int> emptyIDs; // 空闲ID号
stReqData * getReqData(int reqId)
{
if (reqId < reqDatas.size())
{
return reqDatas[reqId];
}
return NULL;
}
void setReqData(stReqData *data)
{
if (emptyIDs.size())
{
int id = *emptyIDs.begin();
emptyIDs.erase(emptyIDs.begin());
reqDatas[id] = data;
data->reqId = id;
}else{
data->reqId = reqDatas.size();
reqDatas.push_back(data);
}
}
void removeReqData(stReqData *data)
{
emptyIDs.insert(data->reqId);
reqDatas[data->reqId] = NULL;
}
};
#endif