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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
#![warn(missing_docs)]

// Copyright 2020 sacn Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//
// This file was created as part of a University of St Andrews Computer Science BSC Senior Honours Dissertation Project.

// Report point: There is no guarantees made by the protocol that different sources will have different names.
// As names are used to match universe discovery packets this means that if 2 sources have the same name it won't
// be clear which one is sending what universes as they will appear as one source.

// Report point: partially discovered sources are only marked as discovered when a full set of discovery packets has been
// received, if a discovery packet is received but there are more pages the source won't be discovered until all the pages are received.
// If a page is lost this therefore means the source update / discovery in its entirety will be lost - implementation detail.

/// Socket 2 used for the underlying UDP socket that sACN is sent over.
use socket2::{Domain, Protocol, SockAddr, Socket, Type};

/// Mass import as a very large amount of packet is used here (upwards of 20 items) and this is much cleaner.
use packet::{E131RootLayerData::*, *};

/// Same reasoning as for packet meaning all sacn errors are imported.
use error::errors::{ErrorKind::*, *};

/// The uuid crate is used for working with/generating UUIDs which sACN uses as part of the cid field in the protocol.
/// This is used for uniquely identifying sources when counting sequence numbers.
use uuid::Uuid;

use std::borrow::Cow;
use std::cmp::{max, Ordering};
use std::collections::HashMap;
use std::fmt;
use std::net::{Ipv4Addr, SocketAddr};
use std::time::{Duration, Instant};

/// Extra net imports required for the IPv6 handling on the linux side.
#[cfg(target_os = "linux")]
use std::net::{IpAddr, Ipv6Addr};

/// Constants required to detect if an IP is IPv4 or IPv6.
#[cfg(target_os = "linux")]
use libc::{AF_INET, AF_INET6};

/// The libc constants required are not available on many windows environments and therefore are hard-coded.
/// Defined as per https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
#[cfg(target_os = "windows")]
const AF_INET: i32 = 2;

/// Defined as per https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
#[cfg(target_os = "windows")]
const AF_INET6: i32 = 23;

/// The default size of the buffer used to receive E1.31 packets.
/// 1143 bytes is biggest packet required as per Section 8 of ANSI E1.31-2018, aligned to 64 bit that is 1144 bytes.
pub const RCV_BUF_DEFAULT_SIZE: usize = 1144;

/// DMX payload size in bytes (512 bytes of data + 1 byte start code).
pub const DMX_PAYLOAD_SIZE: usize = 513;

/// The default value of the process_preview_data flag.
const PROCESS_PREVIEW_DATA_DEFAULT: bool = false;

/// The default value of the announce_source_discovery flag.
/// Defaults to false based on the assumption that often receivers won't have any immediate response/checks to do on a source
/// announcing itself (every source does this approximately every 10 seconds as per the E131_DISCOVERY_INTERVAL).
const ANNOUNCE_SOURCE_DISCOVERY_DEFAULT: bool = false;

/// The default value of the announce_stream_termination flag.
/// Defaults to false based on the assumption that often receivers will want to ignore termination from a source based on there
/// being multiple possible sources.
const ANNOUNCE_STREAM_TERMINATION_DEFAULT: bool = false;

/// The default value of the announce_timeout flag.
///
const ANNOUNCE_TIMEOUT_DEFAULT: bool = false;

/// The sequence number assigned by the receiver to a new source before it has processed the sequence numbers of any data from that source.
///
/// This should be set to the value before the initial expected sequence number from a source. Can't do this using underflow as forbidden in Rust.
///
const INITIAL_SEQUENCE_NUMBER: u8 = 255;

/// If a packet for a universe is waiting to be synchronised and then another packet is received with the same universe and synchronisation address
/// this situation must be handled. By default the implementation discards the lowest priority packet and if equal priority it discards the oldest
/// packet as per ANSI E1.31-2018 Section 6.2.3.
///
/// This can be changed by providing a new function to handle the situation of the user implementing a custom merge/arbitration algorithm as per
/// ANSI E1.31-2018 Section 6.2.3.2.
///
const DEFAULT_MERGE_FUNC: fn(&DMXData, &DMXData) -> Result<DMXData> =
    discard_lowest_priority_then_previous;

/// Holds a universes worth of DMX data.
#[derive(Debug)]
pub struct DMXData {
    /// The universe that the data was sent to.
    pub universe: u16,
    
    /// The actual universe data, if less than 512 values in length then implies trailing 0's to pad to a full-universe of data.
    pub values: Vec<u8>,

    /// The universe the data is (or was if now acted upon) waiting for a synchronisation packet from.
    /// 0 indicates it isn't waiting for a universe synchronisation packet.
    pub sync_uni: u16,

    /// The priority of the data, this may be useful for receivers which want to implement their own implementing merge algorithms.
    /// Must be less than packet::E131_MAX_PRIORITY.
    pub priority: u8,

    /// The unique id of the source of the data, this may be useful for receivers which want to implement their own merge algorithms
    /// which use the identity of the source to decide behaviour.
    /// A value of None indicates that there is no clear source, for example if a merge algorithm has merged data from 2 or more sources together.
    pub src_cid: Option<Uuid>,

    /// Indicates if the data is marked as 'preview' data indicating it is for use by visualisers etc. as per ANSI E1.31-2018 Section 6.2.6.
    pub preview: bool,

    /// The timestamp that the data was received.
    pub recv_timestamp: Instant,
}

/// Allows receiving dmx or other (different startcode) data using sacn.
/// 
/// # Examples
///
/// ```
/// // Example showing creation of a receiver and receiving some data, as there is no sender this receiver then handles the timeout.
/// use sacn::receive::SacnReceiver;
/// use sacn::packet::ACN_SDT_MULTICAST_PORT;
/// 
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
/// use std::time::Duration;
/// 
/// const UNIVERSE1: u16 = 1;
/// const TIMEOUT: Option<Duration> = Some(Duration::from_secs(1)); // A timeout of None means blocking behaviour, some indicates the actual timeout.
/// 
/// let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
///
/// let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();
///
/// dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();
/// 
/// match dmx_rcv.recv(TIMEOUT) {
///     Err(e) => {
///         // Print out the error.
///         println!("{:?}", e);
///     }
///     Ok(p) => {
///         // Print out the packet.
///         println!("{:?}", p);
///     }
/// }
/// ```
pub struct SacnReceiver {
    /// The SacnNetworkReceiver used for handling communication with UDP / Network / Transport layer.
    receiver: SacnNetworkReceiver,

    /// Data that hasn't been passed up yet as it is waiting e.g. due to universe synchronisation.
    /// Key is the universe. A receiver may not have more than one packet waiting per data_universe.
    /// Data_universe used as key as oppose to sync universe because multiple packets might be waiting on the same sync universe
    /// and adding data by data universe is at least as common as retrieving data by sync address because in a normal setup
    /// 1 or more bits of data wait for 1 sync.
    waiting_data: HashMap<u16, DMXData>,

    /// Universes that this receiver is currently listening for.
    universes: Vec<u16>,

    /// Sacn sources that have been discovered by this receiver through universe discovery packets.
    discovered_sources: Vec<DiscoveredSacnSource>,

    /// The merge function used by this receiver if DMXData for the same universe and synchronisation universe is received while there
    /// is already DMXData waiting for that universe and synchronisation address.
    merge_func: fn(&DMXData, &DMXData) -> Result<DMXData>,

    /// Sacn sources that have been partially discovered by only some of their universes being discovered so far with more pages to go.
    partially_discovered_sources: Vec<DiscoveredSacnSource>,

    /// The limit to the number of sources for which to track sequence numbers.
    /// A new source after this limit will cause a SourcesExceededError as per ANSI E1.31-2018 Section 6.2.3.3.
    source_limit: Option<usize>,

    /// The sequence numbers being tracked by this receiver for each packet type, source and universe.
    sequences: SequenceNumbering,

    /// Flag that indicates if this receiver should process packets marked as preview data.
    /// If true then the receiver will process theses packets.
    /// Returned data contains a flag to indicate if it is preview_data which can be used by the implementer to use/discard as required.
    process_preview_data: bool,

    /// Flag which indicates if a SourceDiscovered error should be thrown when receiving data and a source is discovered.
    announce_source_discovery: bool,

    /// Flag which indicates if a StreamTerminated error should be thrown if a receiver receives a stream terminated packet.
    announce_stream_termination: bool,

    /// Flag which indicates if an UniverseTimeout error should be thrown if it is detected that a source has timed out.
    announce_timeout: bool,
}

/// Represents an sACN source/sender on the network that has been discovered by this sACN receiver by receiving universe discovery packets.
#[derive(Clone, Debug)]
pub struct DiscoveredSacnSource {
    /// The name of the source, no protocol guarantee this will be unique but if it isn't then universe discovery may not work correctly.
    pub name: String,

    /// The time at which the discovered source was last updated / a discovery packet was received by the source.
    pub last_updated: Instant,

    /// The pages that have been sent so far by this source when enumerating the universes it is currently sending on.
    pages: Vec<UniversePage>,

    /// The last page that will be sent by this source.
    last_page: u8,
}

/// Used for receiving dmx or other data on a particular universe using multicast.
#[derive(Debug)]
struct SacnNetworkReceiver {
    /// The underlying UDP network socket used.
    socket: Socket,

    /// The address that this SacnNetworkReceiver is bound to.
    addr: SocketAddr,

    /// If true then this receiver supports multicast, is false then it does not.
    /// This flag is set when the receiver is created as not all environments currently support IP multicast.
    /// E.g. IPv6 Windows IP Multicast is currently unsupported.
    is_multicast_enabled: bool,
}

/// Universe discovery packets are broken down into pages to allow sending a large list of universes, each page contains a list of universes and
/// which page it is. The receiver then puts the pages together to get the complete list of universes that the discovered source is sending on.
///
/// The concept of pages is intentionally hidden from the end-user of the library as they are a way of fragmenting large discovery
/// universe lists so that they can work over the network and don't play any part out-side of the protocol.
///
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Debug)]
struct UniversePage {
    /// The page number of this page.
    page: u8,

    /// The universes that the source is transmitting that are on this page, this may or may-not be a complete list of all universes being sent
    /// depending on if there are more pages.
    universes: Vec<u16>,
}

/// Allows debug ({:?}) printing of the SacnReceiver, used during debugging.
///  
impl fmt::Debug for SacnReceiver {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.receiver)?;
        write!(f, "{:?}", self.waiting_data)?;
        write!(f, "{:?}", self.universes)?;
        write!(f, "{:?}", self.discovered_sources)?;
        write!(f, "{:?}", self.partially_discovered_sources)
    }
}

