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