-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathudp_multicast_server.cpp
143 lines (116 loc) · 4.39 KB
/
udp_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
//
// Created by Ivan Shynkarenka on 29.03.2018
//
#include "server/asio/service.h"
#include "server/asio/udp_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 MulticastServer : public UDPServer
{
public:
using UDPServer::UDPServer;
protected:
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "UDP 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("-a", "--address").dest("address").set_default("239.255.0.1").help("Server address. Default: %default");
parser.add_option("-p", "--port").dest("port").action("store").type("int").set_default(3333).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
std::string address(options.get("address"));
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 address: " << address << std::endl;
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, 0);
server->SetupReuseAddress(true);
server->SetupReusePort(true);
// Start the server
std::cout << "Server starting...";
server->Start(address, port);
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;
}