test8.cc revision 65c497a3
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,
2265c497a3SShuo Chen               const char* data,
2365c497a3SShuo Chen               ssize_t len)
2465c497a3SShuo Chen{
2565c497a3SShuo Chen  printf("onMessage(): received %zd bytes from connection [%s]\n",
2665c497a3SShuo Chen         len, conn->name().c_str());
2765c497a3SShuo Chen}
2865c497a3SShuo Chen
2965c497a3SShuo Chenint main()
3065c497a3SShuo Chen{
3165c497a3SShuo Chen  printf("main(): pid = %d\n", getpid());
3265c497a3SShuo Chen
3365c497a3SShuo Chen  muduo::InetAddress listenAddr(9981);
3465c497a3SShuo Chen  muduo::EventLoop loop;
3565c497a3SShuo Chen
3665c497a3SShuo Chen  muduo::TcpServer server(&loop, listenAddr);
3765c497a3SShuo Chen  server.setConnectionCallback(onConnection);
3865c497a3SShuo Chen  server.setMessageCallback(onMessage);
3965c497a3SShuo Chen  server.start();
4065c497a3SShuo Chen
4165c497a3SShuo Chen  loop.loop();
4265c497a3SShuo Chen}
43