| 4 | #include "AsyncLogging.h" |
| 5 | |
| 6 | class EchoServer |
| 7 | { |
| 8 | public: |
| 9 | EchoServer(EventLoop *loop, const InetAddress &addr, const std::string &name) |
| 10 | : server_(loop, addr, name) |
| 11 | , loop_(loop) |
| 12 | { |
| 13 | // 注册回调函数 |
| 14 | server_.setConnectionCallback( |
| 15 | std::bind(&EchoServer::onConnection, this, std::placeholders::_1)); |
| 16 | |
| 17 | server_.setMessageCallback( |
| 18 | std::bind(&EchoServer::onMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); |
| 19 | |
| 20 | // 设置合适的subloop线程数量 |
| 21 | // server_.setThreadNum(3); |
| 22 | } |
| 23 | |
| 24 | void start() |
| 25 | { |
| 26 | server_.start(); |
| 27 | } |
| 28 | |
| 29 | private: |
| 30 | // 连接建立或断开的回调函数 |
| 31 | void onConnection(const TcpConnectionPtr &conn) |
| 32 | { |
| 33 | if (conn->connected()) |
| 34 | { |
| 35 | LOG_INFO << "Connection UP : " << conn->peerAddress().toIpPort().c_str(); |
| 36 | // LOG_INFO("Connection UP : %s", conn->peerAddress().toIpPort().c_str()); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | LOG_INFO << "Connection DOWN : " << conn->peerAddress().toIpPort().c_str(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // 可读写事件回调 |
| 45 | void onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time) |
| 46 | { |
| 47 | printf("onMessage!\n"); |
| 48 | std::string msg = buf->retrieveAllAsString(); |
| 49 | conn->send(msg); |
| 50 | // conn->shutdown(); // 关闭写端 底层响应EPOLLHUP => 执行closeCallback_ |
| 51 | } |
| 52 | |
| 53 | EventLoop *loop_; |
| 54 | TcpServer server_; |
| 55 | }; |
| 56 | |
| 57 | int kRollSize = 500*1000*1000; |
| 58 |
nothing calls this directly
no outgoing calls
no test coverage detected