1b37003a7SShuo Chen#include "TcpServer.h"
2b37003a7SShuo Chen#include "EventLoop.h"
3b37003a7SShuo Chen#include "InetAddress.h"
4b37003a7SShuo Chen#include <stdio.h>
5b37003a7SShuo Chen
6b37003a7SShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn)
7b37003a7SShuo Chen{
8b37003a7SShuo Chen  if (conn->connected())
9b37003a7SShuo Chen  {
10b37003a7SShuo Chen    printf("onConnection(): new connection [%s] from %s\n",
11b37003a7SShuo Chen           conn->name().c_str(),
12b37003a7SShuo Chen           conn->peerAddress().toHostPort().c_str());
13b37003a7SShuo Chen  }
14b37003a7SShuo Chen  else
15b37003a7SShuo Chen  {
16b37003a7SShuo Chen    printf("onConnection(): connection [%s] is down\n",
17b37003a7SShuo Chen           conn->name().c_str());
18b37003a7SShuo Chen  }
19b37003a7SShuo Chen}
20b37003a7SShuo Chen
21b37003a7SShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn,
22b37003a7SShuo Chen               muduo::Buffer* buf,
23b37003a7SShuo Chen               muduo::Timestamp receiveTime)
24b37003a7SShuo Chen{
250ce27c5bSShuo Chen  printf("onMessage(): received %zd bytes from connection [%s] at %s\n",
26b37003a7SShuo Chen         buf->readableBytes(),
27b37003a7SShuo Chen         conn->name().c_str(),
28b37003a7SShuo Chen         receiveTime.toFormattedString().c_str());
29b37003a7SShuo Chen
30b37003a7SShuo Chen  conn->send(buf->retrieveAsString());
31b37003a7SShuo Chen}
32b37003a7SShuo Chen
33b37003a7SShuo Chenint main()
34b37003a7SShuo Chen{
35b37003a7SShuo Chen  printf("main(): pid = %d\n", getpid());
36b37003a7SShuo Chen
37b37003a7SShuo Chen  muduo::InetAddress listenAddr(9981);
38b37003a7SShuo Chen  muduo::EventLoop loop;
39b37003a7SShuo Chen
40b37003a7SShuo Chen  muduo::TcpServer server(&loop, listenAddr);
41b37003a7SShuo Chen  server.setConnectionCallback(onConnection);
42b37003a7SShuo Chen  server.setMessageCallback(onMessage);
43b37003a7SShuo Chen  server.start();
44b37003a7SShuo Chen
45b37003a7SShuo Chen  loop.loop();
46b37003a7SShuo Chen}
47