-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_server.cpp
38 lines (33 loc) · 1.05 KB
/
echo_server.cpp
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
#include "spdlog/spdlog.h"
#include "tcp_server.hpp"
using namespace loevent;
void onConnection(const TcpConnectionPtr &conn) {
// spdlog::debug("[onConnection] fd: {}", conn->getFd());
int fd = conn->getFd();
if (fd % 200 == 0) {
spdlog::info("[onConnection] fd: {}", fd);
}
}
void onMessage(const TcpConnectionPtr &conn) {
auto buffer = conn->getRecvBuffer();
int rb = buffer->readableBytes();
// spdlog::info("[onMessage] recv len: {}", rb);
// printHexDump(buffer->start(), rb);
conn->send(buffer->start(), rb);
buffer->retrieve(rb);
}
int main(int argc, char const *argv[]) {
// spdlog::set_level(spdlog::level::debug);
if (argc < 3) {
error_quit("Example: ./server <port> <threadNum>");
}
int port = strtol(argv[1], NULL, 10);
int threadNum = strtol(argv[2], NULL, 10);
EventLoop eventLoop(20480, threadNum);
spdlog::info("server running...");
TcpServer tcpServer(eventLoop, port, "s233");
tcpServer.setConnectionCallback(onConnection);
tcpServer.setMessageCallback(onMessage);
eventLoop.loop();
return 0;
}