Skip to content

Commit 87bcdd1

Browse files
author
Per Moberg
committed
STAR-1269: Refactor getActiveClients to return a copy of mClientList
Also added a new function to get a vector of just the sockets for all the active clients.
1 parent 9f8e22e commit 87bcdd1

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

SRTNet.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,22 @@ bool SRTNet::waitForSRTClient(bool singleClient) {
347347
return false;
348348
}
349349

350-
void SRTNet::getActiveClients(
351-
const std::function<void(std::map<SRTSOCKET, std::shared_ptr<NetworkConnection>>&)>& function) {
350+
std::vector<std::pair<SRTSOCKET, std::shared_ptr<SRTNet::NetworkConnection>>> SRTNet::getActiveClients() const {
352351
std::lock_guard<std::mutex> lock(mClientListMtx);
353-
function(mClientList);
352+
353+
std::vector<std::pair<SRTSOCKET, std::shared_ptr<NetworkConnection>>> clients;
354+
std::copy(mClientList.begin(), mClientList.end(), std::back_inserter(clients));
355+
return clients;
356+
}
357+
358+
std::vector<SRTSOCKET> SRTNet::getActiveClientSockets() const {
359+
std::lock_guard<std::mutex> lock(mClientListMtx);
360+
361+
std::vector<SRTSOCKET> clientSockets(mClientList.size());
362+
for (const auto& [socket, networkConnection] : mClientList) {
363+
clientSockets.push_back(socket);
364+
}
365+
return clientSockets;
354366
}
355367

356368
bool SRTNet::startClient(const std::string& host,

SRTNet.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,19 @@ class SRTNet {
205205

206206
/**
207207
*
208-
* Get active clients (A server method)
208+
* @brief Get all active clients (A server method)
209+
* @return A vector of pairs containing the SRTSocketHandle (SRTSOCKET) and it's associated NetworkConnection you provided.
209210
*
210-
* @param function. pass a function getting the map containing the list of active connections
211-
* Where the map contains the SRTSocketHandle (SRTSOCKET) and it's associated NetworkConnection you provided.
212211
*/
213-
void
214-
getActiveClients(const std::function<void(std::map<SRTSOCKET, std::shared_ptr<NetworkConnection>>&)>& function);
212+
std::vector<std::pair<SRTSOCKET, std::shared_ptr<NetworkConnection>>> getActiveClients() const;
213+
214+
/**
215+
*
216+
* @brief Get the socket of all active clients (A server method)
217+
* @return A vector containing the SRTSocketHandle (SRTSOCKET) of all active clients.
218+
*
219+
*/
220+
std::vector<SRTSOCKET> getActiveClientSockets() const;
215221

216222
/**
217223
*
@@ -428,7 +434,7 @@ class SRTNet {
428434
mutable std::mutex mNetMtx;
429435
Mode mCurrentMode = Mode::unknown;
430436
std::map<SRTSOCKET, std::shared_ptr<NetworkConnection>> mClientList = {};
431-
std::mutex mClientListMtx;
437+
mutable std::mutex mClientListMtx;
432438
std::shared_ptr<NetworkConnection> mClientContext = nullptr;
433439
std::shared_ptr<NetworkConnection> mConnectionContext = nullptr;
434440
std::atomic<bool> mClientConnected = false;

main.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,8 @@ int main(int argc, const char *argv[]) {
217217
// std::cout << "The server got " << clients->mClientList->size() << " clients." << std::endl;
218218
// clients = nullptr;
219219

220-
gSRTNetServer.getActiveClients([](std::map<SRTSOCKET, std::shared_ptr<SRTNet::NetworkConnection>> &clientList) {
221-
std::cout << "The server got " << clientList.size() << " client(s)." << std::endl;
222-
}
223-
);
220+
auto clientList = gSRTNetServer.getActiveClientSockets();
221+
std::cout << "The server got " << clientList.size() << " client(s)." << std::endl;
224222

225223
//Send 300 packets with 10 milliseconds spacing. Packets are 1000 bytes long
226224
int lTimes = 0;
@@ -272,10 +270,9 @@ int main(int argc, const char *argv[]) {
272270
//SRT_TRACEBSTATS lCurrentServerStats = {0};
273271
//mySRTNetServer.getStatistics(&currentServerStats,SRTNetClearStats::yes,SRTNetInstant::no);
274272

275-
gSRTNetServer.getActiveClients([](std::map<SRTSOCKET, std::shared_ptr<SRTNet::NetworkConnection>> &rClientList) {
276-
std::cout << "The server got " << rClientList.size() << " clients." << std::endl;
277-
}
278-
);
273+
clientList = gSRTNetServer.getActiveClientSockets();
274+
std::cout << "The server got " << clientList.size() << " client(s)." << std::endl;
275+
279276

280277
std::cout << "SRT garbage collect" << std::endl;
281278
gSRTNetServer.stop();
@@ -285,10 +282,8 @@ int main(int argc, const char *argv[]) {
285282
std::cout << "stopClient 2" << std::endl;
286283
gSRTNetClient2.stop();
287284

288-
gSRTNetServer.getActiveClients([](std::map<SRTSOCKET, std::shared_ptr<SRTNet::NetworkConnection>> &clientList) {
289-
std::cout << "The server got " << clientList.size() << " clients." << std::endl;
290-
}
291-
);
285+
clientList = gSRTNetServer.getActiveClientSockets();
286+
std::cout << "The server got " << clientList.size() << " client(s)." << std::endl;
292287

293288
srt_cleanup();
294289
std::cout << "SRT wrapper did end." << std::endl;

0 commit comments

Comments
 (0)