forked from IronsDu/brynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroadCastServer.cpp
More file actions
172 lines (146 loc) · 5.08 KB
/
BroadCastServer.cpp
File metadata and controls
172 lines (146 loc) · 5.08 KB
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
163
164
165
166
167
168
169
170
171
172
#include <functional>
#include <time.h>
#include <stdio.h>
#include <thread>
#include <iostream>
#include <assert.h>
#include <chrono>
#include <vector>
#include <atomic>
#include <brynet/utils/packet.h>
#include <brynet/net/SocketLibFunction.h>
#include <brynet/net/EventLoop.h>
#include <brynet/net/TcpConnection.h>
#include <brynet/net/TCPService.h>
#include <brynet/net/ListenThread.h>
using namespace brynet;
using namespace brynet::net;
using namespace brynet::utils;
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;
TcpService::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 TcpConnection::PacketPtr& 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 = TcpService::Create();
auto mainLoop = std::make_shared<EventLoop>();
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->setDataCallback([mainLoop](const char* buffer, size_t len) {
const char* parseStr = buffer;
size_t totalProcLen = 0;
size_t leftLen = len;
while (true)
{
bool flag = false;
auto HEAD_LEN = sizeof(uint32_t) + sizeof(uint16_t);
if (leftLen >= HEAD_LEN)
{
BasePacketReader rp(parseStr, leftLen);
auto packet_len = rp.readUINT32();
if (leftLen >= packet_len && packet_len >= HEAD_LEN)
{
auto packet = TcpConnection::makePacket(parseStr, packet_len);
mainLoop->runAsyncFunctor([packet]() {
broadCastPacket(packet);
});
totalProcLen += packet_len;
parseStr += packet_len;
leftLen -= packet_len;
flag = true;
}
rp.skipAll();
}
if (!flag)
{
break;
}
}
return totalProcLen;
});
};
service->addTcpConnection(std::move(socket),
brynet::net::TcpService::AddSocketOption::AddEnterCallback(enterCallback),
brynet::net::TcpService::AddSocketOption::WithMaxRecvBufferSize(1024 * 1024));
});
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();
}
}
service->stopWorkerThread();
return 0;
}