12a18e699SShuo Chen#include "TcpServer.h" 22a18e699SShuo Chen#include "EventLoop.h" 32a18e699SShuo Chen#include "InetAddress.h" 42a18e699SShuo Chen#include <stdio.h> 52a18e699SShuo Chen 62a18e699SShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn) 72a18e699SShuo Chen{ 82a18e699SShuo Chen if (conn->connected()) 92a18e699SShuo Chen { 102a18e699SShuo Chen printf("onConnection(): new connection [%s] from %s\n", 112a18e699SShuo Chen conn->name().c_str(), 122a18e699SShuo Chen conn->peerAddress().toHostPort().c_str()); 132a18e699SShuo Chen } 142a18e699SShuo Chen else 152a18e699SShuo Chen { 162a18e699SShuo Chen printf("onConnection(): connection [%s] is down\n", 172a18e699SShuo Chen conn->name().c_str()); 182a18e699SShuo Chen } 192a18e699SShuo Chen} 202a18e699SShuo Chen 212a18e699SShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn, 222a18e699SShuo Chen muduo::Buffer* buf, 232a18e699SShuo Chen muduo::Timestamp receiveTime) 242a18e699SShuo Chen{ 25cf2f7a34SShuo Chen printf("onMessage(): received %zd bytes from connection [%s] at %s\n", 262a18e699SShuo Chen buf->readableBytes(), 272a18e699SShuo Chen conn->name().c_str(), 282a18e699SShuo Chen receiveTime.toFormattedString().c_str()); 292a18e699SShuo Chen 302a18e699SShuo Chen printf("onMessage(): [%s]\n", buf->retrieveAsString().c_str()); 312a18e699SShuo Chen} 322a18e699SShuo Chen 332a18e699SShuo Chenint main() 342a18e699SShuo Chen{ 352a18e699SShuo Chen printf("main(): pid = %d\n", getpid()); 362a18e699SShuo Chen 372a18e699SShuo Chen muduo::InetAddress listenAddr(9981); 382a18e699SShuo Chen muduo::EventLoop loop; 392a18e699SShuo Chen 402a18e699SShuo Chen muduo::TcpServer server(&loop, listenAddr); 412a18e699SShuo Chen server.setConnectionCallback(onConnection); 422a18e699SShuo Chen server.setMessageCallback(onMessage); 432a18e699SShuo Chen server.start(); 442a18e699SShuo Chen 452a18e699SShuo Chen loop.loop(); 462a18e699SShuo Chen} 47