impl SacnReceiver {
    /// Creates a new SacnReceiver.
    ///
    /// SacnReceiverInternal is used for actually receiving the sACN data but is wrapped in SacnReceiver to allow the update thread to handle
    /// timeout etc.
    ///
    /// By default for an IPv6 address this will only receive IPv6 data but IPv4 can also be enabled by calling set_ipv6_only(false).
    /// A receiver with an IPv4 address will only receive IPv4 data.
    ///
    /// IPv6 multicast is unsupported on Windows in Rust. This is due to the underlying library (Socket2) not providing support.
    /// Since UniverseDiscovery is primarily based around multicast to receive the UniverseDiscovery packets this mechanism is expected
    /// to have limited usage when running in an Ipv6 Windows environment. The is_multicast_enabled method can be used to see if multicast
    /// is enabled or not.
    ///
    /// Arguments:
    ///     ip: The address of the interface for this receiver to join, by default this address should use the ACN_SDT_MULTICAST_PORT as defined in
    ///         ANSI E1.31-2018 Appendix A: Defined Parameters (Normative) however another address might be used in some situations.
    ///     source_limit: The limit to the number of sources, past this limit a new source will cause a SourcesExceededError as per ANSI E1.31-2018 Section 6.2.3.3.
    ///                     A source limit of None means no limit to the number of sources.
    ///
    /// # Errors
    /// Will return an InvalidInput error if the source_limit has a value of Some(0) which would indicate this receiver can never receive from any source.
    ///
    /// Will return an error if the SacnReceiver fails to bind to a socket with the given ip.
    /// For more details see socket2::Socket::new().
    ///
    /// Will return an error if the created SacnReceiver fails to listen to the E1.31_DISCOVERY_UNIVERSE.
    /// For more details see SacnReceiver::listen_universes().
    ///
    pub fn with_ip(ip: SocketAddr, source_limit: Option<usize>) -> Result<SacnReceiver> {
        match source_limit {
            Some(x) => {
                if x == 0 {
                    bail!(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Source_limit has a value of Some(0) which would indicate this receiver can never receive from any source"));
                }
            }
            None => {}
        }
        let mut sri = SacnReceiver {
            receiver: SacnNetworkReceiver::new(ip)?,
            waiting_data: HashMap::new(),
            universes: Vec::new(),
            discovered_sources: Vec::new(),
            merge_func: DEFAULT_MERGE_FUNC,
            partially_discovered_sources: Vec::new(),
            process_preview_data: PROCESS_PREVIEW_DATA_DEFAULT,
            source_limit: source_limit,
            sequences: SequenceNumbering::new(),
            announce_source_discovery: ANNOUNCE_SOURCE_DISCOVERY_DEFAULT,
            announce_stream_termination: ANNOUNCE_STREAM_TERMINATION_DEFAULT,
            announce_timeout: ANNOUNCE_TIMEOUT_DEFAULT,
        };

        sri.listen_universes(&[E131_DISCOVERY_UNIVERSE])?;

        Ok(sri)
    }

    /// Sets the value of the is_multicast_enabled flag to the given value.
    ///
    /// If set to false then the receiver won't attempt to join any more multicast groups.
    ///
    /// This method does not attempt to leave multicast groups already joined through previous listen_universe calls.
    ///
    /// # Arguments
    /// val: The new value for the is_multicast_enabled flag.
    ///
    /// # Errors
    /// Will return an OsOperationUnsupported error if attempting to set the flag to true in an environment that multicast
    /// isn't supported i.e. Ipv6 on Windows.
    ///
    pub fn set_is_multicast_enabled(&mut self, val: bool) -> Result<()> {
        self.receiver.set_is_multicast_enabled(val)
    }

    /// Returns true if multicast is enabled on this receiver and false if not.
    /// This flag is set when the receiver is created as not all environments currently support IP multicast.
    /// E.g. IPv6 Windows IP Multicast is currently unsupported.
    ///
    pub fn is_multicast_enabled(&self) -> bool {
        self.receiver.is_multicast_enabled()
    }

    /// Wipes the record of discovered and sequence number tracked sources.
    /// This is one way to handle a sources exceeded condition.
    ///
    /// If you want to wipe data awaiting synchronisation then see (clear_all_waiting_data)[clear_all_waiting_data].
    ///
    pub fn reset_sources(&mut self) {
        self.sequences.clear();
        self.partially_discovered_sources.clear();
        self.discovered_sources.clear();
    }

    /// Deletes all data currently waiting to be passed up - e.g. waiting for a synchronisation packet.
    ///
    /// This allows clearing all data awaiting synchronisation but without forgetting sequence numbers. To wipe sequence numbers
    /// and discovered sources see (reset_sources)[reset_sources].
    ///
    /// To clear only a specific universe of waiting data see (clear_waiting_data)[clear_waiting_data].
    ///
    pub fn clear_all_waiting_data(&mut self) {
        self.waiting_data.clear();
    }

    /// Clears data (if any) waiting to be passed up for the specific universe.
    ///
    /// Returns true if data was removed and false if there wasn't any data to remove for this universe.
    ///
    /// # Arguments
    /// universe: The universe that the data that is waiting was sent to.
    ///
    pub fn clear_waiting_data(&mut self, universe: u16) -> bool {
        self.waiting_data.remove(&universe).is_some()
    }

    /// Sets the merge function to be used by this receiver.
    ///
    /// This merge function is called if data is waiting for a universe e.g. for synchronisation and then further data for that universe with the same
    /// synchronisation address arrives.
    ///
    /// This merge function MUST return a DmxMergeError if there is a problem merging. This error can optionally encapsulate further errors using the Error-chain system
    ///     to provide a more informative backtrace.
    ///
    /// Arguments:
    /// func: The merge function to use. Should take 2 DMXData references as arguments and return a Result<DMXData>.
    ///
    pub fn set_merge_fn(&mut self, func: fn(&DMXData, &DMXData) -> Result<DMXData>) -> Result<()> {
        self.merge_func = func;
        Ok(())
    }

    /// Allow only receiving on Ipv6.
    pub fn set_ipv6_only(&mut self, val: bool) -> Result<()> {
        self.receiver.set_only_v6(val)
    }

    /// Allows receiving from the given universe and starts listening to the multicast addresses which corresponds to the given universe.
    ///
    /// Note that if the is_multicast_enabled flag is set to false then this method will only register the universe to listen to and won't
    /// attempt to join any multicast groups.
    ///
    /// If 1 or more universes in the list are already being listened to this method will have no effect for those universes only.
    ///
    /// # Errors
    /// Returns an ErrorKind::IllegalUniverse error if the given universe is outwith the allowed range of universes,
    /// see (is_universe_in_range)[fn.is_universe_in_range.packet].
    ///
    ///
    ///
    pub fn listen_universes(&mut self, universes: &[u16]) -> Result<()> {
        for u in universes {
            is_universe_in_range(*u)?;
        }

        for u in universes {
            match self.universes.binary_search(u) {
                Err(i) => {
                    // Value not found, i is the position it should be inserted
                    self.universes.insert(i, *u);

                    if self.is_multicast_enabled() {
                        self.receiver.listen_multicast_universe(*u)?;
                    }
                }
                Ok(_) => { // If value found then don't insert to avoid duplicates.
                }
            }
        }

        Ok(())
    }

    /// Stops listening to the given universe.
    ///
    /// # Errors
    /// Returns an ErrorKind::IllegalUniverse error if the given universe is outwith the allowed range of universes,
    /// see (is_universe_in_range)[fn.is_universe_in_range.packet].
    ///
    /// Returns UniverseNotFound if the given universe wasn't already being listened to.
    ///
    pub fn mute_universe(&mut self, universe: u16) -> Result<()> {
        is_universe_in_range(universe)?;

        match self.universes.binary_search(&universe) {
            Err(_) => {
                // Universe isn't found.
                bail!(ErrorKind::UniverseNotFound(
                    "Attempted to mute a universe that wasn't already being listened to"
                        .to_string()
                ));
            }
            Ok(i) => {
                // If value found then don't insert to avoid duplicates.
                self.universes.remove(i);
                self.receiver.mute_multicast_universe(universe)
            }
        }
    }

    /// Set the process_preview_data flag to the given value.
    ///
    /// This flag indicates if this receiver should process packets marked as preview_data or should ignore them.
    ///
    /// Argument:
    /// val: The new value of process_preview_data flag.
    pub fn set_process_preview_data(&mut self, val: bool) {
        self.process_preview_data = val;
    }

    /// Checks if this receiver is currently listening to the given universe.
    ///
    /// A receiver is 'listening' to a universe if it allows that universe to be received without filtering it out.
    /// This does not mean that the multicast address for that universe is or isn't being listened to.
    ///
    /// Arguments:
    /// universe: The sACN universe to check
    ///
    /// Returns:
    /// True if the universe is being listened to by this receiver, false if not.
    pub fn is_listening(&self, universe: &u16) -> bool {
        self.universes.contains(universe)
    }

    /// Attempt to receive data from any of the registered universes.
    /// This is the main method for receiving data.
    /// Any data returned will be ready to act on immediately i.e. waiting e.g. for universe synchronisation
    /// is already handled.
    ///
    /// # Errors
    /// This method will return a WouldBlock (unix) or TimedOut (windows) error if there is no data ready within the given timeout.
    /// A timeout of duration 0 will do timeout checks but otherwise will return a WouldBlock/TimedOut error without checking for data.
    ///
    /// Will return ErrorKind::SourceDiscovered error if the announce_source_discovery flag is set and a universe discovery
    /// packet is received and a source fully discovered.
    ///
    /// Will return a UniverseNotRegistered error if this method is called with an infinite timeout, no
    /// registered data universes and the announce_discovered_sources flag set to off. This is to protect the user from
    /// making this mistake leading to the method never being able to return.
    ///
    /// The method may also return an error if there is an issue setting a timeout on the receiver. See
    /// SacnNetworkReceiver::set_timeout for details.
    ///
    /// The method may also return an error if there is an issue handling the data as either a Data, Synchronisation or Discovery packet.
    /// See the SacnReceiver::handle_data_packet, SacnReceiver::handle_sync_packet and SacnReceiver::handle_universe_discovery_packet methods
    /// for details.
    ///
    /// If the announce_timeout flag is set then the recv will return a UniverseTimeout error if a source fails to send on a universe within the timeout
    /// specified by E131_NETWORK_DATA_LOSS_TIMEOUT (ANSI E1.31-2018 Appendix A).  This may not be detected immediately unless data is received for the timed-out
    /// universe from the source. If it isn't detected immediately it will be detected within an interval of E131_NETWORK_DATA_LOSS_TIMEOUT (assuming code
    /// executes in zero time).
    ///
    pub fn recv(&mut self, timeout: Option<Duration>) -> Result<Vec<DMXData>> {
        if self.universes.len() == 1
            && self.universes[0] == E131_DISCOVERY_UNIVERSE
            && timeout.is_none()
            && !self.announce_source_discovery
        {
            // This indicates that the only universe that can be received is the discovery universe.
            // This means that having no timeout may lead to no data ever being received and so this method blocking forever
            // to prevent this likely unintended behaviour throw a universe not registered error.
            bail!(ErrorKind::UniverseNotRegistered("Attempting to receive data with no data universes registered, an infinite timeout and no discovery announcements".to_string()));
        }

        self.sequences.check_timeouts(self.announce_timeout)?;
        self.check_waiting_data_timeouts();

        if timeout == Some(Duration::from_secs(0)) {
            if cfg!(target_os = "windows") {
                // Use the right expected error for the operating system.
                bail!(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    "No data available in given timeout"
                ));
            } else {
                bail!(std::io::Error::new(
                    std::io::ErrorKind::WouldBlock,
                    "No data available in given timeout"
                ));
            }
        }

        // Forces the actual timeout used for receiving from the underlying network to never exceed E131_NETWORK_DATA_LOSS_TIMEOUT.
        // This means that the timeouts for the sequence numbers will be checked at least every E131_NETWORK_DATA_LOSS_TIMEOUT even if
        // recv is called with a longer timeout.
        let actual_timeout =
            if timeout.is_some() && timeout.unwrap() < E131_NETWORK_DATA_LOSS_TIMEOUT {
                timeout
            } else {
                Some(E131_NETWORK_DATA_LOSS_TIMEOUT)
            };

        self.receiver
            .set_timeout(actual_timeout)
            .chain_err(|| "Failed to sent a timeout value for the receiver")?;
        let start_time = Instant::now();

        let mut buf: [u8; RCV_BUF_DEFAULT_SIZE] = [0; RCV_BUF_DEFAULT_SIZE];
        match self.receiver.recv(&mut buf) {
            Ok(pkt) => {
                let pdu: E131RootLayer = pkt.pdu;
                let data: E131RootLayerData = pdu.data;
                let res = match data {
                    DataPacket(d) => self.handle_data_packet(pdu.cid, d)?,
                    SynchronizationPacket(s) => self.handle_sync_packet(pdu.cid, s)?,
                    UniverseDiscoveryPacket(u) => {
                        let discovered_src: Option<String> =
                            self.handle_universe_discovery_packet(u);
                        if discovered_src.is_some() && self.announce_source_discovery {
                            bail!(ErrorKind::SourceDiscovered(discovered_src.unwrap()));
                        } else {
                            None
                        }
                    }
                };

                match res {
                    Some(r) => Ok(r),
                    None => {
                        // Indicates that there is no data ready to pass up yet even if a packet was received.
                        // To stop recv blocking forever with a non-None timeout due to packets being received consistently (that reset the timeout)
                        // within the receive timeout (e.g. universe discovery packets if the discovery interval < timeout) the timeout needs to be
                        // adjusted to account for the time already taken.
                        if !timeout.is_none() {
                            let elapsed = start_time.elapsed();
                            match timeout.unwrap().checked_sub(elapsed) {
                                None => {
                                    // Indicates that elapsed is bigger than timeout so its time to return.
                                    bail!(std::io::Error::new(
                                        std::io::ErrorKind::WouldBlock,
                                        "No data available in given timeout"
                                    ));
                                }
                                Some(new_timeout) => return self.recv(Some(new_timeout)),
                            }
                        } else {
                            // If the timeout was none then would keep looping till data is returned as the method should keep blocking till then.
                            self.recv(timeout)
                        }
                    }
                }
            }
            Err(err) => {
                match err.kind() {
                    &ErrorKind::Io(ref s) => {
                        match s.kind() {
                            // Windows and Unix use different error types (WouldBlock/TimedOut) for the same error.
                            std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                                if !timeout.is_none() {
                                    let elapsed = start_time.elapsed();
                                    match timeout.unwrap().checked_sub(elapsed) {
                                        None => {
                                            // Indicates that elapsed is bigger than timeout so its time to return.
                                            if cfg!(target_os = "windows") {
                                                // Use the right expected error for the operating system.
                                                bail!(std::io::Error::new(
                                                    std::io::ErrorKind::TimedOut,
                                                    "No data available in given timeout"
                                                ));
                                            } else {
                                                bail!(std::io::Error::new(
                                                    std::io::ErrorKind::WouldBlock,
                                                    "No data available in given timeout"
                                                ));
                                            }
                                        }
                                        Some(new_timeout) => return self.recv(Some(new_timeout)),
                                    }
                                } else {
                                    // If the timeout was none then would keep looping till data is returned as the method should keep blocking till then.
                                    self.recv(timeout)
                                }
                            }
                            _ => {
                                // Not a timeout/wouldblock error meaning the recv should stop with the given error.
                                Err(err)
                            }
                        }
                    }
                    _ => {
                        // Not a timeout/wouldblock error meaning the recv should stop with the given error.
                        Err(err)
                    }
                }
            }
        }
    }

    /// Returns the current value of the announce_source_discovery flag.
    /// See (set_announce_source_discovery)[receive::set_announce_source_discovery] for an explanation of the flag.
    pub fn get_announce_source_discovery(&self) -> bool {
        self.announce_source_discovery
    }

    /// Gets all discovered sources without checking if any are timed out.
    /// As the sources may be timed out get_discovered_sources is the preferred method but this is included
    /// to allow receivers to disable universe discovery source timeouts which may be useful in very high latency networks.
    pub fn get_discovered_sources_no_check(&mut self) -> Vec<DiscoveredSacnSource> {
        self.discovered_sources.clone()
    }

    /// Returns a list of the sources that have been discovered on the network by this receiver through the E1.31 universe discovery mechanism.
    pub fn get_discovered_sources(&mut self) -> Vec<DiscoveredSacnSource> {
        self.remove_expired_sources();
        self.discovered_sources.clone()
    }

    /// Sets the value of the announce_source_discovery flag to the given value.
    ///
    /// By default this flag is false which indicates that when receiving data discovered sources through universe discovery
    ///  won't be announced by the recv method and the receivers list of discovered universes will be updated silently.
    /// If set to true then it means that a SourceDiscovered error will be thrown whenever a source is discovered through a
    ///  complete universe discovery packet.
    ///
    /// # Arguments:
    /// new_val: The new value for the announce_source_discovery flag.
    ///
    pub fn set_announce_source_discovery(&mut self, new_val: bool) {
        self.announce_source_discovery = new_val;
    }

    /// Returns the current value of the announce_timeout flag.
    /// See (set_announce_timeout)[set_announce_timeout] for an explanation of the flag.
    pub fn get_announce_timeout(&self) -> bool {
        self.announce_timeout
    }

    /// Sets the value of the announce_timeout flag to the given value.
    ///
    /// By default this flag is false which means that if a universe for a source times out due to data not being sent then
    /// this will be updated on the receiver silently.
    /// If set to true then a UniverseTimeout error will be thrown when attempting to receive if it is detected that a source universe has
    /// timed out as per ANSI E1.31-2018 Section 6.7.1.
    ///
    /// # Arguments:
    /// new_val: The new value for the announce_timeout flag.
    ///
    pub fn set_announce_timeout(&mut self, new_val: bool) {
        self.announce_timeout = new_val;
    }

    /// Returns the current value of the announce_stream_termination flag.
    /// See (set_announce_stream_termination)[set_announce_stream_termination] for an explanation of the flag.
    pub fn get_announce_stream_termination(&self) -> bool {
        self.announce_stream_termination
    }

    /// Sets the value of the announce_stream_termination flag to the given value.
    ///
    /// By default this flag is false. This indicates that if a source sends a stream termination packet it will be handled silently by the receiver.
    /// If set to true then a UniverseTermination error will be thrown when attempting to receive if a termination packet is received as per
    /// ANSI E1.31-2018 Section 6.2.6.
    ///
    pub fn set_announce_stream_termination(&mut self, new_val: bool) {
        self.announce_stream_termination = new_val;
    }

    /// Handles the given data packet for this DMX receiver.
    ///
    /// Returns the universe data if successful.
    /// If the returned value is None it indicates that the data was received successfully but isn't ready to act on.
    ///
    /// Synchronised data packets handled as per ANSI E1.31-2018 Section 6.2.4.1.
    ///
    /// Arguments:
    /// data_pkt: The sACN data packet to handle.
    ///
    /// # Errors
    /// Returns an OutOfSequence error if a packet is received out of order as detected by the different between
    /// the packets sequence number and the expected sequence number as specified in ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    ///
    /// Returns a UniversesTerminated error if a packet is received with the stream_terminated flag set indicating that the source is no longer
    /// sending on that universe and the announce_stream_termination_flag is set to true.
    ///
    /// Will return an DmxMergeError if there is an issue merging or replacing new and existing waiting data.
    ///
    fn handle_data_packet(
        &mut self,
        cid: Uuid,
        data_pkt: DataPacketFramingLayer,
    ) -> Result<Option<Vec<DMXData>>> {
        if data_pkt.preview_data && !self.process_preview_data {
            // Don't process preview data unless receiver has process_preview_data flag set.
            return Ok(None);
        }

        if data_pkt.stream_terminated {
            self.terminate_stream(cid, data_pkt.source_name, data_pkt.universe);
            if self.announce_stream_termination {
                bail!(ErrorKind::UniverseTerminated(cid, data_pkt.universe));
            }
            return Ok(None);
        }

        if !self.is_listening(&data_pkt.universe) {
            return Ok(None); // If not listening for this universe then ignore the packet.
        }

        // Preview data and stream terminated both get precedence over checking the sequence number.
        // This is as per ANSI E1.31-2018 Section 6.2.6, Stream_Terminated: Bit 6, 'Any property values
        // in an E1.31 Data Packet containing this bit shall be ignored'

        self.sequences.check_data_seq_number(
            self.source_limit,
            cid,
            data_pkt.sequence_number,
            data_pkt.universe,
            self.announce_timeout,
        )?;

        if data_pkt.synchronization_address == E131_NO_SYNC_ADDR {
            self.clear_waiting_data(data_pkt.universe);

            let vals: Vec<u8> = data_pkt.data.property_values.into_owned();
            let dmx_data: DMXData = DMXData {
                universe: data_pkt.universe,
                values: vals.to_vec(),
                sync_uni: data_pkt.synchronization_address,
                priority: data_pkt.priority,
                src_cid: Some(cid),
                preview: data_pkt.preview_data,
                recv_timestamp: Instant::now(),
            };

            return Ok(Some(vec![dmx_data]));
        } else {
            // As per ANSI E1.31-2018 Appendix B.2 the receiver should listen at the synchronisation address when a data packet is received with a non-zero
            // synchronisation address.
            self.listen_universes(&[data_pkt.synchronization_address])?;

            let vals: Vec<u8> = data_pkt.data.property_values.into_owned();
            let dmx_data: DMXData = DMXData {
                universe: data_pkt.universe,
                values: vals.to_vec(),
                sync_uni: data_pkt.synchronization_address,
                priority: data_pkt.priority,
                src_cid: Some(cid),
                preview: data_pkt.preview_data,
                recv_timestamp: Instant::now(),
            };

            self.store_waiting_data(dmx_data)?;
            Ok(None)
        }
    }

    /// Removes the given universe from the discovered sACN source with the given name, also stops tracking
    /// sequence numbers for that universe / sender combination.
    ///
    /// Note this is just a record keeping operation, it doesn't actually effect the real sACN sender it
    /// just updates the record of what universes are expected on this receiver.
    ///
    /// If the src_cid/source_name/universe isn't currently registered then this method has no effect.
    /// This is intentional as it allows calling this function multiple times without worrying about failure because
    /// it comes to the same result.
    ///     E.g. when a source terminates it sends 3 termination packets but a receiver should only terminate once.
    ///
    /// # Arguments:
    ///
    /// src_cid: The CID of the source which is terminating a universe.
    ///
    /// source_name: The human readable name of the sACN source to remove the universe from.
    ///
    /// universe:    The sACN universe to remove.
    ///
    fn terminate_stream<'a>(&mut self, src_cid: Uuid, source_name: Cow<'a, str>, universe: u16) {
        match self.sequences.remove_seq_numbers(src_cid, universe) {
            _ => {
                // Will only return an error if the source/universe wasn't found which is acceptable because as it
                // comes to the same result.
            }
        }

        match find_discovered_src(&self.discovered_sources, &source_name.to_string()) {
            Some(index) => {
                self.discovered_sources[index].terminate_universe(universe);
            }
            None => {
                // As with sequence numbers the source might not be found which is acceptable.
            }
        }
    }

    /// Takes the given data and tries to add it to the waiting data.
    ///
    /// Note that a receiver will only store a single packet of data per data_universe at once.
    ///
    /// If there is waiting data for the same universe as the data then it will be merged as per the
    /// merge_func which by default keeps the highest priority data, if the data has the same priority
    /// then the newest data is kept.
    ///
    /// # Errors
    /// Will return an DmxMergeError if there is an issue merging or replacing new and existing waiting data.
    ///
    fn store_waiting_data(&mut self, data: DMXData) -> Result<()> {
        match self.waiting_data.remove(&data.universe) {
            Some(existing) => {
                self.waiting_data
                    .insert(data.universe, ((self.merge_func)(&existing, &data))?);
            }
            None => {
                self.waiting_data.insert(data.universe, data);
            }
        }
        Ok(())
    }

    /// Handles the given synchronisation packet for this DMX receiver.
    ///
    /// Synchronisation packets handled as described by ANSI E1.31-2018 Section 6.2.4.1.
    ///
    /// Returns the released / previously blocked data if successful.
    /// If the returned Vec is empty it indicates that no data was waiting.
    ///
    /// E1.31 Synchronization Packets occur on specific universes. Upon receipt, they indicate that any data advertising that universe as its Synchronization Address must be acted upon.
    /// In an E1.31 Data Packet, a value of 0 in the Synchronization Address indicates that the universe data is not synchronized. If a receiver is presented with an E1.31 Data Packet
    /// containing a Synchronization Address of 0, it shall discard any data waiting to be processed and immediately act on that Data Packet.
    ///
    /// If the Synchronization Address field is not 0, and the receiver is receiving an active synchronization stream for that Synchronization Address,
    /// it shall hold that E1.31 Data Packet until the arrival of the appropriate E1.31 Synchronization Packet before acting on it.
    ///
    /// Arguments:
    /// sync_pkt: The E1.31 synchronisation part of the synchronisation packet to handle.
    ///
    /// # Errors
    /// Returns an OutOfSequence error if a packet is received out of order as detected by the different between
    /// the packets sequence number and the expected sequence number as specified in ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    ///
    fn handle_sync_packet(
        &mut self,
        cid: Uuid,
        sync_pkt: SynchronizationPacketFramingLayer,
    ) -> Result<Option<Vec<DMXData>>> {
        if !self.is_listening(&sync_pkt.synchronization_address) {
            return Ok(None); // If not listening for this universe then ignore the packet.
        }

        self.sequences.check_sync_seq_number(
            self.source_limit,
            cid,
            sync_pkt.sequence_number,
            sync_pkt.synchronization_address,
            self.announce_timeout,
        )?;

        let res = self.rtrv_waiting_data(sync_pkt.synchronization_address);
        if res.len() == 0 {
            Ok(None)
        } else {
            Ok(Some(res))
        }
    }

    /// Retrieves and removes the DMX data of all waiting data with a synchronisation address matching the one provided.
    /// Returns an empty Vec if there is no data waiting.
    ///
    /// Arguments:
    /// sync_uni: The synchronisation universe of the data that should be retrieved.
    ///
    fn rtrv_waiting_data(&mut self, sync_uni: u16) -> Vec<DMXData> {
        // Get the universes (used as keys) to remove and then move the corresponding data out of the waiting data and into the result.
        // This prevents having to copy DMXData.
        // Cannot do both actions at once as cannot modify a data structure while iterating over it.
        let mut keys: Vec<u16> = Vec::new();
        for (uni, data) in self.waiting_data.iter() {
            if data.sync_uni == sync_uni {
                keys.push(*uni);
            }
        }

        let mut res: Vec<DMXData> = Vec::new();
        for k in keys {
            let data = self.waiting_data.remove(&k).unwrap();
            if data.recv_timestamp.elapsed() < E131_NETWORK_DATA_LOSS_TIMEOUT {
                res.push(data);
            }
        }

        res
    }

    /// Takes the given DiscoveredSacnSource and updates the record of discovered sacn sources.
    ///
    /// This adds the new source deleting any previous source with the same name.
    ///
    /// Arguments:
    /// src: The DiscoveredSacnSource to update the record of discovered sacn sources with.
    fn update_discovered_srcs(&mut self, src: DiscoveredSacnSource) {
        match find_discovered_src(&self.discovered_sources, &src.name) {
            Some(index) => {
                self.discovered_sources.remove(index);
            }
            None => {}
        }
        self.discovered_sources.push(src);
    }

    /// Handles the given universe discovery packet.
    ///
    /// This universe discovery packet might be the whole thing or may be just one page of a discovery packet.
    /// This method puts the pages to produce the DiscoveredSacnSource which is stored in the receiver.
    ///
    /// Returns the source name if a source was fully discovered or None if the source was only partially discovered.
    ///
    /// Arguments:
    /// discovery_pkt: The universe discovery part of the universe discovery packet to handle.
    ///
    fn handle_universe_discovery_packet(
        &mut self,
        discovery_pkt: UniverseDiscoveryPacketFramingLayer,
    ) -> Option<String> {
        let data: UniverseDiscoveryPacketUniverseDiscoveryLayer = discovery_pkt.data;

        let page: u8 = data.page;
        let last_page: u8 = data.last_page;

        let universes = data.universes;

        let uni_page: UniversePage = UniversePage {
            page: page,
            universes: universes.into(),
        };

        // See if some pages that belong to the source that this page belongs to have already been received.
        match find_discovered_src(
            &self.partially_discovered_sources,
            &discovery_pkt.source_name.to_string(),
        ) {
            Some(index) => {
                // Some pages have already been received from this source.
                self.partially_discovered_sources[index]
                    .pages
                    .push(uni_page);
                self.partially_discovered_sources[index].last_updated = Instant::now();
                if self.partially_discovered_sources[index].has_all_pages() {
                    let discovered_src: DiscoveredSacnSource =
                        self.partially_discovered_sources.remove(index);
                    self.update_discovered_srcs(discovered_src);
                    return Some(discovery_pkt.source_name.to_string());
                }
            }
            None => {
                // This is the first page received from this source.
                let discovered_src: DiscoveredSacnSource = DiscoveredSacnSource {
                    name: discovery_pkt.source_name.to_string(),
                    last_page: last_page,
                    pages: vec![uni_page],
                    last_updated: Instant::now(),
                };

                if page == 0 && page == last_page {
                    // Indicates that this is a single page universe discovery packet.
                    self.update_discovered_srcs(discovered_src);
                    return Some(discovery_pkt.source_name.to_string());
                } else {
                    // Indicates that this is a page in a set of pages as part of a sources universe discovery.
                    self.partially_discovered_sources.push(discovered_src);
                }
            }
        }

        return None; // No source fully discovered.
    }

    /// Goes through all the waiting data and removes any which has timed out as a sync-packet for it hasn't been received within the E131_NETWORK_DATA_LOSS_TIMEOUT
    /// period as specified by ANSI E1.31-2018 Section 11.1.2.
    fn check_waiting_data_timeouts(&mut self) {
        self.waiting_data
            .retain(|_uni, data| data.recv_timestamp.elapsed() < E131_NETWORK_DATA_LOSS_TIMEOUT);
    }

    /// Goes through all discovered sources and removes any that have timed out
    fn remove_expired_sources(&mut self) {
        self.partially_discovered_sources
            .retain(|s| s.last_updated.elapsed() < UNIVERSE_DISCOVERY_SOURCE_TIMEOUT);
        self.discovered_sources
            .retain(|s| s.last_updated.elapsed() < UNIVERSE_DISCOVERY_SOURCE_TIMEOUT);
    }
}

