Skip to content

Commit

Permalink
[apps] Test apps improvements + build break fixes with logging off (#…
Browse files Browse the repository at this point in the history
…1638)

- touches the core around exposing some logging staff to apps if built with logging disabled;
- fixes building of testing apps;
- fixes a bug in Options Parser;
- adds the new 'retry' command-line option to srt-test-live.
  • Loading branch information
ethouris committed Nov 6, 2020
1 parent e567939 commit 172d91f
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 36 deletions.
12 changes: 11 additions & 1 deletion apps/apputil.cpp
Expand Up @@ -199,7 +199,17 @@ options_t ProcessOptions(char* const* argv, int argc, std::vector<OptionScheme>
{
const char* a = *p;
// cout << "*D ARG: '" << a << "'\n";
if (moreoptions && a[0] == '-')
bool isoption = false;
if (a[0] == '-')
{
isoption = true;
// If a[0] isn't NUL - because it is dash - then
// we can safely check a[1].
if (a[1] && isdigit(a[1]))
isoption = false;
}

if (moreoptions && isoption)
{
bool arg_specified = false;
size_t seppos; // (see goto, it would jump over initialization)
Expand Down
11 changes: 7 additions & 4 deletions srtcore/common.cpp
Expand Up @@ -661,12 +661,11 @@ uint64_t PacketMetric::fullBytes()
}


// Some logging imps
#if ENABLE_LOGGING

namespace srt_logging
{

// Value display utilities
// (also useful for applications)

std::string SockStatusStr(SRT_SOCKSTATUS s)
{
Expand Down Expand Up @@ -722,6 +721,10 @@ std::string MemberStatusStr(SRT_MEMBERSTATUS s)
}
#endif

// Logging system implementation

#if ENABLE_LOGGING

LogDispatcher::Proxy::Proxy(LogDispatcher& guy) : that(guy), that_enabled(that.CheckEnabled())
{
if (that_enabled)
Expand Down Expand Up @@ -834,7 +837,7 @@ std::string LogDispatcher::Proxy::ExtractName(std::string pretty_function)

return pretty_function.substr(pos+2);
}
#endif

} // (end namespace srt_logging)

#endif
6 changes: 5 additions & 1 deletion srtcore/crypto.cpp
Expand Up @@ -44,7 +44,8 @@ using namespace srt_logging;
// 10* HAICRYPT_DEF_KM_PRE_ANNOUNCE
const int SRT_CRYPT_KM_PRE_ANNOUNCE = 0x10000;

#if ENABLE_LOGGING
namespace srt_logging
{
std::string KmStateStr(SRT_KM_STATE state)
{
switch (state)
Expand All @@ -64,8 +65,11 @@ std::string KmStateStr(SRT_KM_STATE state)
}
}
}
} // namespace

using srt_logging::KmStateStr;

#if ENABLE_LOGGING
std::string CCryptoControl::FormatKmMessage(std::string hdr, int cmd, size_t srtlen)
{
std::ostringstream os;
Expand Down
6 changes: 3 additions & 3 deletions srtcore/crypto.h
Expand Up @@ -28,16 +28,16 @@ written by
#include <haicrypt.h>
#include <hcrypt_msg.h>

#if ENABLE_LOGGING

std::string KmStateStr(SRT_KM_STATE state);

namespace srt_logging
{
std::string KmStateStr(SRT_KM_STATE state);
#if ENABLE_LOGGING
extern Logger cnlog;
#endif
}

#endif

// For KMREQ/KMRSP. Only one field is used.
const size_t SRT_KMR_KMSTATE = 0;
Expand Down
12 changes: 12 additions & 0 deletions testing/srt-test-live.cpp
Expand Up @@ -451,6 +451,7 @@ int main( int argc, char** argv )
o_group ((optargs), "<URIs...> Using multiple SRT connections as redundancy group", "g"),
#endif
o_stime ((optargs), " Pass source time explicitly to SRT output", "st", "srctime", "sourcetime"),
o_retry ((optargs), "<N=-1,0,+N> Retry connection N times if failed on timeout", "rc", "retry"),
o_help ((optargs), "[special=logging] This help", "?", "help", "-help")
;

Expand Down Expand Up @@ -761,6 +762,17 @@ int main( int argc, char** argv )
}
}

