forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgroup.cpp
4390 lines (3746 loc) · 164 KB
/
group.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iterator>
#include "api.h"
#include "group.h"
using namespace std;
using namespace srt::sync;
using namespace srt_logging;
// The SRT_DEF_VERSION is defined in core.cpp.
extern const int32_t SRT_DEF_VERSION;
int32_t CUDTGroup::s_tokenGen = 0;
// [[using locked(this->m_GroupLock)]];
bool CUDTGroup::getBufferTimeBase(CUDT* forthesakeof,
steady_clock::time_point& w_tb,
bool& w_wp,
steady_clock::duration& w_dr)
{
CUDT* master = 0;
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
CUDT* u = &gi->ps->core();
if (gi->laststatus != SRTS_CONNECTED)
{
HLOGC(gmlog.Debug,
log << "getBufferTimeBase: skipping @" << u->m_SocketID
<< ": not connected, state=" << SockStatusStr(gi->laststatus));
continue;
}
if (u == forthesakeof)
continue; // skip the member if it's the target itself
if (!u->m_pRcvBuffer)
continue; // Not initialized yet
master = u;
break; // found
}
// We don't have any sockets in the group, so can't get
// the buffer timebase. This should be then initialized
// the usual way.
if (!master)
return false;
w_wp = master->m_pRcvBuffer->getInternalTimeBase((w_tb), (w_dr));
// Sanity check
if (is_zero(w_tb))
{
LOGC(gmlog.Error, log << "IPE: existing previously socket has no time base set yet!");
return false; // this will enforce initializing the time base normal way
}
return true;
}
// [[using locked(this->m_GroupLock)]];
bool CUDTGroup::applyGroupSequences(SRTSOCKET target, int32_t& w_snd_isn, int32_t& w_rcv_isn)
{
if (m_bConnected) // You are the first one, no need to change.
{
IF_HEAVY_LOGGING(string update_reason = "what?");
// Find a socket that is declared connected and is not
// the socket that caused the call.
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
if (gi->id == target)
continue;
CUDT& se = gi->ps->core();
if (!se.m_bConnected)
continue;
// Found it. Get the following sequences:
// For sending, the sequence that is about to be sent next.
// For receiving, the sequence of the latest received packet.
// SndCurrSeqNo is initially set to ISN-1, this next one is
// the sequence that is about to be stamped on the next sent packet
// over that socket. Using this field is safer because it is volatile
// and its affinity is to the same thread as the sending function.
// NOTE: the groupwise scheduling sequence might have been set
// already. If so, it means that it was set by either:
// - the call of this function on the very first conencted socket (see below)
// - the call to `sendBroadcast` or `sendBackup`
// In both cases, we want THIS EXACTLY value to be reported
if (m_iLastSchedSeqNo != -1)
{
w_snd_isn = m_iLastSchedSeqNo;
IF_HEAVY_LOGGING(update_reason = "GROUPWISE snd-seq");
}
else
{
w_snd_isn = se.m_iSndNextSeqNo;
// Write it back to the groupwise scheduling sequence so that
// any next connected socket will take this value as well.
m_iLastSchedSeqNo = w_snd_isn;
IF_HEAVY_LOGGING(update_reason = "existing socket not yet sending");
}
// RcvCurrSeqNo is increased by one because it happens that at the
// synchronization moment it's already past reading and delivery.
// This is redundancy, so the redundant socket is connected at the moment
// when the other one is already transmitting, so skipping one packet
// even if later transmitted is less troublesome than requesting a
// "mistakenly seen as lost" packet.
w_rcv_isn = CSeqNo::incseq(se.m_iRcvCurrSeqNo);
HLOGC(gmlog.Debug,
log << "applyGroupSequences: @" << target << " gets seq from @" << gi->id << " rcv %" << (w_rcv_isn)
<< " snd %" << (w_rcv_isn) << " as " << update_reason);
return false;
}
}
// If the GROUP (!) is not connected, or no running/pending socket has been found.
// // That is, given socket is the first one.
// The group data should be set up with its own data. They should already be passed here
// in the variables.
//
// Override the schedule sequence of the group in this case because whatever is set now,
// it's not valid.
HLOGC(gmlog.Debug,
log << "applyGroupSequences: no socket found connected and transmitting, @" << target
<< " not changing sequences, storing snd-seq %" << (w_snd_isn));
set_currentSchedSequence(w_snd_isn);
return true;
}
// NOTE: This function is now for DEBUG PURPOSES ONLY.
// Except for presenting the extracted data in the logs, there's no use of it now.
void CUDTGroup::debugMasterData(SRTSOCKET slave)
{
// Find at least one connection, which is running. Note that this function is called
// from within a handshake process, so the socket that undergoes this process is at best
// currently in SRT_GST_PENDING state and it's going to be in SRT_GST_IDLE state at the
// time when the connection process is done, until the first reading/writing happens.
ScopedLock cg(m_GroupLock);
SRTSOCKET mpeer;
steady_clock::time_point start_time;
bool found = false;
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
if (gi->sndstate == SRT_GST_RUNNING)
{
// Found it. Get the socket's peer's ID and this socket's
// Start Time. Once it's delivered, this can be used to calculate
// the Master-to-Slave start time difference.
mpeer = gi->ps->m_PeerID;
start_time = gi->ps->core().socketStartTime();
HLOGC(gmlog.Debug,
log << "getMasterData: found RUNNING master @" << gi->id << " - reporting master's peer $" << mpeer
<< " starting at " << FormatTime(start_time));
found = true;
break;
}
}
if (!found)
{
// If no running one found, then take the first socket in any other
// state than broken, except the slave. This is for a case when a user
// has prepared one link already, but hasn't sent anything through it yet.
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
if (gi->sndstate == SRT_GST_BROKEN)
continue;
if (gi->id == slave)
continue;
// Found it. Get the socket's peer's ID and this socket's
// Start Time. Once it's delivered, this can be used to calculate
// the Master-to-Slave start time difference.
mpeer = gi->ps->core().m_PeerID;
start_time = gi->ps->core().socketStartTime();
HLOGC(gmlog.Debug,
log << "getMasterData: found IDLE/PENDING master @" << gi->id << " - reporting master's peer $" << mpeer
<< " starting at " << FormatTime(start_time));
found = true;
break;
}
}
if (!found)
{
LOGC(cnlog.Debug, log << CONID() << "NO GROUP MASTER LINK found for group: $" << id());
}
else
{
// The returned master_st is the master's start time. Calculate the
// differene time.
steady_clock::duration master_tdiff = m_tsStartTime - start_time;
LOGC(cnlog.Debug, log << CONID() << "FOUND GROUP MASTER LINK: peer=$" << mpeer
<< " - start time diff: " << FormatDuration<DUNIT_S>(master_tdiff));
}
}
// GROUP
std::list<CUDTGroup::SocketData> CUDTGroup::GroupContainer::s_NoList;
CUDTGroup::gli_t CUDTGroup::add(SocketData data)
{
ScopedLock g(m_GroupLock);
// Change the snd/rcv state of the group member to PENDING.
// Default for SocketData after creation is BROKEN, which just
// after releasing the m_GroupLock could be read and interpreted
// as broken connection and removed before the handshake process
// is done.
data.sndstate = SRT_GST_PENDING;
data.rcvstate = SRT_GST_PENDING;
m_Group.push_back(data);
gli_t end = m_Group.end();
if (m_iMaxPayloadSize == -1)
{
int plsize = data.ps->m_pUDT->OPT_PayloadSize();
HLOGC(gmlog.Debug,
log << "CUDTGroup::add: taking MAX payload size from socket @" << data.ps->m_SocketID << ": " << plsize
<< " " << (plsize ? "(explicit)" : "(unspecified = fallback to 1456)"));
if (plsize == 0)
plsize = SRT_LIVE_MAX_PLSIZE;
// It is stated that the payload size
// is taken from first, and every next one
// will get the same.
m_iMaxPayloadSize = plsize;
}
return --end;
}
CUDTGroup::SocketData CUDTGroup::prepareData(CUDTSocket* s)
{
// This uses default SRT_GST_BROKEN because when the group operation is done,
// then the SRT_GST_IDLE state automatically turns into SRT_GST_RUNNING. This is
// recognized as an initial state of the fresh added socket to the group,
// so some "initial configuration" must be done on it, after which it's
// turned into SRT_GST_RUNNING, that is, it's treated as all others. When
// set to SRT_GST_BROKEN, this socket is disregarded. This socket isn't cleaned
// up, however, unless the status is simultaneously SRTS_BROKEN.
// The order of operations is then:
// - add the socket to the group in this "broken" initial state
// - connect the socket (or get it extracted from accept)
// - update the socket state (should be SRTS_CONNECTED)
// - once the connection is established (may take time with connect), set SRT_GST_IDLE
// - the next operation of send/recv will automatically turn it into SRT_GST_RUNNING
SocketData sd = {
s->m_SocketID,
s,
-1,
SRTS_INIT,
SRT_GST_BROKEN,
SRT_GST_BROKEN,
-1,
-1,
sockaddr_any(),
sockaddr_any(),
false,
false,
false,
0 // weight
};
return sd;
}
CUDTGroup::CUDTGroup(SRT_GROUP_TYPE gtype)
: m_pGlobal(&CUDT::s_UDTUnited)
, m_GroupID(-1)
, m_PeerGroupID(-1)
, m_selfManaged(true)
, m_bSyncOnMsgNo(false)
, m_type(gtype)
, m_listener()
, m_iSndOldestMsgNo(SRT_MSGNO_NONE)
, m_iSndAckedMsgNo(SRT_MSGNO_NONE)
, m_uOPT_StabilityTimeout(CUDT::COMM_DEF_STABILITY_TIMEOUT_US)
// -1 = "undefined"; will become defined with first added socket
, m_iMaxPayloadSize(-1)
, m_bSynRecving(true)
, m_bSynSending(true)
, m_bTsbPd(true)
, m_bTLPktDrop(true)
, m_iTsbPdDelay_us(0)
// m_*EID and m_*Epolld fields will be initialized
// in the constructor body.
, m_iSndTimeOut(-1)
, m_iRcvTimeOut(-1)
, m_tsStartTime()
, m_tsRcvPeerStartTime()
, m_RcvBaseSeqNo(SRT_SEQNO_NONE)
, m_bOpened(false)
, m_bConnected(false)
, m_bClosing(false)
, m_iLastSchedSeqNo(SRT_SEQNO_NONE)
, m_iLastSchedMsgNo(SRT_MSGNO_NONE)
{
setupMutex(m_GroupLock, "Group");
setupMutex(m_RcvDataLock, "RcvData");
setupCond(m_RcvDataCond, "RcvData");
m_RcvEID = m_pGlobal->m_EPoll.create(&m_RcvEpolld);
m_SndEID = m_pGlobal->m_EPoll.create(&m_SndEpolld);
// Set this data immediately during creation before
// two or more sockets start arguing about it.
m_iLastSchedSeqNo = CUDT::generateISN();
// Configure according to type
switch (gtype)
{
case SRT_GTYPE_BROADCAST:
m_selfManaged = true;
break;
case SRT_GTYPE_BACKUP:
m_selfManaged = true;
break;
case SRT_GTYPE_BALANCING:
m_selfManaged = true;
m_bSyncOnMsgNo = true;
break;
case SRT_GTYPE_MULTICAST:
m_selfManaged = false;
break;
default:
break;
}
}
CUDTGroup::~CUDTGroup()
{
srt_epoll_release(m_RcvEID);
srt_epoll_release(m_SndEID);
releaseMutex(m_GroupLock);
releaseMutex(m_RcvDataLock);
releaseCond(m_RcvDataCond);
}
void CUDTGroup::GroupContainer::erase(CUDTGroup::gli_t it)
{
if (it == m_LastActiveLink)
{
if (m_List.empty())
{
LOGC(gmlog.Error, log << "IPE: GroupContainer is empty and 'erase' is called on it.");
return; // this avoids any misunderstandings in iterator checks
}
gli_t bb = m_List.begin();
++bb;
if (bb == m_List.end()) // means: m_List.size() == 1
{
// One element, this one being deleted, nothing to point to.
m_LastActiveLink = null();
}
else
{
// Set the link to the previous element IN THE RING.
// We have the position pointer.
// Reverse iterator is automatically decremented.
std::reverse_iterator<gli_t> rt(m_LastActiveLink);
if (rt == m_List.rend())
rt = m_List.rbegin();
m_LastActiveLink = rt.base();
// This operation is safe because we know that:
// - the size of the container is at least 2 (0 and 1 cases are handled above)
// - if m_LastActiveLink == m_List.begin(), `rt` is shifted to the opposite end.
--m_LastActiveLink;
}
}
m_List.erase(it);
}
void CUDTGroup::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
{
HLOGC(gmlog.Debug,
log << "GROUP $" << id() << " OPTION: #" << optName
<< " value:" << FormatBinaryString((uint8_t*)optval, optlen));
switch (optName)
{
case SRTO_RCVSYN:
m_bSynRecving = cast_optval<bool>(optval, optlen);
return;
case SRTO_SNDSYN:
m_bSynSending = cast_optval<bool>(optval, optlen);
return;
case SRTO_SNDTIMEO:
m_iSndTimeOut = cast_optval<int>(optval, optlen);
break;
case SRTO_RCVTIMEO:
m_iRcvTimeOut = cast_optval<int>(optval, optlen);
break;
case SRTO_GROUPSTABTIMEO:
{
const int val = cast_optval<int>(optval, optlen);
// Search if you already have SRTO_PEERIDLETIMEO set
int idletmo = CUDT::COMM_RESPONSE_TIMEOUT_MS;
vector<ConfigItem>::iterator f =
find_if(m_config.begin(), m_config.end(), ConfigItem::OfType(SRTO_PEERIDLETIMEO));
if (f != m_config.end())
{
f->get(idletmo); // worst case, it will leave it unchanged.
}
if (val >= idletmo)
{
LOGC(qmlog.Error,
log << "group option: SRTO_GROUPSTABTIMEO(" << val << ") exceeds SRTO_PEERIDLETIMEO(" << idletmo
<< ")");
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}
m_uOPT_StabilityTimeout = val * 1000;
}
break;
// XXX Currently no socket groups allow any other
// congestion control mode other than live.
case SRTO_CONGESTION:
{
LOGP(gmlog.Error, "group option: SRTO_CONGESTION is only allowed as 'live' and cannot be changed");
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}
// Other options to be specifically interpreted by group may follow.
default:
break;
}
// All others must be simply stored for setting on a socket.
// If the group is already open and any post-option is about
// to be modified, it must be allowed and applied on all sockets.
if (m_bOpened)
{
// There's at least one socket in the group, so only
// post-options are allowed.
if (!std::binary_search(srt_post_opt_list, srt_post_opt_list + SRT_SOCKOPT_NPOST, optName))
{
LOGC(gmlog.Error, log << "setsockopt(group): Group is connected, this option can't be altered");
throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
}
HLOGC(gmlog.Debug, log << "... SPREADING to existing sockets.");
// This means that there are sockets already, so apply
// this option on them.
ScopedLock gg(m_GroupLock);
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
gi->ps->core().setOpt(optName, optval, optlen);
}
}
// Store the option regardless if pre or post. This will apply
m_config.push_back(ConfigItem(optName, optval, optlen));
}
static bool getOptDefault(SRT_SOCKOPT optname, void* optval, int& w_optlen);
// unfortunately this is required to properly handle th 'default_opt != opt'
// operation in the below importOption. Not required simultaneously operator==.
static bool operator!=(const struct linger& l1, const struct linger& l2)
{
return l1.l_onoff != l2.l_onoff || l1.l_linger != l2.l_linger;
}
template <class ValueType>
static void importOption(vector<CUDTGroup::ConfigItem>& storage, SRT_SOCKOPT optname, const ValueType& field)
{
ValueType default_opt = ValueType();
int default_opt_size = sizeof(ValueType);
ValueType opt = field;
if (!getOptDefault(optname, (&default_opt), (default_opt_size)) || default_opt != opt)
{
// Store the option when:
// - no default for this option is found
// - the option value retrieved from the field is different than default
storage.push_back(CUDTGroup::ConfigItem(optname, &opt, default_opt_size));
}
}
// This function is called by the same premises as the CUDT::CUDT(const CUDT&) (copy constructor).
// The intention is to rewrite the part that comprises settings from the socket
// into the group. Note that some of the settings concern group, some others concern
// only target socket, and there are also options that can't be set on a socket.
void CUDTGroup::deriveSettings(CUDT* u)
{
// !!! IMPORTANT !!!
//
// This function shall ONLY be called on a newly created group
// for the sake of the newly accepted socket from the group-enabled listener,
// which is lazy-created for the first ever accepted socket.
// Once the group is created, it should stay with the options
// state as initialized here, and be changeable only in case when
// the option is altered on the group.
// SRTO_RCVSYN
m_bSynRecving = u->m_bSynRecving;
// SRTO_SNDSYN
m_bSynSending = u->m_bSynSending;
// SRTO_RCVTIMEO
m_iRcvTimeOut = u->m_iRcvTimeOut;
// SRTO_SNDTIMEO
m_iSndTimeOut = u->m_iSndTimeOut;
// Ok, this really is disgusting, but there's only one way
// to properly do it. Would be nice to have some more universal
// connection between an option symbolic name and the internals
// in CUDT class, but until this is done, since now every new
// option will have to be handled both in the CUDT::setOpt/getOpt
// functions, and here as well.
// This is about moving options from listener to the group,
// to be potentially replicated on the socket. So both pre
// and post options apply.
#define IM(option, field) importOption(m_config, option, u->field)
IM(SRTO_MSS, m_iMSS);
IM(SRTO_FC, m_iFlightFlagSize);
// Nonstandard
importOption(m_config, SRTO_SNDBUF, u->m_iSndBufSize * (u->m_iMSS - CPacket::UDP_HDR_SIZE));
importOption(m_config, SRTO_RCVBUF, u->m_iRcvBufSize * (u->m_iMSS - CPacket::UDP_HDR_SIZE));
IM(SRTO_LINGER, m_Linger);
IM(SRTO_UDP_SNDBUF, m_iUDPSndBufSize);
IM(SRTO_UDP_RCVBUF, m_iUDPRcvBufSize);
// SRTO_RENDEZVOUS: impossible to have it set on a listener socket.
// SRTO_SNDTIMEO/RCVTIMEO: groupwise setting
IM(SRTO_CONNTIMEO, m_tdConnTimeOut);
IM(SRTO_DRIFTTRACER, m_bDriftTracer);
// Reuseaddr: true by default and should only be true.
IM(SRTO_MAXBW, m_llMaxBW);
IM(SRTO_INPUTBW, m_llInputBW);
IM(SRTO_OHEADBW, m_iOverheadBW);
IM(SRTO_IPTOS, m_iIpToS);
IM(SRTO_IPTTL, m_iIpTTL);
IM(SRTO_TSBPDMODE, m_bOPT_TsbPd);
IM(SRTO_RCVLATENCY, m_iOPT_TsbPdDelay);
IM(SRTO_PEERLATENCY, m_iOPT_PeerTsbPdDelay);
IM(SRTO_SNDDROPDELAY, m_iOPT_SndDropDelay);
IM(SRTO_PAYLOADSIZE, m_zOPT_ExpPayloadSize);
IM(SRTO_TLPKTDROP, m_bTLPktDrop);
IM(SRTO_STREAMID, m_sStreamName);
IM(SRTO_MESSAGEAPI, m_bMessageAPI);
IM(SRTO_NAKREPORT, m_bRcvNakReport);
IM(SRTO_MINVERSION, m_lMinimumPeerSrtVersion);
IM(SRTO_ENFORCEDENCRYPTION, m_bOPT_StrictEncryption);
IM(SRTO_IPV6ONLY, m_iIpV6Only);
IM(SRTO_PEERIDLETIMEO, m_iOPT_PeerIdleTimeout);
IM(SRTO_GROUPSTABTIMEO, m_uOPT_StabilityTimeout);
IM(SRTO_PACKETFILTER, m_OPT_PktFilterConfigString);
importOption(m_config, SRTO_PBKEYLEN, u->m_pCryptoControl->KeyLen());
// Passphrase is empty by default. Decipher the passphrase and
// store as passphrase option
if (u->m_CryptoSecret.len)
{
string password((const char*)u->m_CryptoSecret.str, u->m_CryptoSecret.len);
m_config.push_back(ConfigItem(SRTO_PASSPHRASE, password.c_str(), password.size()));
}
IM(SRTO_KMREFRESHRATE, m_uKmRefreshRatePkt);
IM(SRTO_KMPREANNOUNCE, m_uKmPreAnnouncePkt);
string cc = u->m_CongCtl.selected_name();
if (cc != "live")
{
m_config.push_back(ConfigItem(SRTO_CONGESTION, cc.c_str(), cc.size()));
}
// NOTE: This is based on information extracted from the "semi-copy-constructor" of CUDT class.
// Here should be handled all things that are options that modify the socket, but not all options
// are assigned to configurable items.
#undef IM
}
bool CUDTGroup::applyFlags(uint32_t flags, HandshakeSide hsd)
{
bool synconmsg = IsSet(flags, SRT_GFLAG_SYNCONMSG);
if (m_type == SRT_GTYPE_BALANCING)
{
// We support only TRUE for this flag
if (!synconmsg)
{
HLOGP(gmlog.Debug, "GROUP: Balancing mode implemented only with sync on msgno - overridden request");
return true; // accept, but override
}
// We have this flag set; change it in yourself, if needed.
if (hsd == HSD_INITIATOR && !m_bSyncOnMsgNo)
{
// With this you can change in future the default value to false.
HLOGP(gmlog.Debug, "GROUP: Balancing requrested msgno-sync, OVERRIDING original setting");
m_bSyncOnMsgNo = true;
return true;
}
}
else
{
if (synconmsg)
{
LOGP(gmlog.Error, "GROUP: non-balancing type requested sync on msgno - IPE/EPE?");
return false;
}
}
// Ignore the flag anyway. This can change in future versions though.
return true;
}
template <class Type>
struct Value
{
static int fill(void* optval, int, Type value)
{
// XXX assert size >= sizeof(Type) ?
*(Type*)optval = value;
return sizeof(Type);
}
};
template <>
inline int Value<std::string>::fill(void* optval, int len, std::string value)
{
if (size_t(len) < value.size())
return 0;
memcpy(optval, value.c_str(), value.size());
return value.size();
}
template <class V>
inline int fillValue(void* optval, int len, V value)
{
return Value<V>::fill(optval, len, value);
}
static bool getOptDefault(SRT_SOCKOPT optname, void* pw_optval, int& w_optlen)
{
static const linger def_linger = {1, CUDT::DEF_LINGER_S};
switch (optname)
{
default:
return false;
#define RD(value) \
w_optlen = fillValue((pw_optval), w_optlen, value); \
break
case SRTO_KMSTATE:
case SRTO_SNDKMSTATE:
case SRTO_RCVKMSTATE:
RD(SRT_KM_S_UNSECURED);
case SRTO_PBKEYLEN:
RD(16);
case SRTO_MSS:
RD(CUDT::DEF_MSS);
case SRTO_SNDSYN:
RD(true);
case SRTO_RCVSYN:
RD(true);
case SRTO_ISN:
RD(SRT_SEQNO_NONE);
case SRTO_FC:
RD(CUDT::DEF_FLIGHT_SIZE);
case SRTO_SNDBUF:
case SRTO_RCVBUF:
w_optlen = fillValue((pw_optval), w_optlen, CUDT::DEF_BUFFER_SIZE * (CUDT::DEF_MSS - CPacket::UDP_HDR_SIZE));
break;
case SRTO_LINGER:
RD(def_linger);
case SRTO_UDP_SNDBUF:
case SRTO_UDP_RCVBUF:
RD(CUDT::DEF_UDP_BUFFER_SIZE);
case SRTO_RENDEZVOUS:
RD(false);
case SRTO_SNDTIMEO:
RD(-1);
case SRTO_RCVTIMEO:
RD(-1);
case SRTO_REUSEADDR:
RD(true);
case SRTO_MAXBW:
RD(int64_t(-1));
case SRTO_INPUTBW:
RD(int64_t(-1));
case SRTO_OHEADBW:
RD(0);
case SRTO_STATE:
RD(SRTS_INIT);
case SRTO_EVENT:
RD(0);
case SRTO_SNDDATA:
RD(0);
case SRTO_RCVDATA:
RD(0);
case SRTO_IPTTL:
RD(0);
case SRTO_IPTOS:
RD(0);
case SRTO_SENDER:
RD(false);
case SRTO_TSBPDMODE:
RD(false);
case SRTO_LATENCY:
case SRTO_RCVLATENCY:
case SRTO_PEERLATENCY:
RD(SRT_LIVE_DEF_LATENCY_MS);
case SRTO_TLPKTDROP:
RD(true);
case SRTO_SNDDROPDELAY:
RD(-1);
case SRTO_NAKREPORT:
RD(true);
case SRTO_VERSION:
RD(SRT_DEF_VERSION);
case SRTO_PEERVERSION:
RD(0);
case SRTO_CONNTIMEO:
RD(-1);
case SRTO_DRIFTTRACER:
RD(true);
case SRTO_MINVERSION:
RD(0);
case SRTO_STREAMID:
RD(std::string());
case SRTO_CONGESTION:
RD(std::string());
case SRTO_MESSAGEAPI:
RD(true);
case SRTO_PAYLOADSIZE:
RD(0);
}
#undef RD
return true;
}
void CUDTGroup::getOpt(SRT_SOCKOPT optname, void* pw_optval, int& w_optlen)
{
// Options handled in group
switch (optname)
{
case SRTO_RCVSYN:
*(bool*)pw_optval = m_bSynRecving;
w_optlen = sizeof(bool);
return;
case SRTO_SNDSYN:
*(bool*)pw_optval = m_bSynSending;
w_optlen = sizeof(bool);
return;
default:; // pass on
}
CUDTSocket* ps = 0;
{
// In sockets. All sockets should have all options
// set the same and should represent the group state
// well enough. If there are no sockets, just use default.
// Group lock to protect the container itself.
// Once a socket is extracted, we state it cannot be
// closed without the group send/recv function or closing
// being involved.
ScopedLock lg(m_GroupLock);
if (m_Group.empty())
{
if (!getOptDefault(optname, (pw_optval), (w_optlen)))
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
return;
}
ps = m_Group.begin()->ps;
// Release the lock on the group, as it's not necessary,
// as well as it might cause a deadlock when combined
// with the others.
}
if (!ps)
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
return ps->core().getOpt(optname, (pw_optval), (w_optlen));
}
struct HaveState : public unary_function<pair<SRTSOCKET, SRT_SOCKSTATUS>, bool>
{
SRT_SOCKSTATUS s;
HaveState(SRT_SOCKSTATUS ss)
: s(ss)
{
}
bool operator()(pair<SRTSOCKET, SRT_SOCKSTATUS> i) const { return i.second == s; }
};
SRT_SOCKSTATUS CUDTGroup::getStatus()
{
typedef vector<pair<SRTSOCKET, SRT_SOCKSTATUS> > states_t;
states_t states;
{
ScopedLock cg(m_GroupLock);
for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
{
switch (gi->sndstate)
{
// Check only sndstate. If this machine is ONLY receiving,
// then rcvstate will turn into SRT_GST_RUNNING, while
// sndstate will remain SRT_GST_IDLE, but still this may only
// happen if the socket is connected.
case SRT_GST_IDLE:
case SRT_GST_RUNNING:
states.push_back(make_pair(gi->id, SRTS_CONNECTED));
break;
case SRT_GST_BROKEN:
states.push_back(make_pair(gi->id, SRTS_BROKEN));
break;
default: // (pending, or whatever will be added in future)
{
SRT_SOCKSTATUS st = m_pGlobal->getStatus(gi->id);
states.push_back(make_pair(gi->id, st));
}
}
}
}
// If at least one socket is connected, the state is connected.
if (find_if(states.begin(), states.end(), HaveState(SRTS_CONNECTED)) != states.end())
return SRTS_CONNECTED;
// Otherwise find at least one socket, which's state isn't broken.
// If none found, return SRTS_BROKEN.
states_t::iterator p = find_if(states.begin(), states.end(), not1(HaveState(SRTS_BROKEN)));
if (p != states.end())
{
// Return that state as group state
return p->second;
}
return SRTS_BROKEN;
}
void CUDTGroup::syncWithSocket(const CUDT& core, const HandshakeSide side)
{
// [[using locked(m_GroupLock)]];
if (side == HSD_RESPONDER)
{
// On the listener side you should synchronize ISN with the incoming
// socket, which is done immediately after creating the socket and
// adding it to the group. On the caller side the ISN is defined in
// the group directly, before any member socket is created.
set_currentSchedSequence(core.ISN());
}
// XXX
// Might need further investigation as to whether this isn't
// wrong for some cases. By having this -1 here the value will be
// laziliy set from the first reading one. It is believed that
// it covers all possible scenarios, that is:
//
// - no readers - no problem!
// - have some readers and a new is attached - this is set already
// - connect multiple links, but none has read yet - you'll be the first.
//
// Previous implementation used setting to: core.m_iPeerISN
resetInitialRxSequence();
// Get the latency (possibly fixed against the opposite side)
// from the first socket (core.m_iTsbPdDelay_ms),
// and set it on the current socket.
set_latency(core.m_iTsbPdDelay_ms * int64_t(1000));
}
void CUDTGroup::close()
{
// Close all descriptors, then delete the group.
vector<SRTSOCKET> ids;
{
ScopedLock g(m_GroupLock);
// A non-managed group may only be closed if there are no
// sockets in the group.
// XXX Fortunately there are currently no non-self-managed
// groups, so this error cannot ever happen, but this error
// has the overall code suggesting that it's about the listener,
// so either the name should be changed here, or a different code used.
if (!m_selfManaged && !m_Group.empty())
throw CUDTException(MJ_NOTSUP, MN_BUSY, 0);
// Copy the list of IDs into the array.
for (gli_t ig = m_Group.begin(); ig != m_Group.end(); ++ig)
ids.push_back(ig->id);
}
// Close all sockets with unlocked GroupLock
for (vector<SRTSOCKET>::iterator i = ids.begin(); i != ids.end(); ++i)
m_pGlobal->close(*i);
// Lock the group again to clear the group data
{
ScopedLock g(m_GroupLock);
m_Group.clear();
m_PeerGroupID = -1;
// This takes care of the internal part.
// The external part will be done in Global (CUDTUnited)
}
// Release blocked clients
CSync::lock_signal(m_RcvDataCond, m_RcvDataLock);
}
int CUDTGroup::send(const char* buf, int len, SRT_MSGCTRL& w_mc)
{
switch (m_type)
{
default:
LOGC(gslog.Error, log << "CUDTGroup::send: not implemented for type #" << m_type);
throw CUDTException(MJ_SETUP, MN_INVAL, 0);
case SRT_GTYPE_BROADCAST:
return sendBroadcast(buf, len, (w_mc));
case SRT_GTYPE_BACKUP:
return sendBackup(buf, len, (w_mc));
/* to be implemented
case SRT_GTYPE_BALANCING:
return sendBalancing(buf, len, (w_mc));
case SRT_GTYPE_MULTICAST:
return sendMulticast(buf, len, (w_mc));
*/
}
}
int CUDTGroup::sendBroadcast(const char* buf, int len, SRT_MSGCTRL& w_mc)
{
// Avoid stupid errors in the beginning.
if (len <= 0)
{
throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
}
// NOTE: This is a "vector of list iterators". Every element here
// is an iterator to another container.
// Note that "list" is THE ONLY container in standard C++ library,
// for which NO ITERATORS ARE INVALIDATED after a node at particular