/// By implementing the Drop trait for SacnNetworkReceiver it means that the user doesn't have to explicitly clean up the receiver
/// and if it goes out of reference it will clean itself up.
impl Drop for SacnReceiver {
    fn drop(&mut self) {
        let universes = self.universes.clone();
        for u in universes {
            // Cannot return an error or pass it onto the user because drop might be called during a panic.
            // Therefore if there is an error cleaning up the only options are ignore, notify or panic.
            // Notify using stdout might pollute the application using the library so would require a flag to enable/disable but the function of this
            // is unclear and the problem isn't solved if the flag is disabled.
            // A panic might be unnecessary or pollute another in-progress panic hiding the true problem. It would also prevent muting the other
            // universes.
            // The error is therefore ignored as it can't be fixed eitherway as the SacnReceiver has gone out of scope and won't lead to memory un-safety.
            match self.mute_universe(u) {
                Ok(_) => {}
                Err(_e) => { /* Ignored */ }
            }
        }
    }
}

/// Searches for the discovered source with the given name in the given vector of discovered sources and
/// returns the index of the src in the Vec or None if not found.
///
/// Arguments:
/// srcs: The Vec of DiscoveredSacnSources to search.
/// name: The human readable name of the source to find.
///
fn find_discovered_src(srcs: &Vec<DiscoveredSacnSource>, name: &String) -> Option<usize> {
    for i in 0..srcs.len() {
        if srcs[i].name == *name {
            return Some(i);
        }
    }
    None
}

/// In general the lower level transport layer is handled by SacnNetworkReceiver (which itself wraps a Socket).
/// Windows and linux handle multicast sockets differently.
/// This is built for / tested with Windows 10 1909.
#[cfg(target_os = "windows")]
impl SacnNetworkReceiver {
    /// Creates a new DMX receiver on the interface specified by the given address.
    ///
    /// If the given address is an IPv4 address then communication will only work between IPv4 devices, if the given address is IPv6 then communication
    /// will only work between IPv6 devices by default but IPv4 receiving can be enabled using set_ipv6_only(false).
    ///
    /// # Errors
    /// Will return an error if the SacnReceiver fails to bind to a socket with the given ip.
    /// For more details see socket2::Socket::new().
    ///
    fn new(ip: SocketAddr) -> Result<SacnNetworkReceiver> {
        Ok(SacnNetworkReceiver {
            socket: create_win_socket(ip)?,
            addr: ip,
            is_multicast_enabled: !(ip.is_ipv6()), // IPv6 Windows IP Multicast is currently unsupported.
        })
    }