string retryphrase = Option<OutString>(params, "", o_retry);
if (retryphrase != "")
{
if (retryphrase[retryphrase.size()-1] == 'a')
{
transmit_retry_always = true;
retryphrase = retryphrase.substr(0, retryphrase.size()-1);
}

transmit_retry_connect = stoi(retryphrase);
}

#ifdef _WIN32
#define alarm(argument) (void)0
Expand Down
1 change: 1 addition & 0 deletions testing/srt-test-mpbond.cpp
Expand Up @@ -40,6 +40,7 @@
#define signal_alarm(fn) signal(SIGALRM, fn)
#endif

srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-mpbond");

volatile bool mpbond_int_state = false;
void OnINT_SetIntState(int)
Expand Down
122 changes: 95 additions & 27 deletions testing/testmedia.cpp
Expand Up @@ -18,6 +18,8 @@
#include <stdexcept>
#include <iterator>
#include <map>
#include <chrono>
#include <thread>
#include <srt.h>
#if !defined(_WIN32)
#include <sys/ioctl.h>
Expand All @@ -40,6 +42,7 @@

using namespace std;

using srt_logging::KmStateStr;
using srt_logging::SockStatusStr;
#if ENABLE_EXPERIMENTAL_BONDING
using srt_logging::MemberStatusStr;
Expand All @@ -53,6 +56,8 @@ bool transmit_printformat_json = false;
srt_listen_callback_fn* transmit_accept_hook_fn = nullptr;
void* transmit_accept_hook_op = nullptr;
bool transmit_use_sourcetime = false;
int transmit_retry_connect = 0;
bool transmit_retry_always = false;

