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