-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathtcp_multicast_server.cpp
172 lines (140 loc) · 5.09 KB
/
tcp_multicast_server.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
163
164
165
166
167
168
169
170
171
172
//
// Created by Ivan Shynkarenka on 29.03.2018
//
#include "server/asio/service.h"
#include "server/asio/tcp_server.h"
#include "system/cpu.h"
#include "threads/thread.h"
#include "time/timestamp.h"
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
#include <OptionParser.h>
using namespace CppCommon;
using namespace CppServer::Asio;
class MulticastSession : public TCPSession
{
public:
using TCPSession::TCPSession;
bool SendAsync(const void* buffer, size_t size) override
{
// Limit session send buffer to 1 megabyte
const size_t limit = 1 * 1024 * 1024;
size_t pending = bytes_pending();
if ((pending + size) > limit)
return false;
if (size > (limit - pending))
size = limit - pending;
return TCPSession::SendAsync(buffer, size);
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "TCP session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
class MulticastServer : public TCPServer
{
public:
using TCPServer::TCPServer;
protected:
std::shared_ptr<TCPSession> CreateSession(const std::shared_ptr<TCPServer>& server) override
{
return std::make_shared<MulticastSession>(server);
}
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "TCP server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
auto parser = optparse::OptionParser().version("1.0.0.0");
parser.add_option("-p", "--port").dest("port").action("store").type("int").set_default(1111).help("Server port. Default: %default");
parser.add_option("-t", "--threads").dest("threads").action("store").type("int").set_default(CPU::PhysicalCores()).help("Count of working threads. Default: %default");
parser.add_option("-m", "--messages").dest("messages").action("store").type("int").set_default(1000000).help("Rate of messages per second to send. Default: %default");
parser.add_option("-s", "--size").dest("size").action("store").type("int").set_default(32).help("Single message size. Default: %default");
optparse::Values options = parser.parse_args(argc, argv);
// Print help
if (options.get("help"))
{
parser.print_help();
return 0;
}
// Server port
int port = options.get("port");
int threads = options.get("threads");
int messages_rate = options.get("messages");
int message_size = options.get("size");
std::cout << "Server port: " << port << std::endl;
std::cout << "Working threads: " << threads << std::endl;
std::cout << "Messages rate: " << messages_rate << std::endl;
std::cout << "Message size: " << message_size << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<Service>(threads);
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new multicast server
auto server = std::make_shared<MulticastServer>(service, port);
// server->SetupNoDelay(true);
server->SetupReuseAddress(true);
server->SetupReusePort(true);
// Start the server
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
// Start the multicasting thread
std::atomic<bool> multicasting(true);
auto multicaster = std::thread([&server, &multicasting, messages_rate, message_size]()
{
// Prepare message to multicast
std::vector<uint8_t> message_to_send(message_size);
// Multicasting loop
while (multicasting)
{
auto start = UtcTimestamp();
for (int i = 0; i < messages_rate; ++i)
server->Multicast(message_to_send.data(), message_to_send.size());
auto end = UtcTimestamp();
// Sleep for remaining time or yield
auto milliseconds = (end - start).milliseconds();
if (milliseconds < 1000)
Thread::Sleep(1000 - milliseconds);
else
Thread::Yield();
}
});
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Restart the server
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
}
// Stop the multicasting thread
multicasting = false;
multicaster.join();
// Stop the server
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}