-
Notifications
You must be signed in to change notification settings - Fork 240
/
BroadCastServer.cpp
162 lines (137 loc) · 4.74 KB
/
BroadCastServer.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <atomic>
#include <brynet/base/AppStatus.hpp>
#include <brynet/base/Packet.hpp>
#include <brynet/net/EventLoop.hpp>
#include <brynet/net/ListenThread.hpp>
#include <brynet/net/SocketLibFunction.hpp>
#include <brynet/net/TcpConnection.hpp>
#include <brynet/net/TcpService.hpp>
#include <chrono>
#include <cstdio>
#include <iostream>
#include <thread>
#include <vector>
using namespace brynet;
using namespace brynet::net;
using namespace brynet::base;
std::atomic_llong TotalSendLen = ATOMIC_VAR_INIT(0);
std::atomic_llong TotalRecvLen = ATOMIC_VAR_INIT(0);
std::atomic_llong SendPacketNum = ATOMIC_VAR_INIT(0);
std::atomic_llong RecvPacketNum = ATOMIC_VAR_INIT(0);
std::vector<TcpConnection::Ptr> clients;
IOThreadTcpService::Ptr service;
static void addClientID(const TcpConnection::Ptr& session)
{
clients.push_back(session);
}
static void removeClientID(const TcpConnection::Ptr& session)
{
for (auto it = clients.begin(); it != clients.end(); ++it)
{
if (*it == session)
{
clients.erase(it);
break;
}
}
}
static size_t getClientNum()
{
return clients.size();
}
static void broadCastPacket(const brynet::net::SendableMsg::Ptr& packet)
{
auto packetLen = packet->size();
RecvPacketNum++;
TotalRecvLen += packetLen;
for (const auto& session : clients)
{
session->send(packet);
}
SendPacketNum += clients.size();
TotalSendLen += (clients.size() * packetLen);
}
int main(int argc, char** argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage : <listen port> <thread num> \n");
exit(-1);
}
int port = atoi(argv[1]);
int threadNum = atoi(argv[2]);
brynet::net::base::InitSocket();
service = IOThreadTcpService::Create();
auto mainLoop = std::make_shared<EventLoop>();
mainLoop->bindCurrentThread();
auto listenThread = ListenThread::Create(false, "0.0.0.0", port, [mainLoop](TcpSocket::Ptr socket) {
socket->setNodelay();
socket->setSendSize(32 * 1024);
socket->setRecvSize(32 * 1024);
auto enterCallback = [mainLoop](const TcpConnection::Ptr& session) {
mainLoop->runAsyncFunctor([session]() {
addClientID(session);
});
session->setDisConnectCallback([mainLoop](const TcpConnection::Ptr& session) {
mainLoop->runAsyncFunctor([session]() {
removeClientID(session);
});
});
session->setHighWaterCallback([]() {
std::cout << "high water" << std::endl;
},
1024 * 1024 * 100);
session->setDataCallback([mainLoop](brynet::base::BasePacketReader& reader) {
while (true)
{
auto buffer = reader.currentBuffer();
if (!reader.enough(sizeof(uint32_t)))
{
break;
}
auto packetLen = reader.readUINT32();
if (!reader.enough(packetLen - sizeof(uint32_t)))
{
break;
}
auto packet = brynet::net::MakeStringMsg(buffer, packetLen);
mainLoop->runAsyncFunctor([packet]() {
broadCastPacket(packet);
});
reader.addPos(packetLen - sizeof(uint32_t));
reader.savePos();
}
});
};
ConnectionOption option;
option.enterCallback.emplace_back(enterCallback);
option.maxRecvBufferSize = 1024 * 1024;
service->addTcpConnection(std::move(socket), option);
});
listenThread->startListen();
service->startWorkerThread(threadNum);
auto now = std::chrono::steady_clock::now();
while (true)
{
mainLoop->loop(10);
auto diff = std::chrono::steady_clock::now() - now;
if (diff >= std::chrono::seconds(1))
{
auto msDiff = std::chrono::duration_cast<std::chrono::milliseconds>(diff).count();
std::cout << "cost " << msDiff << " ms, clientnum:" << getClientNum() << ", recv " << (TotalRecvLen / 1024) * 1000 / msDiff << " K/s, "
<< "num : " << RecvPacketNum * 1000 / msDiff << ", send " << (TotalSendLen / 1024) / 1024 * 1000 / msDiff << " M/s, "
<< " num: " << SendPacketNum * 1000 / msDiff << std::endl;
TotalRecvLen = 0;
TotalSendLen = 0;
RecvPacketNum = 0;
SendPacketNum = 0;
now = std::chrono::steady_clock::now();
}
if (app_kbhit())
{
break;
}
}
service->stopWorkerThread();
return 0;
}