Skip to content

Commit d2706ea

Browse files
Mikolaj Maleckimaxsharabayko
authored andcommitted
[apps] Added setopt utility for easily setting socket options
1 parent e837a93 commit d2706ea

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

apps/apputil.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "netinet_any.h"
2222
#include "utilities.h"
23+
#include "srt.h"
2324

2425
#if _WIN32
2526

@@ -336,4 +337,60 @@ std::string OptionHelpItem(const OptionName& o);
336337
const char* SRTClockTypeStr();
337338
void PrintLibVersion();
338339

340+
341+
namespace srt
342+
{
343+
344+
struct OptionSetterProxy
345+
{
346+
SRTSOCKET s;
347+
int result = -1;
348+
349+
OptionSetterProxy(SRTSOCKET ss): s(ss) {}
350+
351+
struct OptionProxy
352+
{
353+
OptionSetterProxy& parent;
354+
SRT_SOCKOPT opt;
355+
356+
#define SPEC(type) \
357+
OptionProxy& operator=(const type& val)\
358+
{\
359+
parent.result = srt_setsockflag(parent.s, opt, &val, sizeof val);\
360+
return *this;\
361+
}
362+
363+
SPEC(int32_t);
364+
SPEC(int64_t);
365+
SPEC(bool);
366+
#undef SPEC
367+
368+
template<size_t N>
369+
OptionProxy& operator=(const char (&val)[N])
370+
{
371+
parent.result = srt_setsockflag(parent.s, opt, val, N-1);
372+
return *this;
373+
}
374+
375+
OptionProxy& operator=(const std::string& val)
376+
{
377+
parent.result = srt_setsockflag(parent.s, opt, val.c_str(), val.size());
378+
return *this;
379+
}
380+
};
381+
382+
OptionProxy operator[](SRT_SOCKOPT opt)
383+
{
384+
return OptionProxy {*this, opt};
385+
}
386+
387+
operator int() { return result; }
388+
};
389+
390+
inline OptionSetterProxy setopt(SRTSOCKET socket)
391+
{
392+
return OptionSetterProxy(socket);
393+
}
394+
395+
}
339396
#endif // INC_SRT_APPCOMMON_H

testing/srt-test-mpbond.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ int main( int argc, char** argv )
189189

190190
SRTSOCKET s = srt_create_socket();
191191

192-
//SRT_GROUPCONNTYPE gcon = SRTGC_GROUPONLY;
193-
int gcon = 1;
194-
srt_setsockflag(s, SRTO_GROUPCONNECT, &gcon, sizeof gcon);
195-
192+
srt::setopt(s)[SRTO_GROUPCONNECT] = 1;
196193
srt_bind(s, sa.get(), sizeof sa);
197194
srt_listen(s, 5);
198195

0 commit comments

Comments
 (0)