    /// Connects this SacnNetworkReceiver to the multicast address which corresponds to the given universe to allow receiving packets for that universe.
    ///
    /// # Errors
    /// Will return an Error if the given universe cannot be converted to an Ipv4 or Ipv6 multicast_addr depending on if the Receiver is bound to an
    /// IPv4 or IPv6 address. See packet::universe_to_ipv4_multicast_addr and packet::universe_to_ipv6_multicast_addr.
    ///
    /// Will return an Io error if cannot join the universes corresponding multicast group address.
    ///
    fn listen_multicast_universe(&self, universe: u16) -> Result<()> {
        let multicast_addr;

        if self.addr.is_ipv4() {
            multicast_addr = universe_to_ipv4_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv4 multicast addr")?;
        } else {
            multicast_addr = universe_to_ipv6_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv6 multicast addr")?;
        }

        Ok(join_win_multicast(&self.socket, multicast_addr)?)
    }

    /// Removes this SacnNetworkReceiver from the multicast group which corresponds to the given universe.
    ///
    /// # Errors
    /// Will return an Error if the given universe cannot be converted to an Ipv4 or Ipv6 multicast_addr depending on if the Receiver is bound to an
    /// IPv4 or IPv6 address. See packet::universe_to_ipv4_multicast_addr and packet::universe_to_ipv6_multicast_addr.
    ///
    fn mute_multicast_universe(&mut self, universe: u16) -> Result<()> {
        let multicast_addr;

        if self.addr.is_ipv4() {
            multicast_addr = universe_to_ipv4_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv4 multicast addr")?;
        } else {
            multicast_addr = universe_to_ipv6_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv6 multicast addr")?;
        }

        Ok(leave_win_multicast(&self.socket, multicast_addr)?)
    }

    /// Sets the value of the is_multicast_enabled flag to the given value.
    ///
    /// If set to false then the receiver won't attempt to join any more multicast groups.
    ///
    /// This method does not attempt to leave multicast groups already joined through previous listen_universe calls.
    ///
    /// # Arguments
    /// val: The new value for the is_multicast_enabled flag.
    ///
    /// # Errors
    /// Will return an OsOperationUnsupported error if attempting to set the flag to true in an environment that multicast
    /// isn't supported i.e. Ipv6 on Windows.
    fn set_is_multicast_enabled(&mut self, val: bool) -> Result<()> {
        if val && self.is_ipv6() {
            bail!(ErrorKind::OsOperationUnsupported(
                "IPv6 multicast is currently unsupported on Windows".to_string()
            ));
        }
        self.is_multicast_enabled = val;
        Ok(())
    }

    /// Returns true if multicast is enabled on this receiver and false if not.
    /// This flag is set when the receiver is created as not all environments currently support IP multicast.
    /// E.g. IPv6 Windows IP Multicast is currently unsupported.
    fn is_multicast_enabled(&self) -> bool {
        return self.is_multicast_enabled;
    }

    /// If set to true then only receive over IPv6. If false then receiving will be over both IPv4 and IPv6.
    /// This will return an error if the SacnReceiver wasn't created using an IPv6 address to bind to.
    fn set_only_v6(&mut self, val: bool) -> Result<()> {
        if self.addr.is_ipv4() {
            bail!(IpVersionError(
                "No data available in given timeout".to_string()
            ))
        } else {
            Ok(self.socket.set_only_v6(val)?)
        }
    }

    /// Returns a packet if there is one available.
    ///
    /// The packet may not be ready to transmit if it is awaiting synchronisation.
    /// Will only block if set_timeout was called with a timeout of None so otherwise (and by default) it won't
    /// block so may return a WouldBlock/TimedOut error to indicate that there was no data ready.
    ///
    /// IMPORTANT NOTE:
    /// An explicit lifetime is given to the AcnRootLayerProtocol which comes from the lifetime of the given buffer.
    /// The compiler will prevent usage of the returned AcnRootLayerProtocol after the buffer is dropped normally but may not in the case
    /// of unsafe code .
    ///
    /// Arguments:
    /// buf: The buffer to use for storing the received data into. This buffer shouldn't be accessed or used directly as the data
    /// is returned formatted properly in the AcnRootLayerProtocol. This buffer is used as memory space for the returned AcnRootLayerProtocol.
    ///
    /// # Errors
    /// May return an error if there is an issue receiving data from the underlying socket, see (recv)[fn.recv.Socket].
    ///
    /// May return an error if there is an issue parsing the data from the underlying socket, see (parse)[fn.AcnRootLayerProtocol::parse.packet].
    ///
    fn recv<'a>(
        &self,
        buf: &'a mut [u8; RCV_BUF_DEFAULT_SIZE],
    ) -> Result<AcnRootLayerProtocol<'a>> {
        self.socket.recv(&mut buf[0..])?;

        Ok(AcnRootLayerProtocol::parse(buf)?)
    }

    /// Set the timeout for the recv operation.
    ///
    /// Arguments:
    /// timeout: The new timeout for the receive operation, a value of None means the recv operation will become blocking.
    ///
    /// Errors:
    /// A timeout with Duration 0 will cause an error. See (set_read_timeout)[fn.set_read_timeout.Socket].
    ///
    fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
        Ok(self.socket.set_read_timeout(timeout)?)
    }

    /// Returns true if this SacnNetworkReceiver is bound to an Ipv6 address.
    fn is_ipv6(&self) -> bool {
        return self.addr.is_ipv6();
    }
}

/// Windows and linux handle multicast sockets differently.
/// This is built for / tested with Fedora 30/31.
#[cfg(target_os = "linux")]
impl SacnNetworkReceiver {
    /// Creates a new DMX receiver on the interface specified by the given address.
    ///
    /// If the given address is an IPv4 address then communication will only work between IPv4 devices, if the given address is IPv6 then communication
    /// will only work between IPv6 devices by default but IPv4 receiving can be enabled using set_ipv6_only(false).
    ///
    /// # Errors
    /// Will return an Io error if the SacnReceiver fails to bind to a socket with the given ip.
    /// For more details see socket2::Socket::new().
    ///
    fn new(ip: SocketAddr) -> Result<SacnNetworkReceiver> {
        Ok(SacnNetworkReceiver {
            socket: create_unix_socket(ip)?,
            addr: ip,
            is_multicast_enabled: true, // Linux IP Multicast is supported for Ipv4 and Ipv6.
        })
    }

    /// Connects this SacnNetworkReceiver to the multicast address which corresponds to the given universe to allow receiving packets for that universe.
    ///
    /// # Errors
    /// Will return an Error if the given universe cannot be converted to an IPv4 or IPv6 multicast_addr depending on if the Receiver is bound to an
    /// IPv4 or IPv6 address. See packet::universe_to_ipv4_multicast_addr and packet::universe_to_ipv6_multicast_addr.
    ///
    /// Will return an Io error if cannot join the universes corresponding multicast group address.
    ///
    fn listen_multicast_universe(&self, universe: u16) -> Result<()> {
        let multicast_addr;

        if self.addr.is_ipv4() {
            multicast_addr = universe_to_ipv4_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv4 multicast addr")?;
        } else {
            multicast_addr = universe_to_ipv6_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv6 multicast addr")?;
        }

        Ok(join_unix_multicast(
            &self.socket,
            multicast_addr,
            self.addr.ip(),
        )?)
    }

    /// Removes this SacnNetworkReceiver from the multicast group which corresponds to the given universe.
    ///
    /// # Errors
    /// Will return an Error if the given universe cannot be converted to an Ipv4 or Ipv6 multicast_addr depending on if the Receiver is bound to an
    /// IPv4 or IPv6 address. See packet::universe_to_ipv4_multicast_addr and packet::universe_to_ipv6_multicast_addr.
    ///
    fn mute_multicast_universe(&mut self, universe: u16) -> Result<()> {
        let multicast_addr;

        if self.addr.is_ipv4() {
            multicast_addr = universe_to_ipv4_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv4 multicast addr")?;
        } else {
            multicast_addr = universe_to_ipv6_multicast_addr(universe)
                .chain_err(|| "Failed to convert universe to IPv6 multicast addr")?;
        }

        Ok(leave_unix_multicast(
            &self.socket,
            multicast_addr,
            self.addr.ip(),
        )?)
    }

    /// Sets the value of the is_multicast_enabled flag to the given value.
    ///
    /// If set to false then the receiver won't attempt to join any more multicast groups.
    ///
    /// This method does not attempt to leave multicast groups already joined through previous listen_universe calls.
    ///
    /// # Arguments
    /// val: The new value for the is_multicast_enabled flag.
    ///
    /// # Errors
    /// Will return an OsOperationUnsupported error if attempting to set the flag to true in an environment that multicast
    /// isn't supported i.e. Ipv6 on Windows. Note that this is the UNIX implementation
    fn set_is_multicast_enabled(&mut self, val: bool) -> Result<()> {
        self.is_multicast_enabled = val;
        Ok(())
    }

    /// Returns true if multicast is enabled on this receiver and false if not.
    /// This flag is set when the receiver is created as not all environments currently support IP multicast.
    /// E.g. IPv6 Windows IP Multicast is currently unsupported.
    fn is_multicast_enabled(&self) -> bool {
        return self.is_multicast_enabled;
    }

    /// If set to true then only receive over IPv6. If false then receiving will be over both IPv4 and IPv6.
    /// This will return an error if the SacnReceiver wasn't created using an IPv6 address to bind to.
    fn set_only_v6(&mut self, val: bool) -> Result<()> {
        if self.addr.is_ipv4() {
            bail!(IpVersionError(
                "No data available in given timeout".to_string()
            ))
        } else {
            Ok(self.socket.set_only_v6(val)?)
        }
    }

    /// Returns a packet if there is one available.
    ///
    /// The packet may not be ready to transmit if it is awaiting synchronisation.
    /// Will only block if set_timeout was called with a timeout of None so otherwise (and by default) it won't
    /// block so may return a WouldBlock/TimedOut error to indicate that there was no data ready.
    ///
    /// IMPORTANT NOTE:
    /// An explicit lifetime is given to the AcnRootLayerProtocol which comes from the lifetime of the given buffer.
    /// The compiler will prevent usage of the returned AcnRootLayerProtocol after the buffer is dropped.
    ///
    /// Arguments:
    /// buf: The buffer to use for storing the received data into. This buffer shouldn't be accessed or used directly as the data
    /// is returned formatted properly in the AcnRootLayerProtocol. This buffer is used as memory space for the returned AcnRootLayerProtocol.
    ///
    /// # Errors
    /// May return an error if there is an issue receiving data from the underlying socket, see (recv)[fn.recv.Socket].
    ///
    /// May return an error if there is an issue parsing the data from the underlying socket, see (parse)[fn.AcnRootLayerProtocol::parse.packet].
    ///
    fn recv<'a>(
        &self,
        buf: &'a mut [u8; RCV_BUF_DEFAULT_SIZE],
    ) -> Result<AcnRootLayerProtocol<'a>> {
        self.socket.recv(&mut buf[0..])?;

        Ok(AcnRootLayerProtocol::parse(buf)?)
    }

    /// Set the timeout for the recv operation.
    ///
    /// Arguments:
    /// timeout: The new timeout for the receive operation, a value of None means the recv operation will become blocking.
    ///
    /// Errors:
    /// A timeout with Duration 0 will cause an error. See (set_read_timeout)[fn.set_read_timeout.Socket].
    ///
    fn set_timeout(&mut self, timeout: Option<Duration>) -> Result<()> {
        Ok(self.socket.set_read_timeout(timeout)?)
    }
}

impl Clone for DMXData {
    fn clone(&self) -> DMXData {
        let new_vals = self.values.to_vec(); // https://stackoverflow.com/questions/21369876/what-is-the-idiomatic-rust-way-to-copy-clone-a-vector-in-a-parameterized-functio (26/12/2019)
        DMXData {
            universe: self.universe,
            values: new_vals,
            sync_uni: self.sync_uni,
            priority: self.priority,
            src_cid: self.src_cid,
            preview: self.preview,
            recv_timestamp: self.recv_timestamp,
        }
    }
}

/// DMXData has a total ordering based on the universe, then sync-universe and finally values.
impl Ord for DMXData {
    fn cmp(&self, other: &Self) -> Ordering {
        self.universe
            .cmp(&other.universe)
            .then(self.sync_uni.cmp(&other.sync_uni))
            .then(self.values.cmp(&other.values))
    }
}

/// See Ord trait implementation for DMXData.
impl PartialOrd for DMXData {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// DMXData is taken to be equivalent iff:
///     - The universes are the same
///     - The synchronisation universes are the same
///     - The values are all the same
///
impl PartialEq for DMXData {
    fn eq(&self, other: &Self) -> bool {
        self.universe == other.universe
            && self.sync_uni == other.sync_uni
            && self.values == other.values
    }
}

/// See PartialEq trait implementation for DMXData.
impl Eq for DMXData {}

impl DiscoveredSacnSource {
    /// Returns true if all the pages sent by this DiscoveredSacnSource have been received.
    ///
    /// This is based on each page containing a last-page value which indicates the number of the last page expected.
    pub fn has_all_pages(&mut self) -> bool {
        // https://rust-lang-nursery.github.io/rust-cookbook/algorithms/sorting.html (31/12/2019)
        self.pages.sort_by(|a, b| a.page.cmp(&b.page));
        for i in 0..(self.last_page + 1) {
            if self.pages[i as usize].page != i {
                return false;
            }
        }

        return true;
    }

    /// Returns all the universes being send by this SacnSource as discovered through the universe discovery mechanism.
    ///
    /// Intentionally abstracts over the underlying concept of pages as this is purely an E1.31 Universe Discovery concept and is otherwise transparent.
    pub fn get_all_universes(&self) -> Vec<u16> {
        let mut uni: Vec<u16> = Vec::new();
        for p in &self.pages {
            uni.extend_from_slice(&p.universes);
        }
        uni
    }

