-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cc
More file actions
64 lines (55 loc) · 1.6 KB
/
server.cc
File metadata and controls
64 lines (55 loc) · 1.6 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
#include <atomic>
#include <server.hh>
#include <functional>
#include <iostream>
#include <thread>
kcp::server* server_global = nullptr;
std::atomic_bool threads[3]{
ATOMIC_VAR_INIT(false),
ATOMIC_VAR_INIT(false),
ATOMIC_VAR_INIT(false)
};
std::thread::id tids[3];
int count[3];
std::atomic_int count_total {100};
void add_connection() {
for(int i = 0; i < 3; i++) {
if (!threads[i]) {
threads[i] = true;
tids[i] = std::this_thread::get_id();
count[i] ++;
break ;
} else if (tids[i] == std::this_thread::get_id()) {
count[i]++;
break ;
}
}
count_total--;
if (!count_total) {
server_global->stop();
}
}
void on_connect(kcp::channel_view channel, bool linked){
if(linked) {
add_connection();
std::cout << "[" << std::this_thread::get_id() << "]连接到来" << std::endl;
}else {
std::cout << "[" << std::this_thread::get_id() << "]连接断开" << std::endl;
}
}
void on_message(kcp::channel_view channel, kcp::packet packet){
// std::cout << "收到消息 : " << (const char*)packet->data() << std::endl;
channel->send(std::move(*packet));
}
int main() {
kcp::server server;
server_global = &server;
server.set_connect_callback(on_connect);
server.set_message_callback(on_message);
server.enable_muliti_thread(3);
server.start(8080);
std::cout << "thread[0] : " << count[0] << std::endl;
std::cout << "thread[1] : " << count[1] << std::endl;
std::cout << "thread[2] : " << count[2] << std::endl;
return 0;
}