-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathudp_multicast_client.cpp
146 lines (116 loc) · 3.91 KB
/
udp_multicast_client.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
/*!
\file udp_multicast_client.cpp
\brief UDP multicast client example
\author Ivan Shynkarenka
\date 27.12.2016
\copyright MIT License
*/
#include "asio_service.h"
#include "server/asio/udp_client.h"
#include "threads/thread.h"
#include <atomic>
#include <iostream>
class MulticastClient : public CppServer::Asio::UDPClient
{
public:
MulticastClient(const std::shared_ptr<CppServer::Asio::Service>& service, const std::string& address, const std::string& multicast, int port)
: CppServer::Asio::UDPClient(service, address, port),
_multicast(multicast)
{
}
void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected())
CppCommon::Thread::Yield();
}
protected:
void onConnected() override
{
std::cout << "Multicast UDP client connected a new session with Id " << id() << std::endl;
// Join UDP multicast group
JoinMulticastGroup(_multicast);
// Start receive datagrams
ReceiveAsync();
}
void onDisconnected() override
{
std::cout << "Multicast UDP client disconnected a session with Id " << id() << std::endl;
// Wait for a while...
CppCommon::Thread::Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) override
{
std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
// Continue receive datagrams
ReceiveAsync();
}
void onError(int error, const std::string& category, const std::string& message) override
{
std::cout << "Multicast UDP client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
private:
std::atomic<bool> _stop{false};
std::string _multicast;
};
int main(int argc, char** argv)
{
// UDP listen address
std::string listen_address = "0.0.0.0";
if (argc > 1)
listen_address = argv[1];
// UDP multicast address
std::string multicast_address = "239.255.0.1";
if (argc > 2)
multicast_address = argv[2];
// UDP multicast port
int multicast_port = 3334;
if (argc > 3)
multicast_port = std::atoi(argv[3]);
std::cout << "UDP listen address: " << listen_address << std::endl;
std::cout << "UDP multicast address: " << multicast_address << std::endl;
std::cout << "UDP multicast port: " << multicast_port << std::endl;
std::cout << std::endl;
// Create a new Asio service
auto service = std::make_shared<AsioService>();
// Start the Asio service
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
// Create a new UDP multicast client
auto client = std::make_shared<MulticastClient>(service, listen_address, multicast_address, multicast_port);
client->SetupMulticast(true);
// Connect the client
std::cout << "Client connecting...";
client->ConnectAsync();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the client or '!' to reconnect the client..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
// Reconnect the client
if (line == "!")
{
std::cout << "Client reconnecting...";
client->IsConnected() ? client->ReconnectAsync() : client->ConnectAsync();
std::cout << "Done!" << std::endl;
continue;
}
}
// Disconnect the client
std::cout << "Client disconnecting...";
client->DisconnectAndStop();
std::cout << "Done!" << std::endl;
// Stop the Asio service
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}