13b467340SShuo Chen #include "TcpServer.h" 23b467340SShuo Chen #include "EventLoop.h" 33b467340SShuo Chen #include "InetAddress.h" 43b467340SShuo Chen #include <stdio.h> 53b467340SShuo Chen 63b467340SShuo Chen void onConnection(const muduo::TcpConnectionPtr& conn) 73b467340SShuo Chen { 83b467340SShuo Chen if (conn->connected()) 93b467340SShuo Chen { 103b467340SShuo Chen printf("onConnection(): new connection [%s] from %s\n", 113b467340SShuo Chen conn->name().c_str(), 123b467340SShuo Chen conn->peerAddress().toHostPort().c_str()); 133b467340SShuo Chen } 143b467340SShuo Chen else 153b467340SShuo Chen { 163b467340SShuo Chen printf("onConnection(): connection [%s] is down\n", 173b467340SShuo Chen conn->name().c_str()); 183b467340SShuo Chen } 193b467340SShuo Chen } 203b467340SShuo Chen 213b467340SShuo Chen void onMessage(const muduo::TcpConnectionPtr& conn, 223b467340SShuo Chen! muduo::Buffer* buf, 233b467340SShuo Chen! muduo::Timestamp receiveTime) 243b467340SShuo Chen { 25cf2f7a34SShuo Chen+ printf("onMessage(): received %zd bytes from connection [%s] at %s\n", 263b467340SShuo Chen+ buf->readableBytes(), 273b467340SShuo Chen+ conn->name().c_str(), 283b467340SShuo Chen+ receiveTime.toFormattedString().c_str()); 293b467340SShuo Chen+ 303b467340SShuo Chen+ printf("onMessage(): [%s]\n", buf->retrieveAsString().c_str()); 313b467340SShuo Chen } 323b467340SShuo Chen 333b467340SShuo Chen int main() 343b467340SShuo Chen { 353b467340SShuo Chen printf("main(): pid = %d\n", getpid()); 363b467340SShuo Chen 373b467340SShuo Chen muduo::InetAddress listenAddr(9981); 383b467340SShuo Chen muduo::EventLoop loop; 393b467340SShuo Chen 403b467340SShuo Chen muduo::TcpServer server(&loop, listenAddr); 413b467340SShuo Chen server.setConnectionCallback(onConnection); 423b467340SShuo Chen server.setMessageCallback(onMessage); 433b467340SShuo Chen server.start(); 443b467340SShuo Chen 453b467340SShuo Chen loop.loop(); 463b467340SShuo Chen } 47