diff --git a/SRTNet.cpp b/SRTNet.cpp index 830db56..26c8c18 100644 --- a/SRTNet.cpp +++ b/SRTNet.cpp @@ -260,7 +260,8 @@ SRTNet::ClientConnectStatus SRTNet::clientConnectToServer() { if (result != SRT_ERROR) { mClientConnected = true; if (connectedToServer) { - connectedToServer(mConnectionContext, mContext); + ConnectionInformation connectionInformation = getConnectionInformation(mContext); + connectedToServer(mConnectionContext, mContext, connectionInformation); } // Break for-loop on first successful connect call break; @@ -315,7 +316,9 @@ bool SRTNet::waitForSRTClient(bool singleClient) { } SRT_LOGGER(true, LOGG_NOTIFY, "Client connected: " << newSocketCandidate); - auto ctx = clientConnected(*reinterpret_cast(&theirAddr), newSocketCandidate, mConnectionContext); + + ConnectionInformation connectionInformation = getConnectionInformation(newSocketCandidate); + auto ctx = clientConnected(*reinterpret_cast(&theirAddr), newSocketCandidate, mConnectionContext, connectionInformation); if (!ctx) { // No ctx in return from clientConnected callback means client was rejected by user. @@ -948,3 +951,19 @@ uint16_t SRTNet::getLocallyBoundPort() const { return 0; } + +SRTNet::ConnectionInformation SRTNet::getConnectionInformation(SRTSOCKET socket) { + uint8_t clientSrtVersion[4]; + int clientSrtVersionSize = sizeof(clientSrtVersion); + srt_getsockflag(socket, SRTO_PEERVERSION, &clientSrtVersion, &clientSrtVersionSize); + + int32_t negotiatedLatency = 0; + int negotiatedLatencySize = sizeof(negotiatedLatency); + srt_getsockflag(socket, SRTO_PEERLATENCY, &negotiatedLatency, &negotiatedLatencySize); + + ConnectionInformation connectionInformation; + 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])); + connectionInformation.mNegotiatedLatency = negotiatedLatency; + + return std::move(connectionInformation); +} diff --git a/SRTNet.h b/SRTNet.h index 5f0c55d..2f63211 100644 --- a/SRTNet.h +++ b/SRTNet.h @@ -60,6 +60,11 @@ class SRTNet { std::any mObject; }; + struct ConnectionInformation { + std::string mPeerSrtVersion; + int32_t mNegotiatedLatency; + }; + /** * * @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 { /// Callback handling connecting clients (only server mode) std::function(struct sockaddr& sin, SRTSOCKET newSocket, - std::shared_ptr& ctx)> + std::shared_ptr& ctx, + const ConnectionInformation& connectionInformation)> clientConnected = nullptr; /// Callback receiving data type vector @@ -310,7 +316,7 @@ class SRTNet { std::function& ctx, SRTSOCKET lSocket)> clientDisconnected = nullptr; /// Callback called whenever the client gets connected to the server (client mode only) - std::function& ctx, SRTSOCKET lSocket)> connectedToServer = nullptr; + std::function& ctx, SRTSOCKET lSocket, const ConnectionInformation& connectionInformation)> connectedToServer = nullptr; // delete copy and move constructors and assign operators SRTNet(SRTNet const&) = delete; // Copy construct @@ -443,4 +449,5 @@ class SRTNet { const std::chrono::milliseconds kConnectionTimeout{1000}; const int64_t kEpollTimeoutMs{500}; + static ConnectionInformation getConnectionInformation(SRTSOCKET socket); }; diff --git a/test/TestSrt.cpp b/test/TestSrt.cpp index 849e44d..6176bb5 100644 --- a/test/TestSrt.cpp +++ b/test/TestSrt.cpp @@ -79,7 +79,8 @@ class TestSRTFixture : public ::testing::Test { // notice when client connects to server mServer.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket, - std::shared_ptr& ctx) { + std::shared_ptr& ctx, + const SRTNet::ConnectionInformation&) { { std::lock_guard lock(mConnectedMutex); mConnected = true; @@ -150,7 +151,8 @@ TEST(TestSrt, StartStop) { // notice when client connects to server server.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket, - std::shared_ptr& ctx) { + std::shared_ptr& ctx, + const SRTNet::ConnectionInformation&) { { std::lock_guard lock(connectedMutex); connected = true; @@ -239,7 +241,8 @@ TEST(TestSrt, TestPsk) { auto ctx = std::make_shared(); server.clientConnected = [&](struct sockaddr& sin, SRTSOCKET newSocket, - std::shared_ptr& ctx) { return ctx; }; + std::shared_ptr& ctx, + const SRTNet::ConnectionInformation&) { return ctx; }; ASSERT_TRUE(server.startServer("127.0.0.1", 8009, 16, 1000, 100, SRT_LIVE_MAX_PLSIZE, 5000, kValidPsk, false, ctx)); EXPECT_FALSE(client.startClient("127.0.0.1", 8009, 16, 1000, 100, ctx, SRT_LIVE_MAX_PLSIZE, false, 5000, kInvalidPsk)) << "Expect to fail when using incorrect PSK";