test8.cc revision 714cd85f
165c497a3SShuo Chen#include "TcpServer.h" 265c497a3SShuo Chen#include "EventLoop.h" 365c497a3SShuo Chen#include "InetAddress.h" 465c497a3SShuo Chen#include <stdio.h> 565c497a3SShuo Chen 665c497a3SShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn) 765c497a3SShuo Chen{ 865c497a3SShuo Chen if (conn->connected()) 965c497a3SShuo Chen { 1065c497a3SShuo Chen printf("onConnection(): new connection [%s] from %s\n", 1165c497a3SShuo Chen conn->name().c_str(), 1265c497a3SShuo Chen conn->peerAddress().toHostPort().c_str()); 1365c497a3SShuo Chen } 1465c497a3SShuo Chen else 1565c497a3SShuo Chen { 1665c497a3SShuo Chen printf("onConnection(): connection [%s] is down\n", 1765c497a3SShuo Chen conn->name().c_str()); 1865c497a3SShuo Chen } 1965c497a3SShuo Chen} 2065c497a3SShuo Chen 2165c497a3SShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn, 22714cd85fSShuo Chen muduo::Buffer* buf, 23714cd85fSShuo Chen muduo::Timestamp receiveTime) 2465c497a3SShuo Chen{ 25714cd85fSShuo Chen printf("onMessage(): received %3zd bytes from connection [%s] at %s\n", 26714cd85fSShuo Chen buf->readableBytes(), 27714cd85fSShuo Chen conn->name().c_str(), 28714cd85fSShuo Chen receiveTime.toFormattedString().c_str()); 29714cd85fSShuo Chen 30714cd85fSShuo Chen printf("onMessage(): [%s]\n", buf->retrieveAsString().c_str()); 3165c497a3SShuo Chen} 3265c497a3SShuo Chen 3365c497a3SShuo Chenint main() 3465c497a3SShuo Chen{ 3565c497a3SShuo Chen printf("main(): pid = %d\n", getpid()); 3665c497a3SShuo Chen 3765c497a3SShuo Chen muduo::InetAddress listenAddr(9981); 3865c497a3SShuo Chen muduo::EventLoop loop; 3965c497a3SShuo Chen 4065c497a3SShuo Chen muduo::TcpServer server(&loop, listenAddr); 4165c497a3SShuo Chen server.setConnectionCallback(onConnection); 4265c497a3SShuo Chen server.setMessageCallback(onMessage); 4365c497a3SShuo Chen server.start(); 4465c497a3SShuo Chen 4565c497a3SShuo Chen loop.loop(); 4665c497a3SShuo Chen} 47