test10.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 message1;
7354280cfSShuo Chenstd::string message2;
8354280cfSShuo Chen
9354280cfSShuo Chenvoid onConnection(const muduo::TcpConnectionPtr& conn)
10354280cfSShuo Chen{
11354280cfSShuo Chen  if (conn->connected())
12354280cfSShuo Chen  {
13354280cfSShuo Chen    printf("onConnection(): tid=%d new connection [%s] from %s\n",
14354280cfSShuo Chen           muduo::CurrentThread::tid(),
15354280cfSShuo Chen           conn->name().c_str(),
16354280cfSShuo Chen           conn->peerAddress().toHostPort().c_str());
17354280cfSShuo Chen    if (!message1.empty())
18354280cfSShuo Chen      conn->send(message1);
19354280cfSShuo Chen    if (!message2.empty())
20354280cfSShuo Chen      conn->send(message2);
21354280cfSShuo Chen    conn->shutdown();
22354280cfSShuo Chen  }
23354280cfSShuo Chen  else
24354280cfSShuo Chen  {
25354280cfSShuo Chen    printf("onConnection(): tid=%d connection [%s] is down\n",
26354280cfSShuo Chen           muduo::CurrentThread::tid(),
27354280cfSShuo Chen           conn->name().c_str());
28354280cfSShuo Chen  }
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  int len1 = 100;
49354280cfSShuo Chen  int len2 = 200;
50354280cfSShuo Chen
51354280cfSShuo Chen  if (argc > 2)
52354280cfSShuo Chen  {
53354280cfSShuo Chen    len1 = atoi(argv[1]);
54354280cfSShuo Chen    len2 = atoi(argv[2]);
55354280cfSShuo Chen  }
56354280cfSShuo Chen
57354280cfSShuo Chen  message1.resize(len1);
58354280cfSShuo Chen  message2.resize(len2);
59354280cfSShuo Chen  std::fill(message1.begin(), message1.end(), 'A');
60354280cfSShuo Chen  std::fill(message2.begin(), message2.end(), 'B');
61354280cfSShuo Chen
62354280cfSShuo Chen  muduo::InetAddress listenAddr(9981);
63354280cfSShuo Chen  muduo::EventLoop loop;
64354280cfSShuo Chen
65354280cfSShuo Chen  muduo::TcpServer server(&loop, listenAddr);
66354280cfSShuo Chen  server.setConnectionCallback(onConnection);
67354280cfSShuo Chen  server.setMessageCallback(onMessage);
68354280cfSShuo Chen  if (argc > 3) {
69354280cfSShuo Chen    server.setThreadNum(atoi(argv[3]));
70354280cfSShuo Chen  }
71354280cfSShuo Chen  server.start();
72354280cfSShuo Chen
73354280cfSShuo Chen  loop.loop();
74354280cfSShuo Chen}
75