This repository was archived by the owner on Mar 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoserver.cc
More file actions
131 lines (96 loc) · 2.83 KB
/
echoserver.cc
File metadata and controls
131 lines (96 loc) · 2.83 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
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
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <linux/version.h>
#include <xe/loop.h>
#include <xe/clock.h>
#include <xe/error.h>
#include <xe/io/socket.h>
#include <xutil/mem.h>
#include <xutil/log.h>
#include <xutil/endian.h>
#include "coroutine.h"
static ulong last_time, recvs = 0, sends = 0, clients = 0;
static int timer_callback(xe_loop& loop, xe_timer& timer){
ulong now = xe_time_ns();
xe_print("%lu recvs %lu sends in %f ms", recvs, sends, (now - last_time) / 1e6);
last_time = now;
recvs = 0;
sends = 0;
return 0;
}
static task echo(xe_loop& loop, int fd){
/* smaller buffer sizes yield greater performance due to close proximity between blocks */
constexpr uint buffer_length = 512;
xe_socket socket(loop);
int result;
byte* buf;
buf = xe_alloc_aligned<byte>(buffer_length, buffer_length);
socket.accept(fd);
while(true){
result = co_await socket.recv(buf, buffer_length, 0);
if(result <= 0)
break;
recvs++;
result = co_await socket.send(buf, result, 0);
if(result < 0)
break;
sends++;
}
socket.close();
xe_dealloc(buf);
xe_print("client closed with error: %s, %lu still open", xe_strerror(result), --clients);
}
static task start_server(xe_loop& loop){
/* listen addr */
sockaddr_in addr;
int yes = 1;
xe_zero(&addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = xe_hton<ushort>(8080);
xe_socket server(loop);
/* create a socket */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 19, 0)
server.init_sync(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#else
co_await server.init(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#endif
setsockopt(server.fd(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
server.bind((sockaddr*)&addr, sizeof(sockaddr));
server.listen(SOMAXCONN);
int client;
/* accept clients */
while(true){
client = co_await server.accept(null, null, 0);
if(client < 0)
break;
setsockopt(client, SOL_SOCKET, TCP_NODELAY, &yes, sizeof(yes));
echo(loop, client);
xe_print("accepted a client. %lu clients open", ++clients);
}
server.close();
xe_print("could not accept client: %s", xe_strerror(client));
}
int main(){
using namespace std::chrono_literals;
xe_loop loop;
xe_loop_options options;
xe_timer timer;
options.entries = 256; /* number of sqes, seems to work the best */
options.cq_entries = 65536;
options.flag_cqsize = true;
/* init */
loop.init_options(options);
xe_print("initialized with %u sqes and %u cqes", loop.sqe_count(), loop.cqe_count());
/* start */
start_server(loop);
/* stats */
timer.callback = timer_callback;
loop.timer(timer, 1s, 1s, XE_TIMER_REPEAT | XE_TIMER_ALIGN);
last_time = xe_time_ns();
loop.run();
/* cleanup */
loop.close();
return 0;
}