Skip to content

Commit 4b698c7

Browse files
author
Per Moberg
committed
STAR-1275: Read the peers SRT version and the negotiated latency from the socket
Pass a reference to a ConnectionInformation struct back to user when a new client connects and the called connects to a server.
1 parent c7d91e9 commit 4b698c7

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

SRTNet.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ SRTNet::ClientConnectStatus SRTNet::clientConnectToServer() {
260260
if (result != SRT_ERROR) {
261261
mClientConnected = true;
262262
if (connectedToServer) {
263-
connectedToServer(mConnectionContext, mContext);
263+
ConnectionInformation connectionInformation = getConnectionInformation(mContext);
264+
connectedToServer(mConnectionContext, mContext, connectionInformation);
264265
}
265266
// Break for-loop on first successful connect call
266267
break;
@@ -315,7 +316,9 @@ bool SRTNet::waitForSRTClient(bool singleClient) {
315316
}
316317

317318
SRT_LOGGER(true, LOGG_NOTIFY, "Client connected: " << newSocketCandidate);
318-
auto ctx = clientConnected(*reinterpret_cast<sockaddr*>(&theirAddr), newSocketCandidate, mConnectionContext);
319+
320+
ConnectionInformation connectionInformation = getConnectionInformation(newSocketCandidate);
321+
auto ctx = clientConnected(*reinterpret_cast<sockaddr*>(&theirAddr), newSocketCandidate, mConnectionContext, connectionInformation);
319322

320323
if (!ctx) {
321324
// No ctx in return from clientConnected callback means client was rejected by user.
@@ -948,3 +951,19 @@ uint16_t SRTNet::getLocallyBoundPort() const {
948951

949952
return 0;
950953
}
954+
955+
SRTNet::ConnectionInformation SRTNet::getConnectionInformation(SRTSOCKET socket) {
956+
uint8_t clientSrtVersion[4];
957+
int clientSrtVersionSize = sizeof(clientSrtVersion);
958+
srt_getsockflag(socket, SRTO_PEERVERSION, &clientSrtVersion, &clientSrtVersionSize);
959+
960+
int32_t negotiatedLatency = 0;
961+
int negotiatedLatencySize = sizeof(negotiatedLatency);
962+
srt_getsockflag(socket, SRTO_PEERLATENCY, &negotiatedLatency, &negotiatedLatencySize);
963+
964+
ConnectionInformation connectionInformation;
965+
connectionInformation.mPeerSrtVersion = std::string(std::to_string((int32_t)clientSrtVersion[2]) + "." + std::to_string((int32_t)clientSrtVersion[1]) + "." + std::to_string((int32_t)clientSrtVersion[0]));
966+
connectionInformation.mNegotiatedLatency = negotiatedLatency;
967+
968+
return std::move(connectionInformation);
969+
}

SRTNet.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class SRTNet {
6060
std::any mObject;
6161
};
6262

63+
struct ConnectionInformation {
64+
std::string mPeerSrtVersion;
65+
int32_t mNegotiatedLatency;
66+
};
67+
6368
/**
6469
*
6570
* @brief Constructor that can set a log prefix which will be added to the start of all log messages from this
@@ -288,7 +293,8 @@ class SRTNet {
288293
/// Callback handling connecting clients (only server mode)
289294
std::function<std::shared_ptr<NetworkConnection>(struct sockaddr& sin,
290295
SRTSOCKET newSocket,
291-
std::shared_ptr<NetworkConnection>& ctx)>
296+
std::shared_ptr<NetworkConnection>& ctx,
297+
const ConnectionInformation& connectionInformation)>
292298
clientConnected = nullptr;
293299

294300
/// Callback receiving data type vector
@@ -310,7 +316,7 @@ class SRTNet {
310316
std::function<void(std::shared_ptr<NetworkConnection>& ctx, SRTSOCKET lSocket)> clientDisconnected = nullptr;
311317

312318
/// Callback called whenever the client gets connected to the server (client mode only)
313-
std::function<void(std::shared_ptr<NetworkConnection>& ctx, SRTSOCKET lSocket)> connectedToServer = nullptr;
319+
std::function<void(std::shared_ptr<NetworkConnection>& ctx, SRTSOCKET lSocket, const ConnectionInformation& connectionInformation)> connectedToServer = nullptr;
314320

315321
// delete copy and move constructors and assign operators
316322
SRTNet(SRTNet const&) = delete; // Copy construct
@@ -443,4 +449,5 @@ class SRTNet {
443449

444450
const std::chrono::milliseconds kConnectionTimeout{1000};
445451
const int64_t kEpollTimeoutMs{500};
452+
static ConnectionInformation getConnectionInformation(SRTSOCKET socket);
446453
};

test/TestSrt.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class TestSRTFixture : public ::testing::Test {
7979

8080
// notice when client connects to server
8181
mServer.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket,
82-
std::shared_ptr<SRTNet::NetworkConnection>& ctx) {
82+
std::shared_ptr<SRTNet::NetworkConnection>& ctx,
83+
const SRTNet::ConnectionInformation&) {
8384
{
8485
std::lock_guard<std::mutex> lock(mConnectedMutex);
8586
mConnected = true;
@@ -150,7 +151,8 @@ TEST(TestSrt, StartStop) {
150151

151152
// notice when client connects to server
152153
server.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket,
153-
std::shared_ptr<SRTNet::NetworkConnection>& ctx) {
154+
std::shared_ptr<SRTNet::NetworkConnection>& ctx,
155+
const SRTNet::ConnectionInformation&) {
154156
{
155157
std::lock_guard<std::mutex> lock(connectedMutex);
156158
connected = true;
@@ -239,7 +241,8 @@ TEST(TestSrt, TestPsk) {
239241

240242
auto ctx = std::make_shared<SRTNet::NetworkConnection>();
241243
server.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket,
242-
std::shared_ptr<SRTNet::NetworkConnection>& ctx) { return ctx; };
244+
std::shared_ptr<SRTNet::NetworkConnection>& ctx,
245+
const SRTNet::ConnectionInformation&) { return ctx; };
243246
ASSERT_TRUE(server.startServer("127.0.0.1", 8009, 16, 1000, 100, SRT_LIVE_MAX_PLSIZE, 5000, kValidPsk, false, ctx));
244247
EXPECT_FALSE(client.startClient("127.0.0.1", 8009, 16, 1000, 100, ctx, SRT_LIVE_MAX_PLSIZE, false, 5000, kInvalidPsk))
245248
<< "Expect to fail when using incorrect PSK";

0 commit comments

Comments
 (0)