-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirslwip.h
1413 lines (1317 loc) · 42.8 KB
/
irslwip.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
#ifndef LWIP_HARDFLOW_H
#define LWIP_HARDFLOW_H
#include <irsdefs.h>
#include <hardflowg.h>
#define USE_DHCP
#ifdef USE_LWIP
extern "C" {
#pragma diag_suppress=Pa181
#include <lwip/init.h>
#include <lwip/ip_addr.h>
#ifndef IRS_STM32H7xx
#include <lwip/timers.h>
#else // defined(IRS_STM32H7xx)
#include <lwip/timeouts.h>
#endif // IRS_STM32H7xx
#include <lwip/tcp.h>
#include <lwip/udp.h>
#include <lwip/mem.h>
#include <lwip/memp.h>
#include <lwip/dhcp.h>
#include <netif/etharp.h>
#pragma diag_default=Pa181
} // extern "C"
#endif // USE_LWIP
#if defined(IRS_STM32H7xx)// || defined(ARMxxx)
#define IRSLIB_USE_LWIP_CONTROL
#endif
#ifdef IRSLIB_USE_LWIP_CONTROL
#ifdef IRS_STM32H7xx
extern "C" {
#include "ethernet_h7.h"
}
#else
#error "Íå ïîäêëþ÷åí ôàéë ethernet äëÿ òåêóùåãî ìèêðîêîíòðîëëåðà"
#endif // include ethernet
#endif // IRSLIB_USE_LWIP_CONTROL
#include <irsfinal.h>
#ifdef USE_LWIP
#ifndef IRS_LIB_LWIP_ETH_DEBUG_TYPE
# define IRS_LIB_LWIP_ETH_DEBUG_TYPE IRS_LIB_DEBUG_NONE
#endif // IRS_LIB_LWIP_ETH_DEBUG_TYPE
# if (IRS_LIB_LWIP_ETH_DEBUG_TYPE == IRS_LIB_DEBUG_BASE)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BASE(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_BASE(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_SRC_BASE(msg) IRS_LIB_DBG_MSG_SRC(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_DETAIL(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_DETAIL(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_BASE(msg) msg
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_DETAIL(msg)
#elif (IRS_LIB_LWIP_ETH_DEBUG_TYPE == IRS_LIB_DEBUG_DETAIL)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BASE(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_BASE(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_SRC_BASE(msg) IRS_LIB_DBG_MSG_SRC(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_BASE(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_DETAIL(msg) IRS_LIB_DBG_RAW_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_DETAIL(msg) IRS_LIB_DBG_MSG(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_DETAIL(msg) msg
#else // IRS_LIB_LWIP_ETH_DEBUG_TYPE == IRS_LIB_DEBUG_NONE
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BASE(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_BASE(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_SRC_BASE(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_BASE(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_DETAIL(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_DETAIL(msg)
# define IRS_LIB_LWIP_ETH_DBG_MSG_SRC_DETAIL(msg)
# define IRS_LIB_LWIP_ETH_DBG_RAW_MSG_BLOCK_DETAIL(msg)
#endif // IRS_LIB_LWIP_ETH_DEBUG_TYPE == IRS_LIB_DEBUG_NONE
namespace irs {
namespace lwip {
class ethernet_t
{
public:
struct configuration_t
{
mxip_t ip;
mxip_t netmask;
mxip_t gateway;
bool dhcp_enabled;
configuration_t():
ip(mxip_t::any_ip()),
netmask(mxip_t::any_ip()),
gateway(mxip_t::any_ip()),
dhcp_enabled(false)
{
}
};
ethernet_t(simple_ethernet_t* ap_simple_ethernet,
const configuration_t& a_configuration);
~ethernet_t();
mxip_t get_ip();
mxip_t get_netmask();
mxip_t get_gateway();
netif* get_netif();
void tick();
private:
void start_dhcp();
void lwip_tick();
static err_t low_level_output(struct netif *ap_netif, struct pbuf *p);
err_t ethernetif_input();
static struct pbuf * low_level_input();
static err_t ethernetif_init(struct netif *ap_netif);
static void low_level_init(struct netif *ap_netif);
//static int accept_udp_port(u16_t a_port);
static simple_ethernet_t* mp_simple_ethernet;
netif m_netif;
loop_timer_t m_sys_check_timeouts_loop_timer;
loop_timer_t m_etharp_tmr_loop_timer;
#ifdef USE_DHCP
loop_timer_t m_dhcp_fine_tmr_loop_timer;
loop_timer_t m_dhcp_coarse_tmr_loop_timer;
#endif // USE_DHCP
};
#ifdef IRSLIB_USE_LWIP_CONTROL
//Ïåðåïèñàííûé èç ethernet_t êëàññ, êîòîðûé èñïîëüçóåò ïîäêëþ÷åííûé ôàéë
//ñ ðåàëèçàöèåé ethernet
class lwip_control_t
{
public:
struct config_t
{
mxip_t ip;
mxip_t netmask;
mxip_t gateway;
bool dhcp_enabled;
config_t():
ip(mxip_t::any_ip()),
netmask(mxip_t::any_ip()),
gateway(mxip_t::any_ip()),
dhcp_enabled(false)
{
}
};
lwip_control_t(const config_t& a_configuration);
~lwip_control_t();
mxip_t get_ip();
bool is_dhcp_ready();
mxip_t get_netmask();
mxip_t get_gateway();
netif* get_netif();
void tick();
mxmac_t get_mac();
private:
void start_dhcp();
void lwip_tick();
mxmac_t st_generate_mac(device_code_t a_device_code);
//static err_t low_level_output(struct netif *ap_netif, struct pbuf *p);
//err_t ethernetif_input();
//static struct pbuf * low_level_input();
//static err_t ethernetif_init(struct netif *ap_netif);
//static void low_level_init(struct netif *ap_netif);
mxmac_t m_mac;
netif m_netif;
loop_timer_t m_sys_check_timeouts_loop_timer;
loop_timer_t m_etharp_tmr_loop_timer;
loop_timer_t m_check_link_timer;
#ifdef USE_DHCP
loop_timer_t m_dhcp_fine_tmr_loop_timer;
loop_timer_t m_dhcp_coarse_tmr_loop_timer;
#endif // USE_DHCP
};
#endif // IRSLIB_USE_LWIP_CONTROL
} // namespace lwip
namespace hardflow {
namespace lwip {
class buffers_t
{
public:
typedef size_t size_type;
buffers_t(size_type a_buf_max_count = 2, size_type a_buf_max_size = 1500);
size_type write(size_type a_channel_id, const irs_u8* ap_pbuf,
size_type a_size);
template <class DataReader>
size_type write(size_type a_channel_id, DataReader a_data);
size_type available_for_write(size_type a_channel_id) const;
size_type available_for_write_to_new_channel() const;
size_type read(size_type a_channel_id, irs_u8* ap_buf, size_type a_size);
size_type available_for_read(size_type a_channel_id) const;
void free_buffer(size_type a_channel_id);
size_type channel_next_available_for_reading();
void reserve_buffers();
private:
irs::deque_data_t<irs_u8>* create_buf(size_type a_channel_id);
void insert_to_id_buf_pos(size_type a_channel_id, size_type a_pos);
void erase_from_id_buf_pos(size_type a_channel_id);
void erase_from_id_buf_pos(map<size_type, size_type>::iterator a_it);
size_type m_buf_max_count;
size_type m_buf_max_size;
vector<irs::deque_data_t<irs_u8> > m_buffers;
map<size_type, size_type> m_id_buf_pos;
vector<size_type> m_free_buffers;
map<size_type, size_type>::iterator m_channel_for_read;
};
template <class DataReader>
buffers_t::size_type
buffers_t::write(size_type a_channel_id, DataReader a_data)
{
if (a_data.size() == 0) {
return 0;
}
if (a_data.size() > m_buf_max_size) {
return 0;
}
irs::deque_data_t<irs_u8>* buf = create_buf(a_channel_id);
if (!buf) {
return 0;
}
IRS_LIB_ASSERT(buf->size() <= m_buf_max_size);
const size_type available_size = (m_buf_max_size - buf->size());
size_type size = min(available_size, a_data.size());
#ifndef IRS_NO_EXCEPTIONS
try {
#endif // IRS_NO_EXCEPTIONS
buf->reserve(buf->size() + a_data.size());
for (const irs_u8* start = a_data.data(); start != NULL;
start = a_data.next()) {
const irs_u8* end = start + a_data.data_size();
buf->push_back(start, end);
}
return size;
#ifndef IRS_NO_EXCEPTIONS
} catch (std::bad_alloc&) {
return 0;
}
#endif // IRS_NO_EXCEPTIONS
}
class pbuf_reader_t
{
public:
typedef size_t size_type;
pbuf_reader_t(const pbuf* ap_pbuf):
mp_pbuf(ap_pbuf)
{
}
size_type size() const
{
return mp_pbuf->tot_len;
}
const irs_u8* next()
{
if (!mp_pbuf) {
return NULL;
}
mp_pbuf = mp_pbuf->next;
return reinterpret_cast<const irs_u8*>(mp_pbuf->payload);
}
const irs_u8* data() const
{
if (!mp_pbuf) {
return NULL;
}
return reinterpret_cast<irs_u8*>(mp_pbuf->payload);
}
size_type data_size() const
{
if (!mp_pbuf) {
return 0;
}
return mp_pbuf->len;
}
private:
pbuf_reader_t();
const pbuf* mp_pbuf;
};
class tcp_server_t: public hardflow_t
{
public:
typedef std::size_t size_type;
struct configuration_t
{
//! \brief Ìàêñèìàëüíûé ðàçìåð áóôåðà êàíàëà
size_type channel_buffer_max_size;
//! \brief Ìàêñèìàëüíîå êîëè÷åñòâî áóôåðîâ êàíàëîâ, ìîæåò áûòü ìåíüøå ëèáî
//! ðàâíî êîëè÷åñòâó êàíàëîâ, çàäàâàåìîìó ÷åðåç ïàðàìåòð êîíñòðóêòîðà
//! a_channel_max_count
size_type channel_buffer_max_count;
//! \brief Ðåçåðâèðîâàòü ïàìÿòü äëÿ áóôåðîâ
bool reserve_buffers;
configuration_t():
channel_buffer_max_size(1500),
channel_buffer_max_count(2),
reserve_buffers(true)
{
}
};
tcp_server_t(const mxip_t& a_local_ip, irs_u16 a_local_port,
size_type a_channel_max_count = 3,
const configuration_t& a_configuration = configuration_t());
~tcp_server_t();
virtual string_type param(const string_type& a_param_name);
virtual void set_param(const string_type& a_param_name,
const string_type& a_value);
virtual size_type read(size_type a_channel_ident, irs_u8* ap_buf,
size_type a_size);
virtual size_type write(size_type a_channel_ident, const irs_u8* ap_buf,
size_type a_size);
virtual size_type channel_next();
virtual bool is_channel_exists(size_type a_channel_ident);
virtual void set_channel_switching_mode(channel_switching_mode_t a_mode);
virtual void tick();
bool listen(const mxip_t& a_ip, irs_u16 a_port);
private:
struct channel_t
{
tcp_server_t* server;
size_type id;
tcp_pcb* pcb;
bool marked_for_erase;
channel_t():
server(NULL),
id(0),
pcb(NULL),
marked_for_erase(false)
{
}
};
enum { pool_interval = 10 };
bool close_connection(channel_t* ap_channel);
channel_t* create_channel(tcp_pcb *ap_pcb);
void erase_channel(channel_t* ap_channel);
static err_t accept(void *arg, tcp_pcb *pcb, err_t err);
static err_t recv(void *arg, tcp_pcb *pcb, pbuf *p, err_t err);
static void conn_err(void *arg, err_t err);
static err_t poll(void *arg, tcp_pcb *pcb);
static err_t sent(void *arg, tcp_pcb *pcb, u16_t len);
tcp_pcb* mp_pcb;
std::deque<irs::handle_t<channel_t> > m_channels;
std::map<size_type, channel_t*> m_channels_map;
const size_type m_channel_max_count;
buffers_t m_buffers;
size_type m_current_processed_channel;
size_type m_current_channel;
channel_switching_mode_t m_mode;
size_type m_last_id;
static size_type m_read_channel;
};
class tcp_client_t: public hardflow_t
{
public:
struct configuration_t
{
//! \brief Ìàêñèìàëüíûé ðàçìåð áóôåðà
size_type buffer_max_size;
//! \brief Ðåçåðâèðîâàòü ïàìÿòü äëÿ áóôåðà
bool reserve_buffer;
configuration_t():
buffer_max_size(1500),
reserve_buffer(true)
{
}
};
tcp_client_t(const mxip_t& a_local_ip, irs_u16 a_local_port,
const mxip_t& a_dest_ip, irs_u16 a_dest_port,
const configuration_t& a_configuration = configuration_t());
virtual ~tcp_client_t();
virtual size_type read(size_type a_channel_ident, irs_u8 *ap_buf,
size_type a_size);
virtual size_type write(size_type a_channel_ident, const irs_u8 *ap_buf,
size_type a_size);
virtual void tick();
virtual string_type param(const string_type &a_param_name);
virtual void set_param(const string_type &a_param_name,
const string_type &a_param_value);
virtual size_type channel_next();
virtual bool is_channel_exists(size_type a_channel_ident);
private:
enum { pool_interval = 10 };
bool connect();
bool close_connection();
static err_t connected(void* arg, tcp_pcb* tpcb, err_t err);
static err_t recv(void *arg, tcp_pcb *pcb, pbuf *p, err_t err);
static void conn_err(void *arg, err_t err);
static err_t poll(void *arg, tcp_pcb *pcb);
static err_t sent(void *arg, tcp_pcb *pcb, u16_t len);
const mxip_t m_local_ip;
const irs_u16 m_local_port;
const mxip_t m_dest_ip;
const irs_u16 m_dest_port;
tcp_pcb* mp_pcb;
bool m_marked_for_erase;
size_type m_channel_id;
irs::deque_data_t<irs_u8> m_buffer;
size_type m_buffer_max_size;
bool m_need_connect;
bool m_connection_is_established;
timer_t m_delay_before_connect;
};
#if LWIP_UDP
#ifndef __ICCAVR__
template <class address_t>
class udp_channels_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;
measure_time_t lifetime;
measure_time_t downtime;
channel_t():
address(),
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 typename map<address_type, id_type>::const_iterator
map_address_id_const_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_channels_t(
const udp_limit_connections_mode_t a_mode = udplc_mode_queue,
const size_type a_max_size = 1000,
const size_type channel_buffer_max_count = 2,
const size_type a_channel_buf_max_size = 1500,
const bool reserve_buffers = false,
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_size_get(const address_type& a_address) const;
size_type channel_buf_size_get(const id_type a_id) const;
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);
template <class DataReader>
size_type write(const address_type& a_address, DataReader a_data);
size_type available_for_write(const address_type& a_address) const;
size_type read(size_type a_id, irs_u8 *ap_buf,
size_type a_size);
size_type channel_next();
size_type channel_next_available_for_reading();
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;
buffers_t m_buffers;
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>
udp_channels_t<address_t>::udp_channels_t(
const udp_limit_connections_mode_t a_mode,
const size_type a_max_size,
const size_type channel_buffer_max_count,
const size_type a_channel_buf_max_size,
const bool reserve_buffers,
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_buffers(channel_buffer_max_count, a_channel_buf_max_size),
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))
{
if (reserve_buffers) {
m_buffers.reserve_buffers();
}
}
template <class address_t>
void udp_channels_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 udp_channels_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 udp_channels_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 udp_channels_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_buffers.free_buffer(it_map_address_id->second);
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 udp_channels_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 udp_channels_t<address_t>::size_type
udp_channels_t<address_t>::channel_buf_size_get(
const address_type& a_address) const
{
map_address_id_const_iterator it = m_map_address_id.find(a_address);
if (it != m_map_address_id.end()) {
m_buffers.available_for_read(it->second);
}
return 0;
}
template <class address_t>
typename udp_channels_t<address_t>::size_type
udp_channels_t<address_t>::channel_buf_size_get(const id_type a_id) const
{
return m_buffers.available_for_read(a_id);
}
template <class address_t>
typename udp_channels_t<address_t>::size_type
udp_channels_t<address_t>::channel_buf_max_size_get() const
{
return m_buf_max_size;
}
/*template <class address_t>
void udp_channels_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.resize(m_buf_max_size);
}
}*/
template <class address_t>
double udp_channels_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 udp_channels_t<address_t>::limit_lifetime_enabled_get() const
{
return m_max_lifetime_enabled;
}
template <class address_t>
double udp_channels_t<address_t>::max_lifetime_get() const
{
return CNT_TO_DBLTIME(m_max_lifetime);
}
template <class address_t>
void udp_channels_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 udp_channels_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 udp_channels_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 udp_channels_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 udp_channels_t<address_t>::limit_downtime_enabled_get() const
{
return m_max_downtime_enabled;
}
template <class address_t>
void udp_channels_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 udp_channels_t<address_t>::max_downtime_get() const
{
return CNT_TO_DBLTIME(m_max_downtime);
}
template <class address_t>
void udp_channels_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 udp_channels_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 udp_channels_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_buffers.free_buffer(id);
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 udp_channels_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 udp_channels_t<address_t>::size_type
udp_channels_t<address_t>::size()
{
return m_id_list.size();
}
template <class address_t>
void udp_channels_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 udp_channels_t<address_t>::size_type
udp_channels_t<address_t>::max_size_get()
{
return m_max_size;
}
template <class address_t>
void udp_channels_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 {
// Óäàëåíèå ëèøíèõ êàíàëîâ íå òðåáóåòñÿ