1a1bde736SShuo Chen#include "TcpServer.h"
2a1bde736SShuo Chen#include "EventLoop.h"
3a1bde736SShuo Chen#include "InetAddress.h"
4a1bde736SShuo Chen#include <stdio.h>
5a1bde736SShuo Chen
6a1bde736SShuo Chenstd::string message1;
7a1bde736SShuo Chenstd::string message2;
8a1bde736SShuo Chen
9a1bde736SShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn)
10a1bde736SShuo Chen{
11a1bde736SShuo Chen  if (conn->connected())
12a1bde736SShuo Chen  {
13a1bde736SShuo Chen    printf("onConnection(): tid=%d new connection [%s] from %s\n",
14a1bde736SShuo Chen           muduo::CurrentThread::tid(),
15a1bde736SShuo Chen           conn->name().c_str(),
16a1bde736SShuo Chen           conn->peerAddress().toHostPort().c_str());
17a1bde736SShuo Chen    if (!message1.empty())
18a1bde736SShuo Chen      conn->send(message1);
19a1bde736SShuo Chen    if (!message2.empty())
20a1bde736SShuo Chen      conn->send(message2);
21a1bde736SShuo Chen    conn->shutdown();
22a1bde736SShuo Chen  }
23a1bde736SShuo Chen  else
24a1bde736SShuo Chen  {
25a1bde736SShuo Chen    printf("onConnection(): tid=%d connection [%s] is down\n",
26a1bde736SShuo Chen           muduo::CurrentThread::tid(),
27a1bde736SShuo Chen           conn->name().c_str());
28a1bde736SShuo Chen  }
29a1bde736SShuo Chen}
30a1bde736SShuo Chen
31a1bde736SShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn,
32a1bde736SShuo Chen               muduo::Buffer* buf,
33a1bde736SShuo Chen               muduo::Timestamp receiveTime)
34a1bde736SShuo Chen{
35a1bde736SShuo Chen  printf("onMessage(): tid=%d received %zd bytes from connection [%s] at %s\n",
36a1bde736SShuo Chen         muduo::CurrentThread::tid(),
37a1bde736SShuo Chen         buf->readableBytes(),
38a1bde736SShuo Chen         conn->name().c_str(),
39a1bde736SShuo Chen         receiveTime.toFormattedString().c_str());
40a1bde736SShuo Chen
41a1bde736SShuo Chen  buf->retrieveAll();
42a1bde736SShuo Chen}
43a1bde736SShuo Chen
44a1bde736SShuo Chenint main(int argc, char* argv[])
45a1bde736SShuo Chen{
46a1bde736SShuo Chen  printf("main(): pid = %d\n", getpid());
47a1bde736SShuo Chen
48a1bde736SShuo Chen  int len1 = 100;
49a1bde736SShuo Chen  int len2 = 200;
50a1bde736SShuo Chen
51a1bde736SShuo Chen  if (argc > 2)
52a1bde736SShuo Chen  {
53a1bde736SShuo Chen    len1 = atoi(argv[1]);
54a1bde736SShuo Chen    len2 = atoi(argv[2]);
55a1bde736SShuo Chen  }
56a1bde736SShuo Chen
57a1bde736SShuo Chen  message1.resize(len1);
58a1bde736SShuo Chen  message2.resize(len2);
59a1bde736SShuo Chen  std::fill(message1.begin(), message1.end(), 'A');
60a1bde736SShuo Chen  std::fill(message2.begin(), message2.end(), 'B');
61a1bde736SShuo Chen
62a1bde736SShuo Chen  muduo::InetAddress listenAddr(9981);
63a1bde736SShuo Chen  muduo::EventLoop loop;
64a1bde736SShuo Chen
65a1bde736SShuo Chen  muduo::TcpServer server(&loop, listenAddr);
66a1bde736SShuo Chen  server.setConnectionCallback(onConnection);
67a1bde736SShuo Chen  server.setMessageCallback(onMessage);
68a1bde736SShuo Chen  if (argc > 3) {
69a1bde736SShuo Chen    server.setThreadNum(atoi(argv[3]));
70a1bde736SShuo Chen  }
71a1bde736SShuo Chen  server.start();
72a1bde736SShuo Chen
73a1bde736SShuo Chen  loop.loop();
74a1bde736SShuo Chen}
75