-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardflowg.h
2299 lines (2184 loc) · 74 KB
/
hardflowg.h
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
//! \file
//! \ingroup in_out_group
//! \ingroup network_in_out_group
//! \brief Êîììóíèêàöèîííûå ïîòîêè
//!
//! Äàòà: 21.05.2011\n
//! Äàòà ñîçäàíèÿ: 27.08.2009
#ifndef hardflowgH
#define hardflowgH
#include <irsdefs.h>
#if defined(IRS_WIN32)
// Standart Windows headers
#include <winsock2.h>
#elif defined(IRS_LINUX)
// Standart Linux headers
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif // IRS_WINDOWS IRS_LINUX
#ifdef IRS_STM32F_2_AND_4
# include <armioregs.h>
# include <armcfg.h>
#endif //IRS_STM32F_2_AND_4
// Standart C++ headers
#include <string.h>
// mxsrclib headers
#include <irsstd.h>
#include <timer.h>
#include <irserror.h>
#include <mxifar.h>
#include <mxdata.h>
#include <irscpp.h>
#include <irstcpip.h>
#include <irsnetdefs.h>
#include <irssysutils.h>
#include <irsdsp.h>
#include <irsfinal.h>
#ifndef IRS_LIB_HARDFLOWG_DEBUG_TYPE
# define IRS_LIB_HARDFLOWG_DEBUG_TYPE IRS_LIB_DEBUG_NONE
#endif // !IRS_LIB_HARDFLOWG_DEBUG_TYPE
# if (IRS_LIB_HARDFLOWG_DEBUG_TYPE == IRS_LIB_DEBUG_BASE)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BASE(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_BASE(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_SRC_BASE(msg) IRS_LIB_DBG_MSG_SRC(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_DETAIL(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_DETAIL(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_BASE(msg) msg
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_DETAIL(msg)
#elif (IRS_LIB_HARDFLOWG_DEBUG_TYPE == IRS_LIB_DEBUG_DETAIL)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BASE(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_BASE(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_SRC_BASE(msg) IRS_LIB_DBG_MSG_SRC(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_BASE(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_DETAIL(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_DETAIL(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_DETAIL(msg) msg
#else // IRS_LIB_HARDFLOWG_DEBUG_TYPE == IRS_LIB_DEBUG_NONE
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BASE(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_BASE(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_SRC_BASE(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_BASE(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_DETAIL(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_DETAIL(msg)
# define IRS_LIB_HARDFLOWG_DBG_MSG_SRC_DETAIL(msg)
# define IRS_LIB_HARDFLOWG_DBG_RAW_MSG_BLOCK_DETAIL(msg)
#endif // IRS_LIB_HARDFLOWG_DEBUG_TYPE == IRS_LIB_DEBUG_NONE
#if defined(IRS_WIN32) || defined(IRS_LINUX)
inline unsigned long extract_ip(const sockaddr_in& a_address)
{
#if defined(IRS_WIN32)
return a_address.sin_addr.S_un.S_addr;
#elif defined (IRS_LINUX)
return a_address.sin_addr.s_addr;
#else //arch
#error Current architecture not supported. Supported only IRS_WIN32 \
and IRS_LINUX
return 0;
#endif //arch
}
inline bool operator<(const sockaddr_in& a_first_adr,
const sockaddr_in& a_second_adr)
{
bool first_less_second = false;
if (extract_ip(a_first_adr) < extract_ip(a_second_adr))
{
first_less_second = true;
} else if (extract_ip(a_first_adr) == extract_ip(a_second_adr)) {
first_less_second = (a_first_adr.sin_port < a_second_adr.sin_port);
} else {
// Ïåðâûé áîëüøå âòîðîãî
}
return first_less_second;
};
#endif // defined(IRS_WIN32) || defined(IRS_LINUX)
namespace irs {
//! \brief Àáñòðàêòíûé èíòåðôåéñ äëÿ êëàññîâ, ðåàëèçóþùèõ îáìåí äàííûìè ïî
//! ðàçëè÷íûì ïðîòîêîëàì.
//! \ingroup in_out_group
//! \author Krasheninnikov Maxim
class hardflow_t {
public:
typedef size_t size_type;
typedef string_t string_type;
typedef char_t char_type;
enum {
invalid_channel = 0
};
enum channel_switching_mode_t {
csm_any,
csm_ready_for_reading,
csm_ready_for_writing
};
//! \brief Äåñòðóêòîð
virtual ~hardflow_t() {}
//! \brief ×òåíèå ñïåöèôè÷åñêîãî ïàðàìåòðà
//!
//! \param[in] a_name - èìÿ ÷èòàåìîãî ïàðàìåòðà
virtual string_type param(const string_type &a_name) = 0;
//! \brief Óñòàíîâêà ñïåöèôè÷åñêîãî ïàðàìåòðà
//!
//! \param[in] a_name - èìÿ óñòàíàâëèâàåìîãî ïàðàìåòðà
//! \param[in] a_value - íîâîå çíà÷åíèå ïàðàìåòðà
virtual void set_param(const string_type &a_name,
const string_type &a_value) = 0;
//! \brief ×òåíèå äàííûõ
//!
//! \param[in] a_channel_ident – êàíàë, èç êîòîðîãî ÷èòàþòñÿ äàííûå;
//! \param[out] ap_buf – áóôåð, â êîòîðûé ñ÷èòûâàþòñÿ äàííûå;
//! \param[in] a_size – ðàçìåð ñ÷èòûâàåìûõ äàííûõ.
virtual size_type read(size_type a_channel_ident, irs_u8 *ap_buf,
size_type a_size) = 0;
//! \brief Çàïèñü äàííûõ
//!
//! \param[in] a_channel_ident – êàíàë, â êîòîðûé ïèøóòñÿ äàííûå;
//! \param[in] ap_buf – óêàçàòåëü íà íà÷àëî áóôåðà ñ äàííûìè;
//! \param[in] a_size – ðàçìåð ïåðåäàâàåìûõ äàííûõ.
virtual size_type write(size_type a_channel_ident, const irs_u8 *ap_buf,
size_type a_size) = 0;
//! \brief Ïåðåõîä ê ñëåäóþùåìó êàíàëó
virtual size_type channel_next() = 0;
//! \brief Ïåðåêëþ÷åíèå ðåæèìà ïåðåõîäà ê ñëåäóþùåìó ñóùåñòâóþùåìó êàíàëó
virtual void set_channel_switching_mode(channel_switching_mode_t /*a_mode*/)
{
}
//! \brief Ïðîâåðêà íà íàëè÷èå êàíàëà
//!
//! \param[in] a_channel_ident - ïðîâåðÿåìûé êàíàë
virtual bool is_channel_exists(size_type a_channel_ident) = 0;
//! \brief Ýëåìåíòàðíàÿ îïåðàöèÿ
virtual void tick() = 0;
};
namespace hardflow {
//! \addtogroup network_in_out_group
//! @{
typedef hardflow_t::string_type stringns_t;
enum udp_limit_connections_mode_t {
udplc_mode_invalid, //!< \brief Íåäîïóñòèìûé ðåæèì
udplc_mode_queue, //!< \brief Ó÷èòûâàåòñÿ ïåðåìåííàÿ m_max_size
udplc_mode_limited, //!< \brief Ó÷èòûâàåòñÿ ïåðåìåííàÿ m_max_size
udplc_mode_unlimited //!< \brief Ïåðåìåííàÿ m_max_size íå ó÷èòûâàåòñÿ
};
#ifndef __ICCAVR__
template <class address_t>
class udp_channel_list_t
{
public:
class less_t;
class udp_flow_t;
typedef hardflow_t::size_type size_type;
typedef hardflow_t::string_type string_type;
typedef size_type id_type;
typedef irs::deque_data_t<irs_u8> buffer_type;
typedef address_t address_type;
struct channel_t
{
address_type address;
buffer_type buffer;
measure_time_t lifetime;
measure_time_t downtime;
channel_t():
address(),
buffer(),
lifetime(),
downtime()
{
}
};
typedef map<address_type, id_type> map_address_id_type;
typedef typename map<address_type, id_type>::iterator map_address_id_iterator;
typedef map<id_type, channel_t> map_id_channel_type;
typedef typename map<id_type, channel_t>::iterator map_id_channel_iterator;
typedef typename map<id_type, channel_t>::const_iterator
map_id_channel_const_iterator;
typedef deque<id_type> queue_id_type;
enum { invalid_channel = hardflow_t::invalid_channel };
udp_channel_list_t(
const udp_limit_connections_mode_t a_mode = udplc_mode_queue,
const size_type a_max_size = 1000,
const size_type a_channel_buf_max_size = 32768,
const bool a_limit_lifetime_enabled = false,
const double a_max_lifetime_sec = 24*60*60,
const bool a_limit_downtime_enabled = false,
const double a_max_downtime_sec = 60*60
);
bool id_get(address_type a_address, id_type* ap_id);
bool address_get(id_type a_id, address_type* ap_address);
// Âîçâðàùàåò false, åñëè äîñòèãíóò ëèìèò, è true, åñëè àäðåñ óñïåøíî
// äîáàâëåí â ñïèñîê èëè òàêîé àäðåñ óæå åñòü
void insert(address_type a_address, id_type* ap_id, bool* ap_insert_success);
void erase(const id_type a_id);
bool is_channel_exists(const id_type a_id);
size_type channel_buf_max_size_get() const;
void channel_buf_max_size_set(size_type a_channel_buf_max_size);
double lifetime_get(const id_type a_channel_id) const;
bool limit_lifetime_enabled_get() const;
void limit_lifetime_enabled_set(bool a_limit_lifetime_enabled);
double max_lifetime_get() const;
void max_lifetime_set(double a_max_lifetime_sec);
void downtime_timer_reset(const id_type a_channel_id);
double downtime_get(const id_type a_channel_id) const;
bool limit_downtime_enabled_get() const;
void limit_downtime_enabled_set(bool a_limit_downtime_enabled);
double max_downtime_get() const;
void max_downtime_set(double a_max_downtime_sec);
void channel_address_get(const id_type a_channel_id,
address_type* ap_address) ;
void channel_address_set(const id_type a_channel_id,
const address_type& a_address);
void clear();
size_type size();
void mode_set(const udp_limit_connections_mode_t a_mode);
size_type max_size_get();
void max_size_set(size_type a_max_size);
size_type write(const address_type& a_address, const irs_u8 *ap_buf,
size_type a_size);
size_type read(size_type a_id, irs_u8 *ap_buf,
size_type a_size);
size_type channel_next();
size_type cur_channel() const;
void tick();
private:
bool lifetime_exceeded(const map_id_channel_iterator);
bool downtime_exceeded(const map_id_channel_iterator);
void next_free_channel_id();
udp_limit_connections_mode_t m_mode;
size_type m_max_size;
size_type m_channel_id;
bool m_channel_id_overflow;
const size_type m_channel_max_count;
map_id_channel_type m_map_id_channel;
map_address_id_type m_map_address_id;
queue_id_type m_id_list;
size_type m_buf_max_size;
map_id_channel_iterator m_it_cur_channel;
map_id_channel_iterator m_it_cur_channel_for_check;
bool m_max_lifetime_enabled;
bool m_max_downtime_enabled;
counter_t m_max_lifetime;
counter_t m_max_downtime;
};
template <class address_t>
irs::hardflow::udp_channel_list_t<address_t>::udp_channel_list_t(
const udp_limit_connections_mode_t a_mode,
const size_type a_max_size,
const size_type a_channel_buf_max_size,
const bool a_limit_lifetime_enabled,
const double a_max_lifetime_sec,
const bool a_limit_downtime_enabled,
const double a_max_downtime_sec
):
m_mode(a_mode),
m_max_size(min(a_max_size, static_cast<size_type>(-1))),
m_channel_id(invalid_channel + 1),
m_channel_id_overflow(false),
m_channel_max_count(static_cast<size_type>(-1)),
m_map_id_channel(),
//m_map_id_address(),
m_map_address_id(),
m_id_list(),
m_buf_max_size(a_channel_buf_max_size),
m_it_cur_channel(m_map_id_channel.end()),
m_it_cur_channel_for_check(m_map_id_channel.end()),
m_max_lifetime_enabled(a_limit_lifetime_enabled),
m_max_downtime_enabled(a_limit_downtime_enabled),
m_max_lifetime(irs::make_cnt_s(a_max_lifetime_sec)),
m_max_downtime(irs::make_cnt_s(a_max_downtime_sec))
{
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::insert(address_type a_address,
id_type* ap_id, bool* ap_insert_success)
{
*ap_insert_success = false;
// Èùåì, åñòü ëè óæå òàêîé àäðåñ â ñïèñêå
map_address_id_iterator it_map_addr_id = m_map_address_id.find(a_address);
if (it_map_addr_id == m_map_address_id.end()) {
bool allow_add = false;
switch (m_mode) {
case udplc_mode_queue: {
IRS_ASSERT(m_id_list.size() <= m_max_size);
if (m_id_list.size() < m_max_size) {
allow_add = true;
} else {
if (m_id_list.size() > 0) {
erase(m_id_list.front());
allow_add = true;
} else {
// Ìàêñèìàëüíûé ðàçìåð óñòàíîâëåí â íîëü
}
}
} break;
case udplc_mode_limited: {
IRS_ASSERT(m_id_list.size() <= m_max_size);
if (m_id_list.size() < m_max_size) {
allow_add = true;
} else {
// Äîñòèãíóò ëèìèò êîëè÷åñòâà çàïèñåé
}
} break;
case udplc_mode_unlimited: {
if (m_id_list.size() <= m_channel_max_count) {
allow_add = true;
} else {
// Äîñòèãíóò ëèìèò êîëè÷åñòâà çàïèñåé
}
} break;
default : {
IRS_ASSERT_MSG("Íåèçâåñòíûé âàðèàíò ðàáî÷åãî ðåæèìà");
}
}
if (allow_add) {
next_free_channel_id();
IRS_LIB_ASSERT(m_channel_id != invalid_channel);
channel_t channel;
channel.address = a_address;
channel.lifetime.start();
channel.downtime.start();
std::pair<map_id_channel_iterator, bool> map_id_channel_res;
std::pair<map_address_id_iterator, bool> map_address_id_res;
#ifndef IRS_NO_EXCEPTIONS
const size_type channel_prev_count = m_id_list.size();
try {
#endif // IRS_NO_EXCEPTIONS
map_id_channel_res =
m_map_id_channel.insert(make_pair(m_channel_id, channel));
map_address_id_res =
m_map_address_id.insert(make_pair(a_address, m_channel_id));
m_id_list.push_back(m_channel_id);
*ap_id = m_channel_id;
if (m_it_cur_channel == m_map_id_channel.end()) {
m_it_cur_channel = m_map_id_channel.begin();
} else {
// Òåêóùèé êàíàë óæå óñòàíîâëåí
}
if (m_it_cur_channel_for_check == m_map_id_channel.end()) {
m_it_cur_channel_for_check = m_map_id_channel.begin();
} else {
// Òåêóùèé êàíàë äëÿ ïðîâåðêè óæå óñòàíîâëåí
}
*ap_insert_success = true;
#ifndef IRS_NO_EXCEPTIONS
} catch (...) {
if (m_map_id_channel.size() > channel_prev_count) {
m_map_id_channel.erase(map_id_channel_res.first);
}
if (m_map_address_id.size() > channel_prev_count) {
m_map_address_id.erase(map_address_id_res.first);
}
m_id_list.resize(channel_prev_count);
}
#endif // IRS_NO_EXCEPTIONS
} else {
// Äîáàâëåíèå íå ðàçðåøåíî
}
} else {
// Ýëåìåíò óæå ïðèñóòñâóåò â ñïèñêå
*ap_id = it_map_addr_id->second;
*ap_insert_success = true;
}
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::id_get(
address_type a_address, id_type* ap_id)
{
bool find_success = false;
map_address_id_iterator it_map_addr_id = m_map_address_id.end();
it_map_addr_id = m_map_address_id.find(a_address);
if (it_map_addr_id != m_map_address_id.end()) {
*ap_id = it_map_addr_id->second;
find_success = true;
} else {
find_success = false;
}
return find_success;
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::address_get(id_type a_id,
address_type* ap_address)
{
bool find_success = true;
map_id_channel_iterator it_map_id_channel = m_map_id_channel.find(a_id);
if (it_map_id_channel != m_map_id_channel.end()) {
*ap_address = it_map_id_channel->second.address;
find_success = true;
} else {
find_success = false;
}
return find_success;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::erase(id_type a_id)
{
map_id_channel_iterator it_erase_channel =
m_map_id_channel.find(a_id);
if (it_erase_channel != m_map_id_channel.end()) {
if (m_it_cur_channel == it_erase_channel) {
if (m_it_cur_channel != m_map_id_channel.begin()) {
m_it_cur_channel--;
} else {
m_it_cur_channel = m_map_id_channel.end();
if (m_map_id_channel.size() > 1) {
m_it_cur_channel--;
} else {
// Òåêóùèé êàíàë îñòàâëÿåì íåîïðåäåëåííûì,
// òàê êàê óäàëÿåòñÿ ïîñëåäíèé êàíàë
}
}
} else {
// Óäàëÿåìûé êàíàë íå ñîâïàäàåò ñ òåêóùèì
}
if (m_it_cur_channel_for_check == it_erase_channel) {
if (m_it_cur_channel_for_check != m_map_id_channel.begin()) {
m_it_cur_channel_for_check--;
} else {
m_it_cur_channel_for_check = m_map_id_channel.end();
if (m_map_id_channel.size() > 1) {
m_it_cur_channel_for_check--;
} else {
// Òåêóùèé êàíàë îñòàâëÿåì íåîïðåäåëåííûì,
// òàê êàê óäàëÿåòñÿ ïîñëåäíèé êàíàë
}
}
} else {
// Óäàëÿåìûé êàíàë íå ñîâïàäàåò ñ òåêóùèì
}
map_address_id_iterator it_map_address_id =
m_map_address_id.find(it_erase_channel->second.address);
queue_id_type::iterator it_id =
find(m_id_list.begin(), m_id_list.end(), a_id);
m_map_id_channel.erase(it_erase_channel);
m_map_address_id.erase(it_map_address_id);
m_id_list.erase(it_id);
} else {
IRS_LIB_ASSERT_MSG("Êàíàë îòñóòñâóåò");
}
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::is_channel_exists(const id_type a_id)
{
return m_map_id_channel.find(a_id) != m_map_id_channel.end();
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::channel_buf_max_size_get() const
{
return m_buf_max_size;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::channel_buf_max_size_set(
size_type a_channel_buf_max_size)
{
m_buf_max_size = a_channel_buf_max_size;
//map_id_channel_iterator it_channel = m_map_id_channel.begin();
/*while (it_channel != m_map_id_channel.end()) {
it_channel->second.buffer.reserve(m_buf_max_size);
}*/
}
template <class address_t>
double irs::hardflow::udp_channel_list_t<address_t>::lifetime_get(
const id_type a_channel_id) const
{
double channel_lifetime = 0.;
map_id_channel_const_iterator it_channel =
m_map_id_channel.find(a_channel_id);
if (it_channel != m_map_id_channel.end()) {
channel_lifetime = it_channel->second.lifetime.get();
} else {
// Êàíàë îòñóòñâóåò
}
return channel_lifetime;
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::limit_lifetime_enabled_get() const
{
return m_max_lifetime_enabled;
}
template <class address_t>
double irs::hardflow::udp_channel_list_t<address_t>::max_lifetime_get() const
{
return CNT_TO_DBLTIME(m_max_lifetime);
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::max_lifetime_set(
double a_max_lifetime_sec)
{
m_max_lifetime = irs::make_cnt_s(a_max_lifetime_sec);
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::limit_lifetime_enabled_set(
bool a_limit_lifetime_enabled)
{
m_max_lifetime_enabled = a_limit_lifetime_enabled;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::downtime_timer_reset(
const id_type a_channel_id)
{
map_id_channel_iterator it_channel =
m_map_id_channel.find(a_channel_id);
if (it_channel != m_map_id_channel.end()) {
it_channel->second.downtime.start();
} else {
// Êàíàë îòñóòñâóåò
}
}
template <class address_t>
double irs::hardflow::udp_channel_list_t<address_t>::downtime_get(
const id_type a_channel_id) const
{
double channel_downtime = 0.;
map_id_channel_const_iterator it_channel =
m_map_id_channel.find(a_channel_id);
if (it_channel != m_map_id_channel.end()) {
channel_downtime = it_channel->second.downtime.get();
} else {
// Êàíàë îòñóòñâóåò
}
return channel_downtime;
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::limit_downtime_enabled_get() const
{
return m_max_downtime_enabled;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::limit_downtime_enabled_set(
bool a_limit_downtime_enabled)
{
m_max_downtime_enabled = a_limit_downtime_enabled;
}
template <class address_t>
double irs::hardflow::udp_channel_list_t<address_t>::max_downtime_get() const
{
return CNT_TO_DBLTIME(m_max_downtime);
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::max_downtime_set(
double a_max_downtime_sec)
{
m_max_downtime = irs::make_cnt_s(a_max_downtime_sec);
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::channel_address_get(
const id_type a_channel_id, address_type* ap_address)
{
map_id_channel_iterator it_channel = m_map_id_channel.find(a_channel_id);
if (it_channel != m_map_id_channel.end()) {
*ap_address = it_channel->second.address;
} else {
// Êàíàë íå íàéäåí
}
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::channel_address_set(
const id_type a_channel_id, const address_type& a_address)
{
map_id_channel_iterator it_channel = m_map_id_channel.find(a_channel_id);
if (it_channel != m_map_id_channel.end()) {
map_address_id_iterator it_map_addr_id = m_map_address_id.find(
it_channel->second.address);
if (it_map_addr_id != m_map_address_id.end()) {
it_channel->second.address = a_address;
it_channel->second.buffer.clear();
const id_type id = it_map_addr_id->second;
m_map_address_id.erase(it_map_addr_id);
m_map_address_id.insert(make_pair(a_address, id));
} else {
IRS_LIB_ASSERT_MSG("Êàíàë ñ òàêèì àäðåñîì äîëæåí ñóùåñòâîâàòü â "
"îáîèõ ñïèñêàõ");
}
} else {
// Êàíàë íå íàéäåí
}
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::clear()
{
m_map_id_channel.clear();
m_map_address_id.clear();
m_id_list.clear();
m_it_cur_channel = m_map_id_channel.end();
m_it_cur_channel_for_check = m_map_id_channel.end();
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::size()
{
return m_id_list.size();
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::mode_set(
const udp_limit_connections_mode_t a_mode)
{
m_mode = a_mode;
switch (m_mode) {
case udplc_mode_queue: {
if (m_id_list.size() > m_max_size) {
size_type address_count_need_delete = m_id_list.size() - m_max_size;
for (size_type id_i = 0; id_i < address_count_need_delete; id_i++) {
erase(m_id_list.front());
}
} else {
// Óäàëåíèå îáúåêòîâ íå òðåáóåòñÿ
}
} break;
case udplc_mode_limited: {
if (m_id_list.size() > m_max_size) {
size_type address_count_need_delete = m_id_list.size() - m_max_size;
for (size_type id_i = 0; id_i < address_count_need_delete; id_i++) {
erase(m_id_list.back());
}
} else {
// Óäàëåíèå îáúåêòîâ íå òðåáóåòñÿ
}
} break;
case udplc_mode_unlimited: {
// Óäàëåíèå íå òðåáóåòñÿ
} break;
default : {
IRS_ASSERT_MSG("Íåèçâåñòíûé òèï ðàáî÷åãî ðåæèìà");
}
}
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::max_size_get()
{
return m_max_size;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::max_size_set(
size_type a_max_size)
{
m_max_size = min(a_max_size, m_channel_max_count);
switch (m_mode) {
case udplc_mode_queue: {
if (m_id_list.size() > m_max_size) {
const size_type channel_erase_count = m_id_list.size() - m_max_size;
for (size_type channel_i = 0; channel_i < channel_erase_count;
channel_i++)
{
erase(m_id_list.front());
m_id_list.pop_front();
}
} else {
// Óäàëåíèå ëèøíèõ êàíàëîâ íå òðåáóåòñÿ
}
} break;
case udplc_mode_limited: {
const size_type channel_erase_count = m_id_list.size() - m_max_size;
for (size_type channel_i = 0; channel_i < channel_erase_count;
channel_i++)
{
erase(m_id_list.back());
m_id_list.pop_back();
}
} break;
case udplc_mode_unlimited: {
// Óäàëåíèå êàíàëîâ íå òðåáóåòñÿ
} break;
default : {
IRS_ASSERT_MSG("Íåèçâåñòíûé âàðèàíò ðàáî÷åãî ðåæèìà");
}
}
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::write(
const address_type& a_address, const irs_u8 *ap_buf, size_type a_size)
{
size_type write_byte_count = 0;
bool channel_exists = false;
id_type id = 0;
// Ïðîâåðÿåì íàëè÷èå êàíàëà è ñîçäàåì, åñëè åãî íå ñóùåñòâóåò
insert(a_address, &id, &channel_exists);
if (channel_exists) {
map_id_channel_iterator it_map_id_channel =
m_map_id_channel.find(id);
if (it_map_id_channel != m_map_id_channel.end()) {
buffer_type& buf = it_map_id_channel->second.buffer;
IRS_LIB_ASSERT(buf.size() <= m_buf_max_size);
write_byte_count = min(a_size, m_buf_max_size - buf.size());
buf.push_back(ap_buf, ap_buf + write_byte_count);
it_map_id_channel->second.downtime.start();
} else {
IRS_LIB_ASSERT_MSG("Êàíàë îòñóòñâóåò â ñïèñêå");
}
} else {
// Êàíàëà ñ òàêèì àäðåñîì íå ñóùåñòâóåò è ñîçäàòü åãî íå óäàëîñü
}
return write_byte_count;
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::read(size_type a_id,
irs_u8 *ap_buf, size_type a_size)
{
size_type read_byte_count = 0;
map_id_channel_iterator it_map_id_channel =
m_map_id_channel.find(a_id);
if (it_map_id_channel != m_map_id_channel.end()) {
buffer_type& buf = it_map_id_channel->second.buffer;
read_byte_count = min(buf.size(), a_size);
if (read_byte_count > 0) {
buf.copy_to(0, read_byte_count, ap_buf);
buf.pop_front(read_byte_count);
it_map_id_channel->second.downtime.start();
} else {
// Íåò äàííûõ â áóôåðå
}
} else {
// Ýòîãî êàíàëà íå ñóùåñòâóåò
}
return read_byte_count;
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::channel_next()
{
size_type cur_channel_id = invalid_channel;
if (m_it_cur_channel != m_map_id_channel.end()) {
m_it_cur_channel++;
} else {
// Òåêóùèé êàíàë íå óñòàíîâëåí
}
if (m_it_cur_channel != m_map_id_channel.end()) {
cur_channel_id = m_it_cur_channel->first;
} else {
m_it_cur_channel = m_map_id_channel.begin();
if (m_it_cur_channel != m_map_id_channel.end()) {
cur_channel_id = m_it_cur_channel->first;
} else {
// Íåò íè îäíîãî êàíàëà
}
}
return cur_channel_id;
}
template <class address_t>
typename irs::hardflow::udp_channel_list_t<address_t>::size_type
irs::hardflow::udp_channel_list_t<address_t>::cur_channel() const
{
id_type channel_id = invalid_channel;
if (m_it_cur_channel != m_map_id_channel.end()) {
channel_id = m_it_cur_channel->first;
} else {
// Òåêóùèé êàíàë íå óñòàíîâëåí
}
return channel_id;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::tick()
{
if (m_max_lifetime_enabled || m_max_downtime_enabled) {
if (m_it_cur_channel_for_check == m_map_id_channel.end()) {
m_it_cur_channel_for_check = m_map_id_channel.begin();
} else {
// Òåêóùèé êàíàë ïðàâèëüíûé
}
if (m_it_cur_channel_for_check != m_map_id_channel.end()) {
bool channel_need_destroy = false;
if (m_max_lifetime_enabled) {
if (lifetime_exceeded(m_it_cur_channel_for_check)) {
channel_need_destroy = true;
} else {
// Ìàêñèìàëüíîå âðåìÿ æèçíè íå ïðåâûøåíî
}
} else {
// Ïðîâåðêà âðåìåíè æèçíè îòêëþ÷åíà
}
if (m_max_downtime_enabled) {
if (downtime_exceeded(m_it_cur_channel_for_check)) {
channel_need_destroy = true;
} else {
// Ìàêñèìàëüíîå âðåìÿ áåçäåéñòâèå íå ïðåâûøåíî
}
} else {
// Ïðîâåðêà âðåìåíè áåçäåéñòâèÿ îòêëþ÷åíà
}
if (channel_need_destroy) {
erase(m_it_cur_channel_for_check->first);
} else {
// Âðåìÿ æèçíè è âðåìÿ áåçäåéñòâèÿ íå ïðåâûøåíî
m_it_cur_channel_for_check++;
}
} else {
// Íåò íè îäíîãî êàíàëà
IRS_LIB_ASSERT(m_map_id_channel.empty());
}
} else {
// Ïðîâåðêà âðåìåíè æèçíè è âðåìåíè áåçäåéñòâèÿ îòêëþ÷åíà
}
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::lifetime_exceeded(
const map_id_channel_iterator /*a_it_cur_channel*/)
{
bool lifetime_exceeded_status = false;
if (m_it_cur_channel_for_check != m_map_id_channel.end()) {
if (m_it_cur_channel_for_check->second.lifetime.get_cnt() > m_max_lifetime)
{
lifetime_exceeded_status = true;
} else {
// Ìàêñèìàëüíîå âðåìÿ æèçíè íå ïðåâûøåíî
}
} else {
// Íåäîïóñòèìûé êàíàë
}
return lifetime_exceeded_status;
}
template <class address_t>
bool irs::hardflow::udp_channel_list_t<address_t>::downtime_exceeded(
const map_id_channel_iterator /*a_it_cur_channel*/)
{
bool downtime_exceeded_status = false;
if (m_it_cur_channel_for_check != m_map_id_channel.end()) {
if (m_it_cur_channel_for_check->second.downtime.get_cnt() > m_max_downtime)
{
downtime_exceeded_status = true;
} else {
// Ìàêñèìàëüíîå âðåìÿ áåçäåéñòâèÿ íå ïðåâûøåíî
}
} else {
// Íåäîïóñòèìûé êàíàë
}
return downtime_exceeded_status;
}
template <class address_t>
void irs::hardflow::udp_channel_list_t<address_t>::next_free_channel_id()
{
if (!m_channel_id_overflow) {
m_channel_id++;
if (m_channel_id == invalid_channel) {
m_channel_id_overflow = true;
} else {
// Ïåðåïîëíåíèå íå ïðîèçîøëî
}
} else {
// Óæå áûëî ïåðåïîëíåíèå ñ÷åò÷èêà
}
if (m_channel_id_overflow) {
if (m_map_id_channel.size() < m_channel_max_count) {
if(m_channel_id == invalid_channel) {
m_channel_id++;
}
map_id_channel_iterator it_prev =
m_map_id_channel.find(m_channel_id);
map_id_channel_iterator it_cur = it_prev;
if(it_cur != m_map_id_channel.end()) {
while(true) {
it_cur++;
if(it_cur == m_map_id_channel.end()) {
m_channel_id = it_prev->first + 1;
if(m_channel_id == invalid_channel) {
m_channel_id++;
}
it_prev = m_map_id_channel.find(m_channel_id);
it_cur = it_prev;
if(it_cur == m_map_id_channel.end()) {
break;
}
} else if((it_cur->first - it_prev->first) > 1) {
m_channel_id = it_prev->first + 1;
break;
}
it_prev = it_cur;
}
} else {
// Òåêóùåå çíà÷åíèå èäåíòèôèêàòîðà ÿâëÿåòñÿ óíèêàëüíûì
}
} else {
IRS_LIB_HARDFLOWG_DBG_RAW_MSG_DETAIL(
"Íåò ñâîáîäíûõ ìåñò äëÿ íîâîãî êàíàëà" << endl);
m_channel_id = invalid_channel;
}
} else {
// Ïåðåïîëíåíèÿ ñ÷åò÷èêà íå áûëî
}
}
#endif //__ICCAVR__
#if defined(IRS_WIN32) || defined(IRS_LINUX)
#ifdef IRS_LIB_DEBUG
#define HARDFLOW_DBG_ERR(error_code)\
irs::send_format_msg(error_code,__FILE__,__LINE__)
#define HARDFLOW_DBG_MSG(error_code)\
irs::send_format_msg(error_code,__FILE__,__LINE__)
#else // IRS_LIB_DEBUG
#define HARDFLOW_DBG_ERR(error_code)
#define HARDFLOW_DBG_MSG(error_code)
#endif // IRS_LIB_DEBUG
//! \brief Êëàññ äëÿ ïîëó÷åíèÿ ïîñëåäíåé îøèáêè
//! \author Lyashchov Maxim
class error_sock_t
{
public:
#if defined(IRS_WIN32)
enum error_t {
eacess = WSAEACCES, //!< \brief Òîëüêî äëÿ Windows
eaddrenuse = WSAEADDRINUSE,
eaddrnotavail = WSAEADDRNOTAVAIL,
eafnosupport = WSAEAFNOSUPPORT,
econnaborned = WSAECONNABORTED,
econnrefused = WSAECONNREFUSED,
econnreset = WSAECONNRESET,
edestaddrreq = WSAEDESTADDRREQ,
efault = WSAEFAULT, //!< \brief Òîëüêî äëÿ Windows
ehostdown = WSAEHOSTDOWN,
ehostunread = WSAEHOSTUNREACH,
einprogress = WSAEINPROGRESS, //!< \brief Òîëüêî äëÿ Windows
eintr = WSAEINTR, //!< \brief Òîëüêî äëÿ Windows
eisconn = WSAEISCONN,
einval = WSAEINVAL, //!< \brief Òîëüêî äëÿ Windows
emfile = WSAEMFILE, //!< \brief Òîëüêî äëÿ Windows
emsgsize = WSAEMSGSIZE,
enetdown = WSAENETDOWN,
enetreset = WSAENETRESET,