140161064SShuo Chen #include "TcpServer.h" 240161064SShuo Chen #include "EventLoop.h" 340161064SShuo Chen #include "InetAddress.h" 440161064SShuo Chen #include <stdio.h> 540161064SShuo Chen 640161064SShuo Chen std::string message1; 740161064SShuo Chen std::string message2; 840161064SShuo Chen 940161064SShuo Chen void onConnection(const muduo::TcpConnectionPtr& conn) 1040161064SShuo Chen { 1140161064SShuo Chen if (conn->connected()) 1240161064SShuo Chen { 1340161064SShuo Chen! printf("onConnection(): tid=%d new connection [%s] from %s\n", 1440161064SShuo Chen+ muduo::CurrentThread::tid(), 1540161064SShuo Chen conn->name().c_str(), 1640161064SShuo Chen conn->peerAddress().toHostPort().c_str()); 1740161064SShuo Chen+ if (!message1.empty()) 1840161064SShuo Chen conn->send(message1); 1940161064SShuo Chen+ if (!message2.empty()) 2040161064SShuo Chen conn->send(message2); 2140161064SShuo Chen conn->shutdown(); 2240161064SShuo Chen } 2340161064SShuo Chen else 2440161064SShuo Chen { 2540161064SShuo Chen! printf("onConnection(): tid=%d connection [%s] is down\n", 2640161064SShuo Chen+ muduo::CurrentThread::tid(), 2740161064SShuo Chen conn->name().c_str()); 2840161064SShuo Chen } 2940161064SShuo Chen } 3040161064SShuo Chen 3140161064SShuo Chen void onMessage(const muduo::TcpConnectionPtr& conn, 3240161064SShuo Chen muduo::Buffer* buf, 3340161064SShuo Chen muduo::Timestamp receiveTime) 3440161064SShuo Chen { 3540161064SShuo Chen! printf("onMessage(): tid=%d received %zd bytes from connection [%s] at %s\n", 3640161064SShuo Chen+ muduo::CurrentThread::tid(), 3740161064SShuo Chen buf->readableBytes(), 3840161064SShuo Chen conn->name().c_str(), 3940161064SShuo Chen receiveTime.toFormattedString().c_str()); 4040161064SShuo Chen 4140161064SShuo Chen buf->retrieveAll(); 4240161064SShuo Chen } 4340161064SShuo Chen 4440161064SShuo Chen int main(int argc, char* argv[]) 4540161064SShuo Chen { 4640161064SShuo Chen printf("main(): pid = %d\n", getpid()); 4740161064SShuo Chen 4840161064SShuo Chen int len1 = 100; 4940161064SShuo Chen int len2 = 200; 5040161064SShuo Chen 5140161064SShuo Chen if (argc > 2) 5240161064SShuo Chen { 5340161064SShuo Chen len1 = atoi(argv[1]); 5440161064SShuo Chen len2 = atoi(argv[2]); 5540161064SShuo Chen } 5640161064SShuo Chen 5740161064SShuo Chen message1.resize(len1); 5840161064SShuo Chen message2.resize(len2); 5940161064SShuo Chen std::fill(message1.begin(), message1.end(), 'A'); 6040161064SShuo Chen std::fill(message2.begin(), message2.end(), 'B'); 6140161064SShuo Chen 6240161064SShuo Chen muduo::InetAddress listenAddr(9981); 6340161064SShuo Chen muduo::EventLoop loop; 6440161064SShuo Chen 6540161064SShuo Chen muduo::TcpServer server(&loop, listenAddr); 6640161064SShuo Chen server.setConnectionCallback(onConnection); 6740161064SShuo Chen server.setMessageCallback(onMessage); 6840161064SShuo Chen+ if (argc > 3) { 6940161064SShuo Chen+ server.setThreadNum(atoi(argv[3])); 7040161064SShuo Chen+ } 7140161064SShuo Chen server.start(); 7240161064SShuo Chen 7340161064SShuo Chen loop.loop(); 7440161064SShuo Chen } 75