-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
78 lines (69 loc) · 1.81 KB
/
Copy pathserver.cpp
File metadata and controls
78 lines (69 loc) · 1.81 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
#ifdef _WIN32
#define _WIN32_WINNT 0x0601
#endif
#include "net_common/net_server.hpp"
#include <iostream>
// client and server all need this
enum class CustomMsgType : uint32_t
{
ServerAccept,
ServerValidated,
ServerDeny,
ServerPing,
MessageAll,
ServerMessage,
};
class CustomServer : public net::server_interface<CustomMsgType>
{
public:
CustomServer(uint16_t port) : net::server_interface<CustomMsgType>(port) {}
protected:
virtual bool OnClientConnect(std::shared_ptr<net::connection<CustomMsgType>> client)
{
net::message<CustomMsgType> msg;
msg.header.id = CustomMsgType::ServerAccept;
client->Send(msg);
return true;
}
virtual void OnClientValidated(std::shared_ptr<net::connection<CustomMsgType>> client)
{
net::message<CustomMsgType> msg;
msg.header.id = CustomMsgType::ServerValidated;
client->Send(msg);
}
virtual void OnClientDisConnect(std::shared_ptr<net::connection<CustomMsgType>> client)
{
std::cout << "Removing client [" << client->GetID() << "], Remain client count: " << ClientCount() << std::endl;
}
virtual void OnMessage(std::shared_ptr<net::connection<CustomMsgType>> client, const net::message<CustomMsgType> &msg)
{
switch (msg.header.id)
{
case CustomMsgType::ServerPing:
{
std::cout << "[" << client->GetID() << "]" << "Server ping" << std::endl;
client->Send(msg);
break;
}
case CustomMsgType::MessageAll:
{
std::cout << "[" << client->GetID() << "]" << "MessageAll" << std::endl;
net::message<CustomMsgType> msg;
msg.header.id = CustomMsgType::ServerMessage;
msg << client->GetID();
SendMessageAllClients(msg, client);
break;
}
}
}
};
int main()
{
CustomServer server(5050);
server.Start();
while (true)
{
server.Update();
}
return 0;
}