forked from chenshuo/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s10-s09-test11.cc.diff
73 lines (62 loc) · 1.71 KB
/
s10-s09-test11.cc.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "TcpServer.h"
#include "EventLoop.h"
#include "InetAddress.h"
#include <stdio.h>
std::string message;
void onConnection(const muduo::TcpConnectionPtr& conn)
{
if (conn->connected())
{
! printf("onConnection(): tid=%d new connection [%s] from %s\n",
+ muduo::CurrentThread::tid(),
conn->name().c_str(),
conn->peerAddress().toHostPort().c_str());
conn->send(message);
}
else
{
! printf("onConnection(): tid=%d connection [%s] is down\n",
+ muduo::CurrentThread::tid(),
conn->name().c_str());
}
}
void onWriteComplete(const muduo::TcpConnectionPtr& conn)
{
conn->send(message);
}
void onMessage(const muduo::TcpConnectionPtr& conn,
muduo::Buffer* buf,
muduo::Timestamp receiveTime)
{
! printf("onMessage(): tid=%d received %zd bytes from connection [%s] at %s\n",
+ muduo::CurrentThread::tid(),
buf->readableBytes(),
conn->name().c_str(),
receiveTime.toFormattedString().c_str());
buf->retrieveAll();
}
!int main(int argc, char* argv[])
{
printf("main(): pid = %d\n", getpid());
std::string line;
for (int i = 33; i < 127; ++i)
{
line.push_back(char(i));
}
line += line;
for (size_t i = 0; i < 127-33; ++i)
{
message += line.substr(i, 72) + '\n';
}
muduo::InetAddress listenAddr(9981);
muduo::EventLoop loop;
muduo::TcpServer server(&loop, listenAddr);
server.setConnectionCallback(onConnection);
server.setMessageCallback(onMessage);
server.setWriteCompleteCallback(onWriteComplete);
+ if (argc > 1) {
+ server.setThreadNum(atoi(argv[1]));
+ }
server.start();
loop.loop();
}