test11.cc revision 354280cf
1354280cfSShuo Chen#include "TcpServer.h"
2354280cfSShuo Chen#include "EventLoop.h"
3354280cfSShuo Chen#include "InetAddress.h"
4354280cfSShuo Chen#include <stdio.h>
5354280cfSShuo Chen
6354280cfSShuo Chenstd::string message;
7354280cfSShuo Chen
8354280cfSShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn)
9354280cfSShuo Chen{
10354280cfSShuo Chen  if (conn->connected())
11354280cfSShuo Chen  {
12354280cfSShuo Chen    printf("onConnection(): tid=%d new connection [%s] from %s\n",
13354280cfSShuo Chen           muduo::CurrentThread::tid(),
14354280cfSShuo Chen           conn->name().c_str(),
15354280cfSShuo Chen           conn->peerAddress().toHostPort().c_str());
16354280cfSShuo Chen    conn->send(message);
17354280cfSShuo Chen  }
18354280cfSShuo Chen  else
19354280cfSShuo Chen  {
20354280cfSShuo Chen    printf("onConnection(): tid=%d connection [%s] is down\n",
21354280cfSShuo Chen           muduo::CurrentThread::tid(),
22354280cfSShuo Chen           conn->name().c_str());
23354280cfSShuo Chen  }
24354280cfSShuo Chen}
25354280cfSShuo Chen
26354280cfSShuo Chenvoid onWriteComplete(const muduo::TcpConnectionPtr& conn)
27354280cfSShuo Chen{
28354280cfSShuo Chen  conn->send(message);
29354280cfSShuo Chen}
30354280cfSShuo Chen
31354280cfSShuo Chenvoid onMessage(const muduo::TcpConnectionPtr& conn,
32354280cfSShuo Chen               muduo::Buffer* buf,
33354280cfSShuo Chen               muduo::Timestamp receiveTime)
34354280cfSShuo Chen{
35354280cfSShuo Chen  printf("onMessage(): tid=%d received %zd bytes from connection [%s] at %s\n",
36354280cfSShuo Chen         muduo::CurrentThread::tid(),
37354280cfSShuo Chen         buf->readableBytes(),
38354280cfSShuo Chen         conn->name().c_str(),
39354280cfSShuo Chen         receiveTime.toFormattedString().c_str());
40354280cfSShuo Chen
41354280cfSShuo Chen  buf->retrieveAll();
42354280cfSShuo Chen}
43354280cfSShuo Chen
44354280cfSShuo Chenint main(int argc, char* argv[])
45354280cfSShuo Chen{
46354280cfSShuo Chen  printf("main(): pid = %d\n", getpid());
47354280cfSShuo Chen
48354280cfSShuo Chen  std::string line;
49354280cfSShuo Chen  for (int i = 33; i < 127; ++i)
50354280cfSShuo Chen  {
51354280cfSShuo Chen    line.push_back(char(i));
52354280cfSShuo Chen  }
53354280cfSShuo Chen  line += line;
54354280cfSShuo Chen
55354280cfSShuo Chen  for (size_t i = 0; i < 127-33; ++i)
56354280cfSShuo Chen  {
57354280cfSShuo Chen    message += line.substr(i, 72) + '\n';
58354280cfSShuo Chen  }
59354280cfSShuo Chen
60354280cfSShuo Chen  muduo::InetAddress listenAddr(9981);
61354280cfSShuo Chen  muduo::EventLoop loop;
62354280cfSShuo Chen
63354280cfSShuo Chen  muduo::TcpServer server(&loop, listenAddr);
64354280cfSShuo Chen  server.setConnectionCallback(onConnection);
65354280cfSShuo Chen  server.setMessageCallback(onMessage);
66354280cfSShuo Chen  server.setWriteCompleteCallback(onWriteComplete);
67354280cfSShuo Chen  if (argc > 1) {
68354280cfSShuo Chen    server.setThreadNum(atoi(argv[1]));
69354280cfSShuo Chen  }
70354280cfSShuo Chen  server.start();
71354280cfSShuo Chen
72354280cfSShuo Chen  loop.loop();
73354280cfSShuo Chen}
74