-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathcpp_server.cpp
More file actions
93 lines (70 loc) · 1.65 KB
/
cpp_server.cpp
File metadata and controls
93 lines (70 loc) · 1.65 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
#include <stdio.h>
#include <stdlib.h>
#include "NetEvent.h"
#include "EventLoop.h"
#include "NetServer.h"
#include "NetConnect.h"
#include "NetPacket.h"
#include "testmsg.h"
class INetEvent : public NetEvent
{
public:
virtual void onAccept(NetConnect * conn) {
if (!conn)
return;
testmsg msg;
msg.id = 1001;
msg.play.name = "jw";
msg.play.level = 10;
msg.play.msgcount = 3;
msg.array = {110,120,130};
Attr attr1;
attr1.attack = 88;
attr1.hp = 99;
Attr attr2;
attr2.attack = 90;
attr2.hp = 100;
msg.attrs = { attr1, attr2 };
msg.vstr = { "aa", "bbb", "ccc" };
NetPacket packet;
msg.write(&packet);
conn->sendMsg(1, &packet);
};
virtual void onClose(NetConnect * conn) {
};
virtual void onMsg(NetConnect * conn,int msgtype, NetPacket * pack) {
testmsg msg;
msg.read(pack);
printf("id:%d\n", msg.id);
printf("play.name :%s\n", msg.play.name.c_str());
printf("play.level:%d\n", msg.play.level);
printf("play.msgcount:%d\n", msg.play.msgcount);
for (int i = 0; i < msg.array.size(); ++i)
{
printf("array: %d\n", msg.array[i]);
}
for (int i = 0; i < msg.attrs.size(); ++i)
{
printf("%d attack:%d\n", i, msg.attrs[i].attack);
printf("%d hp:%d\n", i, msg.attrs[i].hp);
}
for (int i = 0; i < msg.vstr.size(); ++i)
{
printf("vstr: %s\n", msg.vstr[i].c_str());
}
printf("----------------------------------------\n");
}
};
//#define CLIENT_TEST
int main()
{
//初始化事件循环
INetEvent eve;
EventLoop::Instance()->init();
NetServer server(EventLoop::Instance(), &eve);
server.listen("127.0.0.1", 3001);
//开启事件循环
EventLoop::Instance()->run();
system("pause");
return 0;
}