// Do not unblock. Copy this to an app that uses applog and set appropriate name.
//srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-test");
Expand Down Expand Up @@ -140,7 +145,6 @@ class FileTarget: public virtual Target
{
ofile.write(data.payload.data(), data.payload.size());
#ifdef PLEASE_LOG
extern srt_logging::Logger applog;
applog.Debug() << "FileTarget::Write: " << data.size() << " written to a file";
#endif
}
Expand All @@ -151,7 +155,6 @@ class FileTarget: public virtual Target
void Close() override
{
#ifdef PLEASE_LOG
extern srt_logging::Logger applog;
applog.Debug() << "FileTarget::Close";
#endif
ofile.close();
Expand Down Expand Up @@ -696,9 +699,6 @@ void SrtCommon::Init(string host, int port, string path, map<string,string> par,
srt_getsockflag(m_sock, SRTO_SNDKMSTATE, &snd_kmstate, &len);
srt_getsockflag(m_sock, SRTO_RCVKMSTATE, &rcv_kmstate, &len);

// Bring this declaration temporarily, this is only for testing
std::string KmStateStr(SRT_KM_STATE state);

Verb() << "ENCRYPTION status: " << KmStateStr(kmstate)
<< " (SND:" << KmStateStr(snd_kmstate) << " RCV:" << KmStateStr(rcv_kmstate)
<< ") PBKEYLEN=" << pbkeylen;
Expand Down Expand Up @@ -899,6 +899,10 @@ void TransmitGroupSocketConnect(void* srtcommon, SRTSOCKET sock, int error, cons
return; // nothing to do for a successful socket
}

#ifdef PLEASE_LOG
applog.Debug("connect callback: error on @", sock, " erc=", error, " token=", token);
#endif

/* Example: identify by target address
sockaddr_any peersa = peer;
sockaddr_any agentsa;
Expand Down Expand Up @@ -1024,26 +1028,48 @@ void SrtCommon::OpenGroupClient()
targets.push_back(gd);
}

Verb() << "Waiting for group connection... " << VerbNoEOL;

int fisock = srt_connect_group(m_sock, targets.data(), targets.size());

if (fisock == SRT_ERROR)
::transmit_throw_on_interrupt = true;
for (;;) // REPEATABLE BLOCK
{
// Complete the error information for every member
Connect_Again:
Verb() << "Waiting for group connection... " << VerbNoEOL;

int fisock = srt_connect_group(m_sock, targets.data(), targets.size());

ostringstream out;
for (Connection& c: m_group_nodes)
if (fisock == SRT_ERROR)
{
if (c.error != SRT_SUCCESS)
// Complete the error information for every member
ostringstream out;
set<int> reasons;
for (Connection& c: m_group_nodes)
{
if (c.error != SRT_SUCCESS)
{
out << "[" << c.token << "] " << c.host << ":" << c.port;
if (!c.source.empty())
out << "[[" << c.source.str() << "]]";
out << ": " << srt_strerror(c.error, 0) << ": " << srt_rejectreason_str(c.reason) << endl;
}
reasons.insert(c.reason);
}

if (transmit_retry_connect && (transmit_retry_always || (reasons.size() == 1 && *reasons.begin() == SRT_REJ_TIMEOUT)))
{
out << "[" << c.token << "] " << c.host << ":" << c.port;
if (!c.source.empty())
out << "[[" << c.source.str() << "]]";
out << ": " << srt_strerror(c.error, 0) << ": " << srt_rejectreason_str(c.reason) << endl;
if (transmit_retry_connect != -1)
--transmit_retry_connect;

Verb() << "...all links timeout, retrying (" << transmit_retry_connect << ")...";
continue;
}

Error("srt_connect_group, nodes:\n" + out.str());
}
Error("srt_connect_group, nodes:\n" + out.str());
else
{
Verb() << "[ASYNC] will wait..." << VerbNoEOL;
}

break;
}

if (m_blocking_mode)
Expand Down Expand Up @@ -1127,7 +1153,33 @@ void SrtCommon::OpenGroupClient()
if (find(ready_err, ready_err+len2, m_sock) != ready_err+len2)
{
Verb() << "[EPOLL: " << len2 << " entities FAILED]";
Error("All group connections failed", SRT_REJ_UNKNOWN, SRT_ENOCONN);
// Complete the error information for every member
ostringstream out;
set<int> reasons;
for (Connection& c: m_group_nodes)
{
if (c.error != SRT_SUCCESS)
{
out << "[" << c.token << "] " << c.host << ":" << c.port;
if (!c.source.empty())
out << "[[" << c.source.str() << "]]";
out << ": " << srt_strerror(c.error, 0) << ": " << srt_rejectreason_str(c.reason) << endl;
}
reasons.insert(c.reason);
}

if (transmit_retry_connect && (transmit_retry_always || (reasons.size() == 1 && *reasons.begin() == SRT_REJ_TIMEOUT)))
{
if (transmit_retry_connect != -1)
--transmit_retry_connect;


Verb() << "...all links timeout, retrying in 250ms (" << transmit_retry_connect << ")...";
std::this_thread::sleep_for(std::chrono::milliseconds(250));
goto Connect_Again;
}

Error("srt_connect_group, nodes:\n" + out.str());
}
else if (find(ready_conn, ready_conn+len1, m_sock) != ready_conn+len1)
{
Expand All @@ -1153,6 +1205,7 @@ void SrtCommon::OpenGroupClient()
Error("ConfigurePost");
}

::transmit_throw_on_interrupt = false;

Verb() << "Group connection report:";
for (auto& d: m_group_data)
Expand Down Expand Up @@ -1220,16 +1273,31 @@ void SrtCommon::ConnectClient(string host, int port)
srt_connect_callback(m_sock, &TransmitConnectCallback, 0);
}

int stat = srt_connect(m_sock, sa.get(), sizeof sa);
if (stat == SRT_ERROR)
int stat = -1;
for (;;)
{
int reason = srt_getrejectreason(m_sock);
::transmit_throw_on_interrupt = true;
stat = srt_connect(m_sock, sa.get(), sizeof sa);
::transmit_throw_on_interrupt = false;
if (stat == SRT_ERROR)
{
int reason = srt_getrejectreason(m_sock);
#if PLEASE_LOG
extern srt_logging::Logger applog;
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock);
LOGP(applog.Error, "ERROR reported by srt_connect - closing socket @", m_sock);
#endif
srt_close(m_sock);
Error("srt_connect", reason);
if (transmit_retry_connect && (transmit_retry_always || reason == SRT_REJ_TIMEOUT))
{
if (transmit_retry_connect != -1)
--transmit_retry_connect;

Verb() << "...timeout, retrying (" << transmit_retry_connect << ")...";
continue;
}

srt_close(m_sock);
Error("srt_connect", reason);
}
break;
}

// Wait for REAL connected state if nonblocking mode
Expand Down
2 changes: 2 additions & 0 deletions testing/testmediabase.hpp
Expand Up @@ -24,6 +24,8 @@ extern unsigned transmit_stats_report;
extern size_t transmit_chunk_size;
extern bool transmit_printformat_json;
extern bool transmit_use_sourcetime;
extern int transmit_retry_connect;
extern bool transmit_retry_always;

struct MediaPacket
{
Expand Down

0 comments on commit 172d91f

Please sign in to comment.