    /// Removes the given universe from the list of universes being sent by this discovered source.
    pub fn terminate_universe(&mut self, universe: u16) {
        for p in &mut self.pages {
            p.universes.retain(|x| *x != universe)
        }
    }
}

/// Creates a new Socket2 socket bound to the given address.
///
/// Returns the created socket.
///
/// Arguments:
/// addr: The address that the newly created socket should bind to.
///
/// # Errors
/// Will return an error if the socket cannot be created, see (Socket::new)[fn.new.Socket].
///
/// Will return an error if the socket cannot be bound to the given address, see (bind)[fn.bind.Socket2].
#[cfg(target_os = "linux")]
fn create_unix_socket(addr: SocketAddr) -> Result<Socket> {
    if addr.is_ipv4() {
        let socket = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?;

        // Multiple different processes might want to listen to the sACN stream so therefore need to allow re-using the ACN port.
        socket.set_reuse_port(true)?;
        socket.set_reuse_address(true)?;

        let socket_addr =
            SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);
        socket.bind(&socket_addr.into())?;
        Ok(socket)
    } else {
        let socket = Socket::new(Domain::ipv6(), Type::dgram(), Some(Protocol::udp()))?;

        // Multiple different processes might want to listen to the sACN stream so therefore need to allow re-using the ACN port.
        socket.set_reuse_port(true)?;
        socket.set_reuse_address(true)?;

        let socket_addr =
            SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);
        socket.bind(&socket_addr.into())?;
        Ok(socket)
    }
}

/// Joins the multicast group with the given address using the given socket.
///
/// Arguments:
/// socket: The socket to join to the multicast group.
/// addr:   The address of the multicast group to join.
///
/// # Errors
/// Will return an error if the given socket cannot be joined to the given multicast group address.
///     See join_multicast_v4[fn.join_multicast_v4.Socket] and join_multicast_v6[fn.join_multicast_v6.Socket]
///
/// Will return an IpVersionError if addr and interface_addr are not the same IP version.
///
#[cfg(target_os = "linux")]
fn join_unix_multicast(socket: &Socket, addr: SockAddr, interface_addr: IpAddr) -> Result<()> {
    match addr.family() as i32 {
        // Cast required because AF_INET is defined in libc in terms of a c_int (i32) but addr.family returns using u16.
        AF_INET => match addr.as_inet() {
            Some(a) => match interface_addr {
                IpAddr::V4(ref interface_v4) => {
                    socket
                        .join_multicast_v4(a.ip(), &interface_v4)
                        .chain_err(|| "Failed to join IPv4 multicast")?;
                }
                IpAddr::V6(ref _interface_v6) => {
                    bail!(ErrorKind::IpVersionError(
                        "Multicast address and interface_addr not same IP version".to_string()
                    ));
                }
            },
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET but not actually usable as AF_INET so must be unknown type".to_string()));
            }
        },
        AF_INET6 => match addr.as_inet6() {
            Some(a) => {
                socket
                    .join_multicast_v6(a.ip(), 0)
                    .chain_err(|| "Failed to join IPv6 multicast")?;
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET6 but not actually usable as AF_INET6 so must be unknown type".to_string()));
            }
        },
        x => {
            bail!(ErrorKind::UnsupportedIpVersion(format!("IP version not recognised as AF_INET (Ipv4) or AF_INET6 (Ipv6) - family value (as i32): {}", x).to_string()));
        }
    };

    Ok(())
}

/// Leaves the multicast group with the given address using the given socket.
///
/// Arguments:
/// socket: The socket to leave the multicast group.
/// addr:   The address of the multicast group to leave.
///
/// # Errors
/// Will return an error if the given socket cannot leave the given multicast group address.
///     See leave_multicast_v4[fn.leave_multicast_v4.Socket] and leave_multicast_v6[fn.leave_multicast_v6.Socket]
///
/// Will return an IpVersionError if addr and interface_addr are not the same IP version.
///
#[cfg(target_os = "linux")]
fn leave_unix_multicast(socket: &Socket, addr: SockAddr, interface_addr: IpAddr) -> Result<()> {
    match addr.family() as i32 {
        // Cast required because AF_INET is defined in libc in terms of a c_int (i32) but addr.family returns using u16.
        AF_INET => match addr.as_inet() {
            Some(a) => match interface_addr {
                IpAddr::V4(ref interface_v4) => {
                    socket
                        .leave_multicast_v4(a.ip(), &interface_v4)
                        .chain_err(|| "Failed to leave IPv4 multicast")?;
                }
                IpAddr::V6(ref _interface_v6) => {
                    bail!(ErrorKind::IpVersionError(
                        "Multicast address and interface_addr not same IP version".to_string()
                    ));
                }
            },
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET but not actually usable as AF_INET so must be unknown type".to_string()));
            }
        },
        AF_INET6 => match addr.as_inet6() {
            Some(a) => {
                socket
                    .leave_multicast_v6(a.ip(), 0)
                    .chain_err(|| "Failed to leave IPv6 multicast")?;
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET6 but not actually usable as AF_INET6 so must be unknown type".to_string()));
            }
        },
        x => {
            bail!(ErrorKind::UnsupportedIpVersion(format!("IP version not recognised as AF_INET (Ipv4) or AF_INET6 (Ipv6) - family value (as i32): {}", x).to_string()));
        }
    };

    Ok(())
}

/// Creates a new Socket2 socket bound to the given address.
///
/// Returns the created socket.
///
/// Arguments:
/// addr: The address that the newly created socket should bind to.
///
/// # Errors
/// Will return an error if the socket cannot be created, see (Socket::new)[fn.new.Socket].
///
/// Will return an error if the socket cannot be bound to the given address, see (bind)[fn.bind.Socket].
#[cfg(target_os = "windows")]
fn create_win_socket(addr: SocketAddr) -> Result<Socket> {
    if addr.is_ipv4() {
        let socket = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?;

        socket.set_reuse_address(true)?;
        socket.bind(&SockAddr::from(addr))?;
        Ok(socket)
    } else {
        let socket = Socket::new(Domain::ipv6(), Type::dgram(), Some(Protocol::udp()))?;

        socket.set_reuse_address(true)?;
        socket.bind(&SockAddr::from(addr))?;
        Ok(socket)
    }
}

/// Joins the multicast group with the given address using the given socket on the windows operating system.
///
/// Note that Ipv6 is currently unsupported.
///
/// Arguments:
/// socket: The socket to join to the multicast group.
/// addr:   The address of the multicast group to join.
///
/// # Errors
/// Will return an error if the given socket cannot be joined to the given multicast group address.
///     See join_multicast_v4[fn.join_multicast_v4.Socket] and join_multicast_v6[fn.join_multicast_v6.Socket]
///
/// Will return OsOperationUnsupported error if attempt to leave an Ipv6 multicast group as all Ipv6 multicast operations are currently unsupported in Rust on Windows.
///
#[cfg(target_os = "windows")]
fn join_win_multicast(socket: &Socket, addr: SockAddr) -> Result<()> {
    match addr.family() as i32 {
        // Cast required because AF_INET is defined in libc in terms of a c_int (i32) but addr.family returns using u16.
        AF_INET => match addr.as_inet() {
            Some(a) => {
                socket
                    .join_multicast_v4(a.ip(), &Ipv4Addr::new(0, 0, 0, 0))
                    .chain_err(|| "Failed to join IPv4 multicast")?;
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET but not actually usable as AF_INET so must be unknown type".to_string()));
            }
        },
        AF_INET6 => match addr.as_inet6() {
            Some(_) => {
                bail!(ErrorKind::OsOperationUnsupported(
                    "IPv6 multicast is currently unsupported on Windows".to_string()
                ));
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET6 but not actually usable as AF_INET6 so must be unknown type".to_string()));
            }
        },
        x => {
            bail!(ErrorKind::UnsupportedIpVersion(format!("IP version not recognised as AF_INET (Ipv4) or AF_INET6 (Ipv6) - family value (as i32): {}", x).to_string()));
        }
    };

    Ok(())
}

/// Leaves the multicast group with the given address using the given socket.
///
/// Note that Ipv6 is currently unsupported.
///
/// Arguments:
/// socket: The socket to leave the multicast group.
/// addr:   The address of the multicast group to leave.
///
/// # Errors
/// Will return an error if the given socket cannot leave the given multicast group address.
///     See leave_multicast_v4[fn.leave_multicast_v4.Socket] and leave_multicast_v6[fn.leave_multicast_v6.Socket]
///
/// Will return OsOperationUnsupported error if attempt to leave an Ipv6 multicast group as all Ipv6 multicast operations are currently unsupported in Rust on Windows.
///
#[cfg(target_os = "windows")]
fn leave_win_multicast(socket: &Socket, addr: SockAddr) -> Result<()> {
    match addr.family() as i32 {
        // Cast required because AF_INET is defined in libc in terms of a c_int (i32) but addr.family returns using u16.
        AF_INET => match addr.as_inet() {
            Some(a) => {
                socket
                    .leave_multicast_v4(a.ip(), &Ipv4Addr::new(0, 0, 0, 0))
                    .chain_err(|| "Failed to leave IPv4 multicast")?;
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET but not actually usable as AF_INET so must be unknown type".to_string()));
            }
        },
        AF_INET6 => match addr.as_inet6() {
            Some(_) => {
                bail!(ErrorKind::OsOperationUnsupported(
                    "IPv6 multicast is currently unsupported on Windows".to_string()
                ));
            }
            None => {
                bail!(ErrorKind::UnsupportedIpVersion("IP version recognised as AF_INET6 but not actually usable as AF_INET6 so must be unknown type".to_string()));
            }
        },
        x => {
            bail!(ErrorKind::UnsupportedIpVersion(format!("IP version not recognised as AF_INET (Ipv4) or AF_INET6 (Ipv6) - family value (as i32): {}", x).to_string()));
        }
    };

    Ok(())
}

/// Stores a sequence number and a timestamp.
///
/// Used internally within SequenceNumbering for tracking the last received timestamps of each packet-type, source, universe combination.
///
/// This is then used to workout timeouts to trigger network data loss as per ANSI E1.31-2018 Section 6.7.1.
///
#[derive(Copy, Clone)]
struct TimedStampedSeqNo {
    sequence_number: u8,
    last_recv: Instant,
}

impl TimedStampedSeqNo {
    fn new(sequence_number: u8, last_recv: Instant) -> TimedStampedSeqNo {
        TimedStampedSeqNo {
            sequence_number: sequence_number,
            last_recv: last_recv,
        }
    }
}

/// Stores information about the current expected sequence numbers for each source, universe and packet type.
///
/// Also handles timeouts of sources.
///
/// Abstracts over the internal data-structures/mechanisms used allowing them be changed.
///
struct SequenceNumbering {
    /// The sequence numbers used for data packets, keeps a reference of the last sequence number received for each universe.
    /// Sequence numbers are always in the range [0, 255] inclusive.
    /// Each type of packet is tracked differently with respect to sequence numbers as per ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    /// The uuid refers to the source that is sending the data.
    data_sequences: HashMap<Uuid, HashMap<u16, TimedStampedSeqNo>>,

    /// The sequence numbers used for synchronisation packets, keeps a reference of the last sequence number received for each universe.
    /// Sequence numbers are always in the range [0, 255] inclusive.
    /// Each type of packet is tracked differently with respect to sequence numbers as per ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    /// The uuid refers to the source that is sending the data.
    sync_sequences: HashMap<Uuid, HashMap<u16, TimedStampedSeqNo>>,
}

impl SequenceNumbering {
    /// Creates a new SequenceNumbering for tracking sequence numbers for the various types of packets.
    ///
    /// This implementation uses HashMaps internally to allow O(1) checking and updating of sequence numbers.
    ///
    fn new() -> SequenceNumbering {
        return SequenceNumbering {
            data_sequences: HashMap::new(),
            sync_sequences: HashMap::new(),
        };
    }

    /// Clears the sequence number records completely removing all sources/universes for all types of packet.
    ///
    fn clear(&mut self) {
        self.data_sequences.clear();
        self.sync_sequences.clear();
    }

    /// Checks the timeouts for all packet types, sources and universes with sequence numbers registed.
    /// Removes any universes for which the last_recv time was at least the given timeout amount of time ago.
    /// Any sources which have no universes after this operation are also removed.
    ///
    /// #Arguments
    ///
    /// announce_timeout: A flag, if true it indicates than a UniverseTimeout error should be thrown if a universe times out on a source.
    ///  
    fn check_timeouts(&mut self, announce_timeout: bool) -> Result<()> {
        check_timeouts(
            &mut self.data_sequences,
            E131_NETWORK_DATA_LOSS_TIMEOUT,
            announce_timeout,
        )?;
        check_timeouts(
            &mut self.sync_sequences,
            E131_NETWORK_DATA_LOSS_TIMEOUT,
            announce_timeout,
        )
    }

    /// Checks the sequence number is correct for a data packet with the given sequence_number and universe from the given source with given cid.
    /// Uses the given source_limit to check that it isn't exceeded.
    ///
    /// Returns Ok(()) if the packet is detected in-order.
    ///
    /// # Arguments
    /// source_limit: The limit on the number of sources which are allowed, None indicates no limit, if there is a limit then a SourcesExceededError may be returned.
    ///
    /// cid:    The Uuid of the source that send the packet.
    ///
    /// sequence_number: The sequence number of the packet to check.
    ///
    /// universe: The data universe of the packet.
    ///
    /// # Errors
    /// Returns an OutOfSequence error if a packet is received out of order as detected by the different between
    /// the packets sequence number and the expected sequence number as specified in ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    ///
    /// Return a SourcesExceededError if the cid of the source is new and would cause the number of sources to exceed the given source_limit.
    ///
    fn check_data_seq_number(
        &mut self,
        source_limit: Option<usize>,
        cid: Uuid,
        sequence_number: u8,
        universe: u16,
        announce_timeout: bool,
    ) -> Result<()> {
        check_seq_number(
            &mut self.data_sequences,
            source_limit,
            cid,
            sequence_number,
            universe,
            announce_timeout,
        )
    }

    /// Checks the sequence number is correct for a sync packet with the given sequence_number and universe from the given source with given cid.
    /// Uses the given source_limit to check that it isn't exceeded.
    ///
    /// Returns Ok(()) if the packet is detected in-order.
    ///
    /// # Arguments
    /// source_limit: The limit on the number of sources which are allowed, None indicates no limit, if there is a limit then a SourcesExceededError may be returned.
    ///
    /// cid:    The Uuid of the source that send the packet.
    ///
    /// sequence_number: The sequence number of the packet to check.
    ///
    /// universe: The sync universe of the packet
    ///
    /// # Errors
    /// Returns an OutOfSequence error if a packet is received out of order as detected by the different between
    /// the packets sequence number and the expected sequence number as specified in ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
    ///
    /// Return a SourcesExceededError if the cid of the source is new and would cause the number of sources to exceed the given source_limit.
    ///
    fn check_sync_seq_number(
        &mut self,
        source_limit: Option<usize>,
        cid: Uuid,
        sequence_number: u8,
        sync_uni: u16,
        announce_timeout: bool,
    ) -> Result<()> {
        check_seq_number(
            &mut self.sync_sequences,
            source_limit,
            cid,
            sequence_number,
            sync_uni,
            announce_timeout,
        )
    }

