forked from YukunJ/Turtle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.cpp
More file actions
30 lines (28 loc) · 891 Bytes
/
echo_server.cpp
File metadata and controls
30 lines (28 loc) · 891 Bytes
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
/**
* @file echo_server.cpp
* @author Yukun J
* @expectation this is the demo echo server for illustration and test purpose
* @init_date Dec 26 2022
*/
#include "core/turtle_server.h"
int main() {
TURTLE_SERVER::NetAddress local_address("0.0.0.0", 20080);
TURTLE_SERVER::TurtleServer echo_server(local_address);
echo_server
.OnHandle([&](TURTLE_SERVER::Connection *client_conn) {
int from_fd = client_conn->GetFd();
auto [read, exit] = client_conn->Recv();
if (exit) {
client_conn->GetLooper()->DeleteConnection(from_fd);
// client_conn ptr is invalid below here, do not touch it again
return;
}
if (read) {
client_conn->WriteToWriteBuffer(client_conn->ReadAsString());
client_conn->Send();
client_conn->ClearReadBuffer();
}
})
.Begin();
return 0;
}