    /// Removes the sequence number tracking for the given source / universe combination.
    /// This applies to both data and sync packets.
    ///
    /// # Arguments:
    ///
    /// src_cid: The CID of the source to remove the sequence numbers of.
    ///
    /// universe: The universe being sent by the source from which to remove the sequence numbers.
    ///
    fn remove_seq_numbers<'a>(&mut self, src_cid: Uuid, universe: u16) -> Result<()> {
        remove_source_universe_seq(&mut self.data_sequences, src_cid, universe)?;
        remove_source_universe_seq(&mut self.sync_sequences, src_cid, universe)
    }
}

/// Checks the given sequence number for the given universe against the given expected sequence numbers.
///
/// Returns Ok(()) if the packet is detected in-order.
///
/// # Arguments
/// src_sequences: A mutable hashmap which relates sources identified by Uuid to another hashmap which itself relates universes to sequence numbers. The given hashmap of
///                 sequences should be for the specific packet-type being checked as different packet-types have their own sequence numbers even from the same source.
/// source_limit: The limit on the number of sources which are allowed, None indicates no limit, if there is a limit then a SourcesExceededError may be returned.
/// cid:    The Uuid of the source that send the packet.
/// sequence_number: The sequence number of the packet to check.
/// universe: The universe of the packet (this is the data universe for data packets and the sync universe for synchronisation packets).
///
/// # Errors
/// Returns an OutOfSequence error if a packet is received out of order as detected by the different between
/// the packets sequence number and the expected sequence number as specified in ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
///
/// Return a SourcesExceededError if the cid of the source is new and would cause the number of sources to exceed the given source_limit.
///
fn check_seq_number(
    src_sequences: &mut HashMap<Uuid, HashMap<u16, TimedStampedSeqNo>>,
    source_limit: Option<usize>,
    cid: Uuid,
    sequence_number: u8,
    universe: u16,
    announce_timeout: bool,
) -> Result<()> {
    // Check all the timeouts at the start.
    // This is done for all sources/universes rather than just the source that sent the packet because a completely dead (no packets being sent) universe
    // would not be removed otherwise and would continue to take up space. This comes at the cost of increased processing time complexity as each
    // source is checked every time.
    check_timeouts(
        src_sequences,
        E131_NETWORK_DATA_LOSS_TIMEOUT,
        announce_timeout,
    )?;

    match src_sequences.get(&cid) {
        None => {
            // New source not previously received from.
            if source_limit.is_none() || src_sequences.len() < source_limit.unwrap() {
                src_sequences.insert(cid, HashMap::new());
            } else {
                bail!(ErrorKind::SourcesExceededError(
                    format!("Already at max sources: {}", src_sequences.len()).to_string()
                ));
            }
        }
        Some(_) => {}
    };

    let expected_seq = match src_sequences.get(&cid) {
        Some(src) => {
            let seq_num = match src.get(&universe) {
                // Get the sequence number within the source for the specific universe.
                Some(s) => {
                    // Indicates that the source / universe combination is known.
                    *s
                }
                None => {
                    // Indicates that this is the first time (or the first time since it timed out) the universe has been received from this source.
                    TimedStampedSeqNo::new(INITIAL_SEQUENCE_NUMBER, Instant::now())
                }
            };
            seq_num
        }
        None => {
            // Previously checked that cid is present (and added if not), if None is returned now it indicates that between that check and this
            // function the cid key value has been removed. This can only happen if there is a memory corruption/thread-interleaving or similar external
            // event which the receiver cannot be expected to handle / doesn't support. 
            // The rust typing system forces this possibility to be acknowledged when in some languages this possibility would still exist but it would be hidden
            // within the code. 
            // While a panic!() call here isn't ideal it shows the strength in the explictness of the rust system and points to an area of 
            // potential later improvement within the code by not hiding the problem. As normal if the panic must be caught then rust allows this later on by utilising
            // a mechanism such as catch unwind https://doc.rust-lang.org/std/panic/fn.catch_unwind.html.
            // Another possibility here could be to retry the method but this could end with an infinite loop.
            // Returning an error could also be done but that could confuse error handling as this should not occur and the receiver would be in an inconsistent
            // state.
            panic!();
        }
    };

    let seq_diff: isize = (sequence_number as isize) - (expected_seq.sequence_number as isize);

    if seq_diff <= E131_SEQ_DIFF_DISCARD_UPPER_BOUND && seq_diff > E131_SEQ_DIFF_DISCARD_LOWER_BOUND
    {
        // Reject the out of order packet as per ANSI E1.31-2018 Section 6.7.2 Sequence Numbering.
        bail!(ErrorKind::OutOfSequence(
            format!(
                "Packet received with sequence number {} is out of sequence, last {}, seq-diff {}",
                sequence_number, expected_seq.sequence_number, seq_diff
            )
            .to_string()
        ));
    }

    match src_sequences.get_mut(&cid) {
        Some(src) => {
            // Replace the old sequence number with the new and reset the timeout.
            src.insert(
                universe,
                TimedStampedSeqNo::new(sequence_number, Instant::now()),
            );
        }
        None => {
            // See previous node regarding panic previously in this method.
            panic!();
        }
    };

    Ok(())
}

/// Checks the timeouts for all sources and universes for the given sequences.
/// Removes any universes for which the last_recv time was at least the given timeout amount of time ago.
/// Any sources which have no universes after this operation are also removed.
///
/// #Arguments
///
/// src_sequences: The source sequence numbers to check the timeout of.
///
/// timeout: The exclusive length of time permitted since a source last sent on a universe.
///     If the time elapsed since the last received data that is equal to or great than the timeout then the source is said to have timed out.
///  
fn check_timeouts(
    src_sequences: &mut HashMap<Uuid, HashMap<u16, TimedStampedSeqNo>>,
    timeout: Duration,
    announce_timeout: bool,
) -> Result<()> {
    if announce_timeout {
        let mut timedout_src_id: Option<Uuid> = None;
        let mut timedout_uni: Option<u16> = None;
        for (src_id, universes) in src_sequences.iter_mut() {
            for (uni, seq_num) in universes.iter() {
                if seq_num.last_recv.elapsed() >= timeout {
                    timedout_src_id = Some(*src_id);
                    timedout_uni = Some(*uni);
                    break;
                }
            }
            if timedout_uni == None {
                break;
            }
        }
        if timedout_uni.is_some() {
            // If None then it indicates nothing timed out.
            let uni_to_remove = timedout_uni.unwrap();
            let src_universes = src_sequences.get_mut(&timedout_src_id.unwrap());
            if src_universes.is_some() {
                let universes = src_universes.unwrap();
                universes.remove(&uni_to_remove);
                if universes.is_empty() {
                    // Remove source if all its universes have timed out
                    src_sequences.remove(&timedout_src_id.unwrap());
                }
                bail!(ErrorKind::UniverseTimeout(
                    timedout_src_id.unwrap(),
                    timedout_uni.unwrap()
                ));
            }
        }

        Ok(())
    } else {
        for (_src_id, universes) in src_sequences.iter_mut() {
            universes.retain(|_uni, seq_num| seq_num.last_recv.elapsed() < timeout);
        }
        // Remove all empty sources.
        src_sequences.retain(|_src_id, universes| !universes.is_empty());
        Ok(())
    }
}

/// Removes the sequence number entry from the given sequences for the given source cid and universe.
///
/// This removes the source entirely if there are no universes left.
///
/// # Arguments
/// src_sequences: The sequence numbers for each source and universe.
///
/// src_cid:       The CID for the source to remove the universe from.
///
/// universe:      The universe to remove from the source.
///
/// # Errors
/// Returns a SourceNotFound error if the given src_cid isn't in the given collection of sources/sequence-numbers.
///
/// Returns a UniverseNotFound error if the given universe isn't registered to the given source and so cannot be removed.
///
fn remove_source_universe_seq(
    src_sequences: &mut HashMap<Uuid, HashMap<u16, TimedStampedSeqNo>>,
    src_cid: Uuid,
    universe: u16,
) -> Result<()> {
    match src_sequences.get_mut(&src_cid) {
        Some(x) => {
            match x.remove(&universe) {
                Some(_) => {
                    if x.is_empty() {
                        // Remove the source if there are no universes registered to it.
                        match src_sequences.remove(&src_cid) {
                            Some(_x) => Ok(()),
                            None => {
                                bail!(ErrorKind::SourceNotFound(
                                    "Could not find the source so could not remove it".to_string()
                                ));
                            }
                        }
                    } else {
                        Ok(())
                    }
                }
                None => {
                    bail!(ErrorKind::UniverseNotFound("Could not find universe within source in sequence numbers so could not remove it".to_string()));
                }
            }
        }
        None => {
            bail!(ErrorKind::SourceNotFound(
                "Could not find the source in the sequence numbers so could not remove it"
                    .to_string()
            ));
        }
    }
}

/// The default merge action for the receiver.
///
/// This discarding of the old data is the default action for compliance as specified in ANSI E1.31-2018, Section 11.2.1.
///
/// This can be changed if required as part of the mechanism described in ANSI E1.31-2018, Section 6.2.3.4 Requirements for Merging and Arbitrating.
///
/// The first argument (i) is the existing data, n is the new data.
/// This function is only valid if both inputs have the same universe, sync addr, start_code and the data contains at least the first value (the start code).
/// If this doesn't hold an error will be returned.
/// Other merge functions may allow merging different start codes or not check for them.
pub fn discard_lowest_priority_then_previous(i: &DMXData, n: &DMXData) -> Result<DMXData> {
    if i.priority > n.priority {
        return Ok(i.clone());
    }
    Ok(n.clone())
}

/// Performs a highest takes priority (HTP) (per byte) DMX merge of data.
///
/// Note this merge is done within the explicit priority, if i or n has an explicitly higher priority it will always take precedence before this HTP merge is attempted.
/// If either data has the preview flag set then the result will have the preview flag set.
///
/// Given as an example of a possible merge algorithm.
///
/// The first argument (i) is the existing data, n is the new data.
///
/// This function is only valid if both inputs have the same universe, sync addr, start_code and the data contains at least the first value (the start code).
/// If this doesn't hold an error will be returned.
/// Other merge functions may allow merging different start codes or not check for them.
pub fn htp_dmx_merge(i: &DMXData, n: &DMXData) -> Result<DMXData> {
    if i.values.len() < 1
        || n.values.len() < 1
        || i.universe != n.universe
        || i.values[0] != n.values[0]
        || i.sync_uni != n.sync_uni
    {
        bail!(DmxMergeError("Attempted DMX merge on dmx data with different universes, synchronisation universes or data with no values".to_string()));
    }

    if i.priority > n.priority {
        return Ok(i.clone());
    } else if n.priority > i.priority {
        return Ok(n.clone());
    }
    // Must have same priority.

    let mut r: DMXData = DMXData {
        universe: i.universe,
        values: Vec::new(),
        sync_uni: i.sync_uni,
        priority: i.priority,
        src_cid: None,
        preview: i.preview || n.preview, // If either data is preview then mark the result as preview.
        recv_timestamp: i.recv_timestamp,
    };

    let mut i_iter = i.values.iter();
    let mut n_iter = n.values.iter();

    let mut i_val = i_iter.next();
    let mut n_val = n_iter.next();

    while (i_val.is_some()) || (n_val.is_some()) {
        if i_val == None {
            r.values.push(*n_val.unwrap());
        } else if n_val == None {
            r.values.push(*i_val.unwrap());
        } else {
            r.values.push(max(*n_val.unwrap(), *i_val.unwrap()));
        }

        i_val = i_iter.next();
        n_val = n_iter.next();
    }

    Ok(r)
}

#[cfg(test)]
mod test {
    use super::*;

    use std::borrow::Cow;
    use std::net::{IpAddr, Ipv4Addr, SocketAddr};
    use std::time::Instant;

    use uuid::Uuid;

    const TEST_DATA_SINGLE_UNIVERSE: [u8; 512] = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1, 2, 3, 4,
        5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
        19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8,
        9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
    ];

    #[test]
    fn test_handle_single_page_discovery_packet() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let name = "Test Src 1";
        let page: u8 = 0;
        let last_page: u8 = 0;
        let universes: Vec<u16> = vec![0, 1, 2, 3, 4, 5];

        let discovery_pkt: UniverseDiscoveryPacketFramingLayer =
            UniverseDiscoveryPacketFramingLayer {
                source_name: name.into(),

                /// Universe discovery layer.
                data: UniverseDiscoveryPacketUniverseDiscoveryLayer {
                    page: page,

                    /// The number of the final page.
                    last_page: last_page,

                    /// List of universes.
                    universes: universes.clone().into(),
                },
            };
        let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt);

        assert!(res.is_some());
        assert_eq!(res.unwrap(), name);

        assert_eq!(dmx_rcv.discovered_sources.len(), 1);

        assert_eq!(dmx_rcv.discovered_sources[0].name, name);
        assert_eq!(dmx_rcv.discovered_sources[0].last_page, last_page);
        assert_eq!(dmx_rcv.discovered_sources[0].pages.len(), 1);
        assert_eq!(dmx_rcv.discovered_sources[0].pages[0].page, page);
        assert_eq!(dmx_rcv.discovered_sources[0].pages[0].universes, universes);
    }

    #[test]
    fn test_handle_multi_page_discovery_packet() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let name = "Test Src 1";
        let last_page: u8 = 1;
        let mut universes_page_1: Vec<u16> = Vec::new();
        let mut universes_page_2: Vec<u16> = Vec::new();

        for i in 1..513 {
            universes_page_1.push(i);
        }

        for i in 513..1024 {
            universes_page_2.push(i);
        }

        let discovery_pkt_1: UniverseDiscoveryPacketFramingLayer =
            UniverseDiscoveryPacketFramingLayer {
                source_name: name.into(),

                /// Universe discovery layer.
                data: UniverseDiscoveryPacketUniverseDiscoveryLayer {
                    page: 0,

                    /// The number of the final page.
                    last_page: last_page,

                    /// List of universes.
                    universes: universes_page_1.clone().into(),
                },
            };

        let discovery_pkt_2: UniverseDiscoveryPacketFramingLayer =
            UniverseDiscoveryPacketFramingLayer {
                source_name: name.into(),

                /// Universe discovery layer.
                data: UniverseDiscoveryPacketUniverseDiscoveryLayer {
                    page: 1,

                    /// The number of the final page.
                    last_page: last_page,

                    /// List of universes.
                    universes: universes_page_2.clone().into(),
                },
            };
        let res: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt_1);

        assert!(res.is_none()); // Should be none because first packet isn't complete as its only the first page.

        let res2: Option<String> = dmx_rcv.handle_universe_discovery_packet(discovery_pkt_2);

        assert!(res2.is_some()); // Source should be discovered because the second and last page is now received.
        assert_eq!(res2.unwrap(), name);

        assert_eq!(dmx_rcv.discovered_sources.len(), 1);

        assert_eq!(dmx_rcv.discovered_sources[0].name, name);
        assert_eq!(dmx_rcv.discovered_sources[0].last_page, last_page);
        assert_eq!(dmx_rcv.discovered_sources[0].pages.len(), 2);
        assert_eq!(dmx_rcv.discovered_sources[0].pages[0].page, 0);
        assert_eq!(dmx_rcv.discovered_sources[0].pages[1].page, 1);
        assert_eq!(
            dmx_rcv.discovered_sources[0].pages[0].universes,
            universes_page_1
        );
        assert_eq!(
            dmx_rcv.discovered_sources[0].pages[1].universes,
            universes_page_2
        );
    }

    #[test]
    fn test_store_retrieve_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let sync_uni: u16 = 1;
        let universe: u16 = 1;
        let vals: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let dmx_data = DMXData {
            universe: universe,
            values: vals.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(dmx_data).unwrap();

        let res: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni);

        assert_eq!(res.len(), 1);
        assert_eq!(res[0].universe, universe);
        assert_eq!(res[0].sync_uni, sync_uni);
        assert_eq!(res[0].values, vals);
    }

    #[test]
    fn test_store_2_retrieve_1_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let sync_uni: u16 = 1;
        let universe: u16 = 1;
        let vals: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let dmx_data = DMXData {
            universe: universe,
            values: vals.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        let dmx_data2 = DMXData {
            universe: universe + 1,
            values: vals.clone(),
            sync_uni: sync_uni + 1,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(dmx_data).unwrap();
        dmx_rcv.store_waiting_data(dmx_data2).unwrap();

        let res: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni);

        assert_eq!(res.len(), 1);
        assert_eq!(res[0].universe, universe);
        assert_eq!(res[0].sync_uni, sync_uni);
        assert_eq!(res[0].values, vals);
    }

    #[test]
    fn test_store_2_retrieve_2_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let sync_uni: u16 = 1;
        let universe: u16 = 1;
        let vals: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let dmx_data = DMXData {
            universe: universe,
            values: vals.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        let vals2: Vec<u8> = vec![0, 9, 7, 3, 2, 4, 5, 6, 5, 1, 2, 3];

        let dmx_data2 = DMXData {
            universe: universe + 1,
            values: vals2.clone(),
            sync_uni: sync_uni + 1,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(dmx_data).unwrap();
        dmx_rcv.store_waiting_data(dmx_data2).unwrap();

        let res: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni);

        assert_eq!(res.len(), 1);
        assert_eq!(res[0].universe, universe);
        assert_eq!(res[0].sync_uni, sync_uni);
        assert_eq!(res[0].values, vals);

        let res2: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni + 1);

        assert_eq!(res2.len(), 1);
        assert_eq!(res2[0].universe, universe + 1);
        assert_eq!(res2[0].sync_uni, sync_uni + 1);
        assert_eq!(res2[0].values, vals2);
    }

    #[test]
    fn test_store_2_same_universe_same_priority_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let sync_uni: u16 = 1;
        let universe: u16 = 1;
        let vals: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let dmx_data = DMXData {
            universe: universe,
            values: vals.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        let vals2: Vec<u8> = vec![0, 9, 7, 3, 2, 4, 5, 6, 5, 1, 2, 3];

        let dmx_data2 = DMXData {
            universe: universe,
            values: vals2.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(dmx_data).unwrap();
        dmx_rcv.store_waiting_data(dmx_data2).unwrap();

        let res2: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni);

        assert_eq!(res2.len(), 1);
        assert_eq!(res2[0].universe, universe);
        assert_eq!(res2[0].sync_uni, sync_uni);
        assert_eq!(res2[0].values, vals2);

        assert_eq!(dmx_rcv.rtrv_waiting_data(sync_uni).len(), 0);
    }

    #[test]
    fn test_store_2_same_universe_diff_priority_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let sync_uni: u16 = 1;
        let universe: u16 = 1;
        let vals: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        let dmx_data = DMXData {
            universe: universe,
            values: vals.clone(),
            sync_uni: sync_uni,
            priority: 120,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        let vals2: Vec<u8> = vec![0, 9, 7, 3, 2, 4, 5, 6, 5, 1, 2, 3];

        let dmx_data2 = DMXData {
            universe: universe,
            values: vals2.clone(),
            sync_uni: sync_uni,
            priority: 100,
            src_cid: None,
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(dmx_data).unwrap();
        dmx_rcv.store_waiting_data(dmx_data2).unwrap(); // Won't be added as lower priority than already waiting data.

        let res: Vec<DMXData> = dmx_rcv.rtrv_waiting_data(sync_uni);

        assert_eq!(res.len(), 1);
        assert_eq!(res[0].universe, universe);
        assert_eq!(res[0].sync_uni, sync_uni);
        assert_eq!(res[0].values, vals);

        assert_eq!(dmx_rcv.rtrv_waiting_data(sync_uni).len(), 0);
    }

    /// Generates a data packet framing layer with arbitrary values except for the sequence number which is set to the given value.
    /// This is used for tests targeted at checking sequence number behaviour that don't care about other fields.
    /// The generated data packet framing layer has structure
    /// DataPacketFramingLayer {
    ///     source_name: "Source_A".into(),
    ///     priority: 100,
    ///     synchronization_address: <given sequence number>,
    ///     sequence_number: sequence_number,
    ///     preview_data: false,
    ///     stream_terminated: false,
    ///     force_synchronization: false,
    ///     universe: <given universe>,
    ///     data: DataPacketDmpLayer {
    ///         property_values: Cow::from(&TEST_DATA_SINGLE_UNIVERSE[0..]),
    ///     },
    /// }
    ///
    fn generate_data_packet_framing_layer_seq_num<'a>(
        universe: u16,
        sequence_number: u8,
    ) -> DataPacketFramingLayer<'a> {
        DataPacketFramingLayer {
            source_name: "Source_A".into(),
            priority: 100,
            synchronization_address: 0,
            sequence_number: sequence_number,
            preview_data: false,
            stream_terminated: false,
            force_synchronization: false,
            universe: universe,
            data: DataPacketDmpLayer {
                property_values: Cow::from(&TEST_DATA_SINGLE_UNIVERSE[0..]),
            },
        }
    }

    /// Generates a sync packet framing layer with arbitrary values except for the sequence number which is set to the given value.
    /// This is used for tests targeted at checking sequence number behaviour that don't care about other fields.
    /// The generated Generates a sync packet framing layer has structure:
    /// SynchronizationPacketFramingLayer {
    ///     sequence_number: <given sequence number>,
    ///     synchronization_address: <given synchronisation address>
    /// }
    ///
    fn generate_sync_packet_framing_layer_seq_num<'a>(
        sync_address: u16,
        sequence_number: u8,
    ) -> SynchronizationPacketFramingLayer {
        SynchronizationPacketFramingLayer {
            sequence_number: sequence_number,
            synchronization_address: sync_address,
        }
    }

    /// Creates a receiver and then makes it handle 2 data packets with sequence numbers 0 and 1 respectively.
    /// The receiver is then given a data packet with sequence number 0 which is the lower than the expected value of 2 so should be rejected.
    ///
    /// This shows that sequence numbers are correctly evaluated and packets rejected if the sequence number is too low for data packets.
    ///  
    #[test]
    fn test_data_packet_sequence_number_below_expected() {
        const UNIVERSE1: u16 = 1;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let data_packet = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 0);
        let data_packet2 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 1);
        let data_packet3 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 0); // This data packet has a sequence number lower than the expected value of 2 so should be rejected.

        // Not interested in specific return values from this test, just assert the data is processed successfully.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected first data packet"
        );
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet2)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected second data packet"
        );

        // Check that the third data packet with the low sequence number is rejected correctly with the expected OutOfSequence error.
        match dmx_rcv.handle_data_packet(src_cid, data_packet3) {
            Err(Error(OutOfSequence(_), _)) => {
                assert!(
                    true,
                    "Receiver correctly rejected third data packet with correct error"
                );
            }
            Ok(_) => {
                assert!(false, "Receiver incorrectly accepted third data packet");
            }
            Err(e) => {
                assert!(false, format!("Receiver correctly rejected third data packet but with unexpected error: {}", e));
            }
        }
    }

    /// Creates a receiver and then makes it handle 2 data packets with sequence numbers 0 and 1 respectively meaning the next expected sequence number should be 2.
    /// The receiver is then given a data packet with sequence number x.
    /// This is repeated for all x in [0, 255].
    ///
    /// This exhaustively checks that only sequence numbers outwith the reject range as specified by ANSI E1.31-2018 Section 6.7.2 are accepted for
    /// data packets specifically.
    ///
    #[test]
    fn test_data_packet_sequence_number_exhaustive() {
        const UNIVERSE1: u16 = 1;
        // The inclusive lower limit used for the sequence numbers tried. Chosen as the minimum value that can fit in an unsigned byte.
        const SEQ_NUM_LOWER_BOUND: u8 = 0;
        // The inclusive upper limit used for the sequence numbers tried. Chosen as the maximum value that can fit in an unsigned byte.
        const SEQ_NUM_UPPER_BOUND: u8 = 255;

        // The last sequence number received before the exhaustive checking.
        const LAST_SEQ_NUM: u8 = 1;

        // Reject range set as per ANSI E1.31-2018 Section 6.7.2 "Having first received a packet with sequence number A, a second packet with sequence number B
        // arrives. If, using signed 8-bit binary arithmetic, B - A is less than or equal to 0, but greater than -20, then
        // the packet containing sequence number B shall be deemed out of sequence and discarded."

        // The inclusive upper bound on the diff values (new_packet_seq_num - last_packet_seq_num) that will be rejected.
        const REJECT_RANGE_UPPER_BOUND: i16 = 0;

        // The exclusive lower bound on the diff values (new_packet_seq_num - last_packet_seq_num) that will be rejected.
        const REJECT_RANGE_LOWER_BOUND: i16 = -20;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        for i in SEQ_NUM_LOWER_BOUND..SEQ_NUM_UPPER_BOUND {
            // Create the receiver.
            let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();
            dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();

            // Generate the packets used to put the receiver in a known start state.
            let data_packet =
                generate_data_packet_framing_layer_seq_num(UNIVERSE1, LAST_SEQ_NUM - 1);
            let data_packet2 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, LAST_SEQ_NUM);

            // Not interested in specific return values from this test, just assert the data is processed successfully.
            assert!(
                dmx_rcv
                    .handle_data_packet(src_cid, data_packet)
                    .unwrap()
                    .is_some(),
                "Receiver incorrectly rejected first data packet"
            );
            assert!(
                dmx_rcv
                    .handle_data_packet(src_cid, data_packet2)
                    .unwrap()
                    .is_some(),
                "Receiver incorrectly rejected second data packet"
            );

            // The receiver is now setup correctly ready for the test with a known start state that expects the next data packet sequence number
            // to be 2.

            let res = dmx_rcv.handle_data_packet(
                src_cid,
                generate_data_packet_framing_layer_seq_num(UNIVERSE1, i),
            );

            let diff: i16 = ((i as i16) - (LAST_SEQ_NUM as i16)) as i16;

            match res {
                Err(Error(OutOfSequence(_), _)) => {
                    // Data packet was rejected due to sequence number.
                    if (diff <= REJECT_RANGE_UPPER_BOUND) && (diff > REJECT_RANGE_LOWER_BOUND) {
                        assert!(
                            true,
                            "Rejection is correct as per ANSI E1.31-2018 Section 6.7.2"
                        );
                    } else {
                        assert!(
                            false,
                            format!(
                                "Data packet with sequence number: {} was rejected incorrectly",
                                i
                            )
                        );
                    }
                }
                Ok(_p) => {
                    // Data packet and therefore sequence number was accepted.
                    if (diff <= REJECT_RANGE_UPPER_BOUND) && (diff > REJECT_RANGE_LOWER_BOUND) {
                        assert!(
                            false,
                            format!(
                                "Data packet with sequence number: {} was accepted incorrectly",
                                i
                            )
                        );
                    } else {
                        assert!(
                            true,
                            "Acceptance is correct as per ANSI E1.31-2018 Section 6.7.2"
                        );
                    }
                }
                Err(e) => {
                    // This is never expected and always means test failure.
                    assert!(false, format!("Receiver produced unexpected error: {}", e));
                }
            }
        }
    }

    /// Exactly the same as test_data_packet_sequence_number_exhaustive but using synchronisation packets.
    ///
    /// This exhaustively checks that only sequence numbers outwith the reject range as specified by ANSI E1.31-2018 Section 6.7.2 are accepted for
    /// synchronisation packets specifically.
    ///
    /// As shown by test_sequence_number_packet_type_independence sequence numbers are treated independently for data and synchronisation packets so
    /// therefore appropriate to test separately. Could have been combined with the data packet variant of this test but by keeping them separate
    /// it more clearly shows that data and sync packet sequence numbers should be treated independently and it report errors independently.
    ///  
    #[test]
    fn test_sync_packet_sequence_number_exhaustive() {
        const SYNC_ADDR: u16 = 1;
        // The inclusive lower limit used for the sequence numbers tried. Chosen as the minimum value that can fit in an unsigned byte.
        const SEQ_NUM_LOWER_BOUND: u8 = 0;
        // The inclusive upper limit used for the sequence numbers tried. Chosen as the maximum value that can fit in an unsigned byte.
        const SEQ_NUM_UPPER_BOUND: u8 = 255;

        // The last sequence number received before the exhaustive checking.
        const LAST_SEQ_NUM: u8 = 1;

        // Reject range set as per ANSI E1.31-2018 Section 6.7.2 "Having first received a packet with sequence number A, a second packet with sequence number B
        // arrives. If, using signed 8-bit binary arithmetic, B - A is less than or equal to 0, but greater than -20, then
        // the packet containing sequence number B shall be deemed out of sequence and discarded."

        // The inclusive upper bound on the diff values (new_packet_seq_num - last_packet_seq_num) that will be rejected.
        const REJECT_RANGE_UPPER_BOUND: i16 = 0;

        // The exclusive lower bound on the diff values (new_packet_seq_num - last_packet_seq_num) that will be rejected.
        const REJECT_RANGE_LOWER_BOUND: i16 = -20;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        for i in SEQ_NUM_LOWER_BOUND..SEQ_NUM_UPPER_BOUND {
            // Create the receiver.
            let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();
            dmx_rcv.listen_universes(&[SYNC_ADDR]).unwrap();

            // Generate the packets used to put the receiver in a known start state.
            let sync_packet =
                generate_sync_packet_framing_layer_seq_num(SYNC_ADDR, LAST_SEQ_NUM - 1);
            let sync_packet2 = generate_sync_packet_framing_layer_seq_num(SYNC_ADDR, LAST_SEQ_NUM);

            // Not interested in specific return values from this test, just assert the sync packet is processed successfully.
            assert!(
                dmx_rcv
                    .handle_sync_packet(src_cid, sync_packet)
                    .unwrap()
                    .is_none(),
                "Receiver incorrectly rejected first sync packet"
            );
            assert!(
                dmx_rcv
                    .handle_sync_packet(src_cid, sync_packet2)
                    .unwrap()
                    .is_none(),
                "Receiver incorrectly rejected second sync packet"
            );

            // The receiver is now setup correctly ready for the test with a known start state that expects the next sync packet sequence number
            // to be 2.

            let res = dmx_rcv.handle_sync_packet(
                src_cid,
                generate_sync_packet_framing_layer_seq_num(SYNC_ADDR, i),
            );

            // Cannot do straight 8 bit arithmetic that relies on underflows/overflows as this is undefined behaviour in rust forbidden by the compiler.
            let diff: i16 = ((i as i16) - (LAST_SEQ_NUM as i16)) as i16;

            match res {
                Err(Error(OutOfSequence(_), _)) => {
                    // Sync packet was rejected due to sequence number.
                    if (diff <= REJECT_RANGE_UPPER_BOUND) && (diff > REJECT_RANGE_LOWER_BOUND) {
                        assert!(
                            true,
                            "Rejection is correct as per ANSI E1.31-2018 Section 6.7.2"
                        );
                    } else {
                        assert!(
                            false,
                            format!(
                                "Sync packet with sequence number: {} was rejected incorrectly",
                                i
                            )
                        );
                    }
                }
                Ok(_p) => {
                    // Sync packet and therefore sequence number was accepted.
                    if (diff <= REJECT_RANGE_UPPER_BOUND) && (diff > REJECT_RANGE_LOWER_BOUND) {
                        assert!(
                            false,
                            format!(
                                "Sync packet with sequence number: {} was accepted incorrectly",
                                i
                            )
                        );
                    } else {
                        assert!(
                            true,
                            "Acceptance is correct as per ANSI E1.31-2018 Section 6.7.2"
                        );
                    }
                }
                Err(e) => {
                    // This is never expected and always means test failure.
                    assert!(false, format!("Receiver produced unexpected error: {}", e));
                }
            }
        }
    }

    /// Creates a receiver and then makes it handle 2 sync packets with sequence numbers 0 and 1 respectively.
    /// The receiver is then given a sync packet with sequence number 0 which is the lower than the expected value of 2 so should be rejected.
    ///
    /// This shows that sequence numbers are correctly evaluated and packets rejected if the sequence number is too low for synchronisation packets.
    ///  
    #[test]
    fn test_sync_packet_sequence_number_below_expected() {
        const UNIVERSE1: u16 = 1;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let sync_packet = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 0);
        let sync_packet2 = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 1);
        let sync_packet3 = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 0); // This sync packet has a sequence number lower than the expected value of 2 so should be rejected.

        // Not interested in specific return values from this test, just assert the packets are processed successfully.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected first sync packet"
        );
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet2)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected second sync packet"
        );

        // Check that the third sync packet with the low sequence number is rejected correctly with the expected OutOfSequence error.
        match dmx_rcv.handle_sync_packet(src_cid, sync_packet3) {
            Err(Error(OutOfSequence(_), _)) => {
                assert!(
                    true,
                    "Receiver correctly rejected third sync packet with correct error"
                );
            }
            Ok(_) => {
                assert!(false, "Receiver incorrectly accepted third sync packet");
            }
            Err(e) => {
                assert!(false, format!("Receiver correctly rejected third sync packet but with unexpected error: {}", e));
            }
        }
    }

    /// Creates a receiver and then makes it handle 2 sync packets with sequence numbers 0 and 1 respectively.
    /// The receiver then resets the sequence number counters and then handles a sync packet with sequence number 0. This would normally be rejected
    /// as per test_sync_packet_sequence_number_below_expected but because of the reset it shouldn't be.
    /// 
    /// This checks that the sync packet sequence numbers are reset correctly.
    /// 
    #[test]
    fn test_sync_packet_sequence_number_reset() {
        const UNIVERSE1: u16 = 1;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let sync_packet = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 0);
        let sync_packet2 = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 1);
        let sync_packet3 = generate_sync_packet_framing_layer_seq_num(UNIVERSE1, 0); // This sync packet has a sequence number lower than the expected value of 2 so should be rejected.

        // Not interested in specific return values from this test, just assert the packets are processed successfully.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected first sync packet"
        );
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet2)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected second sync packet"
        );

        dmx_rcv.reset_sources();

        // Packet shouldn't be rejected.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet3)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected third sync packet"
        );
    }

    /// Creates a receiver and then makes it handle 2 data packets with sequence numbers 0 and 1 respectively.
    /// The receiver then resets the sequence number counters and then handles a data packet with sequence number 0. This would normally be rejected
    /// as per test_data_packet_sequence_number_below_expected but because of the reset it shouldn't be.
    /// 
    /// This checks that the data packet sequence numbers are reset correctly.
    /// 
    #[test]
    fn test_data_packet_sequence_number_reset() {
        const UNIVERSE1: u16 = 1;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE1]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let data_packet = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 0);
        let data_packet2 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 1);
        let data_packet3 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 0); // This data packet has a sequence number lower than the expected value of 2 so should be rejected.

        // Not interested in specific return values from this test, just assert the data is processed successfully.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected first data packet"
        );
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet2)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected second data packet"
        );

        dmx_rcv.reset_sources();

        // Packet shouldn't be rejected.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet3)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected third data packet"
        );
    }

    /// Creates a receiver and then makes it handle 2 data packets with sequence numbers 0 and 1.
    /// This then means the receiver will reject another data packet with sequence number 0.
    /// The receiver is then passed a sync packet with sequence number 0 which shouldn't be rejected as it is a different packet type.
    ///
    /// Shows sequence numbers are evaluated separately for each packet type as per ANSI E1.31-2018 Section 6.7.2.
    ///
    #[test]
    fn test_sequence_number_packet_type_independence() {
        const UNIVERSE: u16 = 1;

        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let data_packet = generate_data_packet_framing_layer_seq_num(UNIVERSE, 0);
        let data_packet2 = generate_data_packet_framing_layer_seq_num(UNIVERSE, 1);

        let sync_packet = generate_sync_packet_framing_layer_seq_num(UNIVERSE, 0);

        // Not interested in specific return values from this test, just assert the data is processed successfully.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected first data packet"
        );
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet2)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected second data packet"
        );

        // At this point the receiver should be expecting data_packet sequence number 2.
        // Pass the receiver a sync packet with sequence number 0.
        // If this isn't rejected it shows that the receiver correctly treats different packet types individually.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected synchronisation packet"
        );
    }

    /// Creates a receiver and then makes it handle 2 data packets for the same universe with sequence numbers 0 and 1.
    /// This then means the receiver will reject another data packet for that universe with sequence number 0.
    /// The receiver is then passed a data packet with sequence number 0 for a different universe which shouldn't be rejected as it is for a different universe.
    ///
    /// Shows sequence numbers are evaluated separately for each universe as per ANSI E1.31-2018 Section 6.7.2.
    ///
    #[test]
    fn test_data_packet_sequence_number_universe_independence() {
        const UNIVERSE1: u16 = 1;
        const UNIVERSE2: u16 = 2;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.listen_universes(&[UNIVERSE1, UNIVERSE2]).unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let data_packet = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 0);
        let data_packet2 = generate_data_packet_framing_layer_seq_num(UNIVERSE1, 1);
        let data_packet3 = generate_data_packet_framing_layer_seq_num(UNIVERSE2, 0);

        // Not interested in specific return values from this test, just assert the data is processed successfully.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected first data packet"
        );
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet2)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected second data packet"
        );

        // At this point the receiver will (as shown by test_data_packet_sequence_number_below_expected) reject a data packet to UNIVERSE1 with sequence number 0
        // however this data packet is for UNIVERSE2 and so therefore should be accepted.
        assert!(
            dmx_rcv
                .handle_data_packet(src_cid, data_packet3)
                .unwrap()
                .is_some(),
            "Receiver incorrectly rejected third data packet"
        );
    }

    /// Creates a receiver and then makes it handle 2 sync packets for the same synchronisation address with sequence numbers 0 and 1.
    /// This then means the receiver will reject another sync packet for that universe with sequence number 0.
    /// The receiver is then passed a sync packet with sequence number 0 for a different synchronisation address which shouldn't be rejected as it is for
    /// a different synchronisation address.
    ///
    /// Shows sequence numbers are evaluated separately for each synchronisation address individually as per ANSI E1.31-2018 Section 6.7.2.
    ///
    #[test]
    fn test_sync_packet_sequence_number_universe_independence() {
        const SYNC_ADDR_1: u16 = 1;
        const SYNC_ADDR_2: u16 = 2;
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);

        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv
            .listen_universes(&[SYNC_ADDR_1, SYNC_ADDR_2])
            .unwrap();

        let src_cid: Uuid = Uuid::from_bytes(&[
            0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6,
            0x14, 0x3e,
        ])
        .unwrap();

        let sync_packet = generate_sync_packet_framing_layer_seq_num(SYNC_ADDR_1, 0);
        let sync_packet2 = generate_sync_packet_framing_layer_seq_num(SYNC_ADDR_1, 1);
        let sync_packet3 = generate_sync_packet_framing_layer_seq_num(SYNC_ADDR_2, 0);

        // Not interested in specific return values from this test, just assert the data is processed successfully.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected first sync packet"
        );
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet2)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected second sync packet"
        );

        // At this point the receiver will (as shown by test_sync_packet_sequence_number_below_expected) reject a sync packet for SYNC_ADDR_1 with sequence number 0
        // however this sync packet is for SYNC_ADDR_2 and so therefore should be accepted.
        assert!(
            dmx_rcv
                .handle_sync_packet(src_cid, sync_packet3)
                .unwrap()
                .is_none(),
            "Receiver incorrectly rejected third sync packet"
        );
    }

    #[test]
    fn test_source_limit_0() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let source_limit: Option<usize> = Some(0);

        match SacnReceiver::with_ip(addr, source_limit) {
            Err(e) => {
                match e.kind() {
                    ErrorKind::Io(x) => {
                        match x.kind() {
                            std::io::ErrorKind::InvalidInput => {
                                assert!(true, "Correct error returned");
                            }
                            _ => {
                                assert!(false, "Expected error returned");
                            }
                        }
                    }
                    _ => {
                        assert!(false, "Unexpected error type returned");
                    }
                }
            }
            _ => {
                assert!(false, "SacnReceiver accepted 0 source limit when it shouldn't");
            }
        }
    }

    #[test]
    fn test_is_multicast_enabled() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        assert!(dmx_rcv.is_multicast_enabled(), "Multicast not enabled by default");
    }

    #[test]
    fn test_set_is_multicast_enabled() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        dmx_rcv.set_is_multicast_enabled(false).unwrap();

        assert!(!dmx_rcv.is_multicast_enabled(), "Multicast not disabled correctly");
    }

    #[test]
    fn test_clear_waiting_data() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        const SYNC_ADDR: u16 = 1;

        let data: DMXData = DMXData {
            universe: 0,
            values: vec![1,2,3],
            sync_uni: SYNC_ADDR,
            priority: 100,
            src_cid: Some(Uuid::new_v4()),
            preview: false,
            recv_timestamp: Instant::now(),
        };

        dmx_rcv.store_waiting_data(data).unwrap();

        dmx_rcv.clear_all_waiting_data();

        assert_eq!(dmx_rcv.rtrv_waiting_data(SYNC_ADDR), Vec::new(), "Data was not reset as expected");
    }

    #[test]
    fn test_get_announce_source_discovery() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        assert!(!dmx_rcv.get_announce_source_discovery(), "Announce source discovery is true by default when should be false");
    }

    #[test]
    fn test_get_announce_timeout() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        assert!(!dmx_rcv.get_announce_timeout(), "Announce timeout flag is true by default when should be false");
    }

    #[test]
    fn test_get_announce_stream_termination() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        assert!(!dmx_rcv.get_announce_stream_termination(), "Announce termination flag is true by default when should be false");
    }

    /// Tests handling a sync packet for a synchronisation address which isn't currently being listened to.
    #[test]
    fn test_handle_sync_packet_not_listening_to_sync_addr() {
        let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), ACN_SDT_MULTICAST_PORT);
        let mut dmx_rcv = SacnReceiver::with_ip(addr, None).unwrap();

        let res = dmx_rcv.handle_sync_packet(Uuid::new_v4(), SynchronizationPacketFramingLayer{
            sequence_number: 0,
            synchronization_address: 1
        }).unwrap(); // Checks that no error is produced.

        assert_eq!(res, None, "Sync packet produced output when should have been ignored as for an address that isn't being listened to");
    }

    /// Tests the equivalence of 2 DMXDatas which are only similar in the aspects used for checking equivalence.
    #[test]
    fn test_dmx_data_eq() {
        const UNIVERSE: u16 = 1;
        let values: Vec<u8> = vec!(1,2,3);
        const SYNC_ADDR: u16 = 1;
        const PRIORITY: u8 = 100;
        const PREVIEW: bool = false;

        let data1 = DMXData {
            universe: UNIVERSE,
            values: values.clone(),
            sync_uni: SYNC_ADDR,

            // The below values can be different for 2 DMXData to be taken as equivalent.
            priority: PRIORITY,
            src_cid: Some(Uuid::new_v4()),
            preview: PREVIEW,
            recv_timestamp: Instant::now(),
        };
        
        let data2 = DMXData {
            universe: UNIVERSE,
            values: values,
            sync_uni: SYNC_ADDR,

            // The below values can be different for 2 DMXData to be taken as equivalent.
            priority: PRIORITY + 50,
            src_cid: None,
            preview: !PREVIEW,
            recv_timestamp: Instant::now(),
        };

        assert_eq!(data1, data2, "DMX data not seen as equivalent when should be");
    }
}