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
#![allow(clippy::type_complexity)]

use std::any::{type_name, Any, TypeId};
use std::cmp::max;
use std::error::Error;
use std::fmt::Debug;
use std::sync::Arc;
use std::time::Duration;

use bimap::BiMap;
use bytes::Bytes;
use hashbrown::HashMap;
use indexmap::IndexMap;
use log::{debug, error, trace, warn};
use quinn::{Connection, Endpoint, ReadError, RecvStream, SendStream, VarInt, WriteError};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::runtime::Runtime;
use tokio::sync::mpsc::error::TryRecvError;
use tokio::sync::mpsc::Receiver;
use tokio::sync::{mpsc, Mutex, RwLock};
use tokio::task::JoinHandle;

use crate::quinn_helpers::{make_client_endpoint, make_server_endpoint};
use crate::{CloseError, ConnectionError, ErrorType, ReceiveError, SendError};

#[cfg(test)]
#[path = "./packet_tests.rs"]
mod packet_tests;

// Sent at the end of frames
const FRAME_BOUNDARY: &[u8] = b"AAAAAA031320050421";

/// Packet trait that allows a struct to be sent through [`PacketManager`], for serializing
///
/// This is automatically implemented if using the macros [`bincode_packet`](`durian_macros::bincode_packet`),
/// [`BinPacket`](`durian_macros::BinPacket`), or [`UnitPacket`](`durian_macros::UnitPacket`).
pub trait Packet {
    /// Return a serialized form of the Packet as [`Bytes`]
    fn as_bytes(&self) -> Bytes;

    // https://stackoverflow.com/questions/33687447/how-to-get-a-reference-to-a-concrete-type-from-a-trait-object
    // fn as_any(self: Self) -> Box<dyn Any>;
}

/// PacketBuilder is the deserializer for [`Packet`] and used when [`PacketManager`] receives bytes
///
/// This is automatically implemented if using the macros [`bincode_packet`](`durian_macros::bincode_packet`),
/// [`BinPacket`](`durian_macros::BinPacket`), or [`UnitPacket`](`durian_macros::UnitPacket`).
pub trait PacketBuilder<T: Packet> {
    /// Deserializes [`Bytes`] into the [`Packet`] this PacketBuilder is implemented for
    ///
    /// # Error
    /// Returns an error if deserializing fails
    fn read(&self, bytes: Bytes) -> Result<T, Box<dyn Error>>;
}

/// The core of `durian` that is the central struct containing all the necessary APIs for initiating and managing
/// connections, creating streams, sending [`Packets`](`Packet`), receiving, broadcasting, etc.
///
/// A `PacketManager` would be created on each client to connect to a
/// single server, and one created on the server to connect to multiple clients. It contains both
/// synchronous and asynchronous APIs, so you can call the functions both from a synchronous
/// context, or within an async runtime (_Note: the synchronous path will create a separate
/// isolated async runtime context per `PacketManager` instance._)
///
/// There are 4 basic steps to using the `PacketManager`, which would be done on both the client
/// and server side:
///
/// 1. Create a `PacketManager` via [`new()`](`PacketManager::new()`) or, if calling from an async context, [`new_for_async()`](`PacketManager::new_for_async()`)
///
/// 2. Register the [`Packets`](`Packet`) and [`PacketBuilders`](`PacketBuilder`) that the `PacketManager` will __receive__
/// and __send__ using [`register_receive_packet()`](`PacketManager::register_receive_packet()`) and [`register_send_packet()`](`PacketManager::register_send_packet()`).  
/// The ordering of `Packet` registration matters for the `receive` channel and
/// `send` channel each - the client and server must register the same packets in the same order,
/// for the opposite channels.  
///     - In other words, the client must register `receive` packets in the
/// same order the server registers the same as `send` packets, and vice versa, the client must
/// register `send` packets in the same order the server registers the same as `receive` packets.
/// This helps to ensure the client and servers are in sync on what Packets to send/receive, almost
/// like ensuring they are on the same "version" so to speak, and is used to properly identify
/// Packets.
///
/// 3. Initiate connection(s) with [`init_client()`](`PacketManager::init_client()`) (or the async variant [`async_init_client()`](`PacketManager::async_init_client()`)
/// if on the client side, else use [`init_server()`](`PacketManager::init_server()`) (or the async variant [`async_init_server)`](`PacketManager::async_init_server()`)
/// if on the server side.
///
/// 4. Send packets using any of [`broadcast()`](`PacketManager::broadcast()`), [`send()`](`PacketManager::send()`), [`send_to()`](`PacketManager::send_to()`)
/// or the respective `async` variants if calling from an async context already.  Receive packets
/// using any of [`received_all()`](`PacketManager::received_all()`) , [`received()`](`PacketManager::received()`), or the respective
/// `async` variants.
///
/// # Example
///
/// ```rust
/// use durian::{ClientConfig, PacketManager};
/// use durian_macros::bincode_packet;
///
/// #[bincode_packet]
/// struct Position { x: i32, y: i32 }
/// #[bincode_packet]
/// struct ServerAck;
/// #[bincode_packet]
/// struct ClientAck;
/// #[bincode_packet]
/// struct InputMovement { direction: String }
///
/// fn packet_manager_example() {
///     // Create PacketManager
///     let mut manager = PacketManager::new();
///
///     // Register send and receive packets
///     manager.register_receive_packet::<Position>(PositionPacketBuilder).unwrap();
///     manager.register_receive_packet::<ServerAck>(ServerAckPacketBuilder).unwrap();
///     manager.register_send_packet::<ClientAck>().unwrap();
///     manager.register_send_packet::<InputMovement>().unwrap();
///
///     // Initialize a client
///     let client_config = ClientConfig::new("127.0.0.1:5001", "127.0.0.1:5000", 2, 2);
///     manager.init_client(client_config).unwrap();
///
///     // Send and receive packets
///     manager.broadcast(InputMovement { direction: "North".to_string() }).unwrap();
///     manager.received_all::<Position, PositionPacketBuilder>(false).unwrap();
///
///     // The above PacketManager is for the client.  Server side is similar except the packets
///     // are swapped between receive vs send channels:
///
///     // Create PacketManager
///     let mut server_manager = PacketManager::new();
///
///     // Register send and receive packets
///     server_manager.register_receive_packet::<ClientAck>(ClientAckPacketBuilder).unwrap();
///     server_manager.register_receive_packet::<InputMovement>(InputMovementPacketBuilder).unwrap();
///     server_manager.register_send_packet::<Position>().unwrap();
///     server_manager.register_send_packet::<ServerAck>().unwrap();
///
///     // Initialize a client
///     let client_config = ClientConfig::new("127.0.0.1:5001", "127.0.0.1:5000", 2, 2);
///     server_manager.init_client(client_config).unwrap();
///
///     // Send and receive packets
///     server_manager.broadcast(Position { x: 1, y: 3 }).unwrap();
///     server_manager.received_all::<InputMovement, InputMovementPacketBuilder>(false).unwrap();
/// }
/// ```
#[derive(Debug)]
pub struct PacketManager {
    /// Packet Id <-> Packet TypeId
    receive_packets: BiMap<u32, TypeId>,
    /// Packet Id <-> Packet TypeId
    send_packets: BiMap<u32, TypeId>,
    recv_packet_builders: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
    // DenseSlotMap for the below so we can iterate fast, while not degrading insert/remove much
    /// Send streams RemoteId -> Packet Id -> SendStream
    send_streams: IndexMap<u32, HashMap<u32, RwLock<SendStream>>>,
    /// Receive channels, to be filled by separate threads
    /// RemoteId -> PacketId -> (Receiver channel, Thread JoinHandle)
    rx: IndexMap<u32, HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>>,
    /// Track new send streams to be put into the send_streams IndexMap
    /// This is so new send streams can be created after PacketManager initialization
    new_send_streams: Arc<RwLock<Vec<(u32, HashMap<u32, RwLock<SendStream>>)>>>,
    /// Track new receive streams to be put into the rx IndexMap
    /// This is so new receive streams can be created after PacketManager initialization
    new_rxs: Arc<RwLock<Vec<(u32, HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>)>>>,
    // Endpoint and Connection structs moved to the struct fields to prevent closing connections
    // by dropping.
    // This is also used to count the number of clients when broadcasting
    /// Remote Id (index in IndexMaps) -> (addr, Connection)
    remote_connections: Arc<RwLock<HashMap<u32, (String, Connection)>>>,
    // (Socket Address, Endpoint)
    source: (String, Option<Arc<Endpoint>>),
    /// For tracking receive packet Ids
    next_receive_id: u32,
    /// For tracking send packet Ids
    next_send_id: u32,
    // We construct a single Tokio Runtime to be used by each PacketManger instance, so that
    // methods can be synchronous.  There is also an async version of each API if the user wants
    // to use their own runtime.
    runtime: Arc<Option<Runtime>>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
/// Client configuration for initiating a connection from a client to a server via [`PacketManager::init_client()`]
pub struct ClientConfig {
    /// Socket address of the client (`hostname:port`) (e.g. "127.0.0.1:5001")
    pub addr: String,
    /// Socket address of the server to connect to (`hostname:port`) (e.g. "127.0.0.1:5000")
    pub server_addr: String,
    /// Number of `receive` streams to accept from server to client.  Must be equal to number of receive packets
    /// registered through [`PacketManager::register_receive_packet()`]
    pub num_receive_streams: u32,
    /// Number of `send` streams to open from client to server.  Must be equal to number of send packets registered
    /// through [`PacketManager::register_send_packet()`]
    pub num_send_streams: u32,
    /// Period of inactivity before sending a keep-alive packet
    ///
    /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
    ///
    /// None to disable, which is the default. Only one side of any given connection needs keep-alive enabled for
    /// the connection to be preserved. Must be set lower than the idle_timeout of both peers to be effective.
    pub keep_alive_interval: Option<Duration>,
    /// Maximum duration of inactivity to accept before timing out the connection.
    /// The true idle timeout is the minimum of this and the peer's own max idle timeout. Defaults to 60 seconds.
    /// None represents an infinite timeout.
    ///
    /// __IMPORTANT: In the case of clients disconnecting abruptly, i.e. your application cannot call
    /// [`PacketManager::close_connection()`] or [`PacketManager::finish_connection()`] gracefully, the *true idle timeout* will help to remove
    /// disconnected clients from the connection queue, thus allowing them to reconnect after that timeout frame.__
    ///
    /// __WARNING: If a peer or its network path malfunctions or acts maliciously, an infinite idle timeout can result
    /// in permanently hung futures!__
    pub idle_timeout: Option<Duration>,

    /// Protocols to send to server if applicable.
    ///
    /// ## Example:
    ///
    /// ```
    /// use durian::ServerConfig;
    ///
    /// let mut config = ServerConfig::new("127.0.0.1:5000", 0, None, 2, 2);
    /// config.with_alpn_protocols(&[b"hq-29"]);
    /// ```
    pub alpn_protocols: Option<Vec<Vec<u8>>>,
}

impl ClientConfig {
    /// Construct and return a new [`ClientConfig`]
    pub fn new<S: Into<String>>(
        addr: S,
        server_addr: S,
        num_receive_streams: u32,
        num_send_streams: u32,
    ) -> Self {
        ClientConfig {
            addr: addr.into(),
            server_addr: server_addr.into(),
            num_receive_streams,
            num_send_streams,
            keep_alive_interval: None,
            idle_timeout: Some(Duration::from_secs(60)),
            alpn_protocols: None,
        }
    }

    /// Set the keep alive interval
    pub fn with_keep_alive_interval(&mut self, duration: Duration) -> &mut Self {
        self.keep_alive_interval = Some(duration);
        self
    }

    /// Set the idle timeout
    pub fn with_idle_timeout(&mut self, duration: Duration) -> &mut Self {
        self.idle_timeout = Some(duration);
        self
    }

    /// Set the ALPN protocols
    pub fn with_alpn_protocols(&mut self, protocols: &[&[u8]]) -> &mut Self {
        self.alpn_protocols = Some(protocols.iter().map(|&x| x.into()).collect());
        self
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
/// Server configuration for spinning up a server on a socket address via [`PacketManager::init_server()`]
pub struct ServerConfig {
    /// Socket address of the server (`hostname:port`) (e.g. "127.0.0.1:5001")
    pub addr: String,
    /// Number of clients to block waiting for incoming connections
    pub wait_for_clients: u32,
    /// [Optional] Total number of clients the server expects to connect with.  The server will spin up a thread that
    /// waits for incoming client connections until `total_expected_clients` is reached.  Set to `None` to allow any
    /// number of clients connections.  If `wait_for_clients > total_expected_clients`, the server will still wait
    /// for `wait_for_clients` number of client connections.
    pub total_expected_clients: Option<u32>,
    /// Set the max number of concurrent connection accepts to process at any given time.
    ///
    /// A thread will be spawned for each, allowing `max_concurrent_accepts` connections to come in at the same time.
    /// Default to `wait_for_clients + total_expected_clients(if set)`, else number of cores available.
    pub max_concurrent_accepts: u32,
    /// Number of `receive` streams to accept from server to client.  Must be equal to number of receive packets
    /// registered through [`PacketManager::register_receive_packet()`]
    pub num_receive_streams: u32,
    /// Number of `send` streams to open from client to server.  Must be equal to number of send packets registered
    /// through [`PacketManager::register_send_packet()`]
    pub num_send_streams: u32,
    /// Period of inactivity before sending a keep-alive packet
    ///
    /// Keep-alive packets prevent an inactive but otherwise healthy connection from timing out.
    ///
    /// None to disable, which is the default. Only one side of any given connection needs keep-alive enabled for
    /// the connection to be preserved. Must be set lower than the idle_timeout of both peers to be effective.
    pub keep_alive_interval: Option<Duration>,
    /// Maximum duration of inactivity to accept before timing out the connection.
    /// The true idle timeout is the minimum of this and the peer's own max idle timeout. Defaults to 60 seconds.
    /// None represents an infinite timeout.
    ///
    /// __IMPORTANT: In the case of clients disconnecting abruptly, i.e. your application cannot call
    /// [`PacketManager::close_connection()`] or [`PacketManager::finish_connection()`] gracefully, the *true idle timeout* will help to remove
    /// disconnected clients from the connection queue, thus allowing them to reconnect after that timeout frame.__
    ///
    /// __WARNING: If a peer or its network path malfunctions or acts maliciously, an infinite idle timeout can result
    /// in permanently hung futures!__
    pub idle_timeout: Option<Duration>,

    /// Protocols to send to server if applicable.
    ///
    /// ## Example:
    ///
    /// ```
    /// use durian::ServerConfig;
    ///
    /// let mut config = ServerConfig::new("127.0.0.1:5000", 0, None, 2, 2);
    /// config.with_alpn_protocols(&[b"hq-29"]);
    /// ```
    pub alpn_protocols: Option<Vec<Vec<u8>>>,
}

impl ServerConfig {
    /// Construct and return a new [`ServerConfig`]
    pub fn new<S: Into<String>>(
        addr: S,
        wait_for_clients: u32,
        total_expected_clients: Option<u32>,
        num_receive_streams: u32,
        num_send_streams: u32,
    ) -> Self {
        // If total_expected_clients is erroneously set to lower than wait_for_clients, just set it to 0
        let mut expected_clients =
            if let Some(expected) = total_expected_clients { expected } else { 0 };
        expected_clients = if expected_clients > wait_for_clients {
            expected_clients - wait_for_clients
        } else {
            0
        };
        ServerConfig {
            addr: addr.into(),
            wait_for_clients,
            total_expected_clients,
            max_concurrent_accepts: max(num_cpus::get() as u32, expected_clients),
            num_receive_streams,
            num_send_streams,
            keep_alive_interval: None,
            idle_timeout: Some(Duration::from_secs(60)),
            alpn_protocols: None,
        }
    }

    /// Construct and return a new [`ServerConfig`] that only allows up to `wait_for_clients` number of client connections
    pub fn new_with_max_clients<S: Into<String>>(
        addr: S,
        wait_for_clients: u32,
        num_receive_streams: u32,
        num_send_streams: u32,
    ) -> Self {
        ServerConfig {
            addr: addr.into(),
            wait_for_clients,
            total_expected_clients: Some(wait_for_clients),
            max_concurrent_accepts: num_cpus::get() as u32,
            num_receive_streams,
            num_send_streams,
            keep_alive_interval: None,
            idle_timeout: Some(Duration::from_secs(60)),
            alpn_protocols: None,
        }
    }

    /// Construct and return a new [`ServerConfig`], with [`total_expected_clients`](`ServerConfig::total_expected_clients`) set to `None` so the server
    /// continuously accepts new client connections
    pub fn new_listening<S: Into<String>>(
        addr: S,
        wait_for_clients: u32,
        num_receive_streams: u32,
        num_send_streams: u32,
    ) -> Self {
        ServerConfig {
            addr: addr.into(),
            wait_for_clients,
            total_expected_clients: None,
            max_concurrent_accepts: num_cpus::get() as u32,
            num_receive_streams,
            num_send_streams,
            keep_alive_interval: None,
            idle_timeout: Some(Duration::from_secs(60)),
            alpn_protocols: None,
        }
    }

    /// Set the keep alive interval
    pub fn with_keep_alive_interval(&mut self, duration: Duration) -> &mut Self {
        self.keep_alive_interval = Some(duration);
        self
    }

    /// Set the max idle timeout
    pub fn with_idle_timeout(&mut self, duration: Option<Duration>) -> &mut Self {
        self.idle_timeout = duration;
        self
    }

    /// Set the ALPN protocols
    pub fn with_alpn_protocols(&mut self, protocols: &[&[u8]]) -> &mut Self {
        self.alpn_protocols = Some(protocols.iter().map(|&x| x.into()).collect());
        self
    }

    /// Set the max concurrent accept connections
    pub fn with_max_concurrent_accepts(&mut self, max_concurrent_accepts: u32) -> &mut Self {
        self.max_concurrent_accepts = max_concurrent_accepts;
        self
    }
}

// TODO: Allow closing Endpoint directly, along with its threads
impl PacketManager {
    /// Create a new `PacketManager`
    ///
    /// If calling from an asynchronous context/runtime, use the [`new_for_async()`](`PacketManager::new_for_async()`) variant.
    /// This constructs a [`tokio Runtime`](`Runtime`) for the `PacketManager` instance.
    ///
    /// # Panic
    /// If the [`Runtime`] could not be created.  Usually happens if you call `new()` from an existing async runtime.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build();
        match runtime {
            Ok(runtime) => PacketManager {
                receive_packets: BiMap::new(),
                send_packets: BiMap::new(),
                recv_packet_builders: HashMap::new(),
                send_streams: IndexMap::new(),
                rx: IndexMap::new(),
                new_send_streams: Arc::new(RwLock::new(vec![])),
                new_rxs: Arc::new(RwLock::new(Vec::new())),
                remote_connections: Arc::new(RwLock::new(HashMap::new())),
                source: ("".to_string(), None),
                next_receive_id: 0,
                next_send_id: 0,
                runtime: Arc::new(Some(runtime)),
            },
            Err(e) => {
                panic!("Could not create a Tokio runtime for PacketManager.  If you are calling new() from code that already has an async runtime available, use PacketManager.new_async(), and respective async_*() versions of APIs.  --  {}", e);
            }
        }
    }

    /// Create a new `PacketManager`
    ///
    /// If calling from a synchronous context, use [`new()`](`PacketManager::new()`)
    pub fn new_for_async() -> Self {
        PacketManager {
            receive_packets: BiMap::new(),
            send_packets: BiMap::new(),
            recv_packet_builders: HashMap::new(),
            send_streams: IndexMap::new(),
            rx: IndexMap::new(),
            new_send_streams: Arc::new(RwLock::new(vec![])),
            new_rxs: Arc::new(Default::default()),
            remote_connections: Arc::new(RwLock::new(HashMap::new())),
            source: ("".to_string(), None),
            next_receive_id: 0,
            next_send_id: 0,
            runtime: Arc::new(None),
        }
    }

    /// Initialize a client side `PacketManager`
    ///
    /// # Arguments
    ///
    /// * `client_config` - Client configuration
    ///
    /// # Returns
    /// A [`Result`] containing `()` if successful, else a [`ConnectionError`] on error
    ///
    /// # Panics
    /// When the `PacketManager` does not have a runtime instance associated with it, which can happen if you created
    /// the `PacketManager` using [`new_for_async()`](`PacketManager::new_for_async()`) instead of [`new()`](`PacketManager::new()`).
    pub fn init_client(&mut self, client_config: ClientConfig) -> Result<(), ConnectionError> {
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_init_client()?");
            }
            Some(runtime) => {
                self.validate_connection_prereqs(
                    client_config.num_receive_streams,
                    client_config.num_send_streams,
                )?;
                self.source.0 = client_config.addr.clone();
                runtime.block_on(PacketManager::init_client_helper(
                    client_config,
                    &self.runtime,
                    &self.new_rxs,
                    &self.new_send_streams,
                    &self.remote_connections,
                    &mut self.source.1,
                ))
            }
        }
    }

    /// Initialize a client side `PacketManager`, to be used if calling from an async context
    ///
    /// # Arguments
    ///
    /// * `client_config` - Client configuration
    ///
    /// # Returns
    /// A `Future` that returns a [`Result`] containing `()` if successful, else a [`ConnectionError`] on error
    ///
    /// # Panics
    /// When the `PacketManager` has a runtime instance associated with it, which can happen if you created
    /// the `PacketManager` using [`new()`](`PacketManager::new()`) instead of [`new_for_async()`](`PacketManager::new_for_async()`).
    pub async fn async_init_client(
        &mut self,
        client_config: ClientConfig,
    ) -> Result<(), ConnectionError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_init_client(), make sure you create the PacketManager using new_for_async(), not new()");
        }
        self.validate_connection_prereqs(
            client_config.num_receive_streams,
            client_config.num_send_streams,
        )?;
        self.source.0 = client_config.addr.clone();
        PacketManager::init_client_helper(
            client_config,
            &self.runtime,
            &self.new_rxs,
            &self.new_send_streams,
            &self.remote_connections,
            &mut self.source.1,
        )
        .await
    }

    /// Initialize a server side `PacketManager`
    ///
    /// # Arguments
    ///
    /// * `server_config` - Server configuration
    ///
    /// # Returns
    /// A [`Result`] containing `()` if successful, else a [`ConnectionError`] on error
    ///
    /// # Panics
    /// When the `PacketManager` does not have a runtime instance associated with it, which can happen if you created
    /// the `PacketManager` using [`new_for_async()`](`PacketManager::new_for_async()`) instead of [`new()`](`PacketManager::new()`).
    pub fn init_server(&mut self, server_config: ServerConfig) -> Result<(), ConnectionError> {
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_init_server()?");
            }
            Some(runtime) => {
                self.validate_connection_prereqs(
                    server_config.num_receive_streams,
                    server_config.num_send_streams,
                )?;
                self.source.0 = server_config.addr.clone();
                runtime.block_on(PacketManager::init_server_helper(
                    server_config,
                    &self.runtime,
                    &self.new_rxs,
                    &self.new_send_streams,
                    &self.remote_connections,
                    &mut self.source.1,
                ))
            }
        }
    }

    /// Initialize a server side `PacketManager`, to be used if calling from an async context
    ///
    /// # Arguments
    ///
    /// * `server_config` - Server configuration
    ///
    /// # Returns
    /// A `Future` that returns a [`Result`] containing `()` if successful, else a [`ConnectionError`] on error
    ///
    /// # Panics
    /// When the `PacketManager` has a runtime instance associated with it, which can happen if you created
    /// the `PacketManager` using [`new()`](`PacketManager::new()`) instead of [`new_for_async()`](`PacketManager::new_for_async()`).
    pub async fn async_init_server(
        &mut self,
        server_config: ServerConfig,
    ) -> Result<(), ConnectionError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_init_server(), make sure you create the PacketManager using new_for_async(), not new()");
        }
        self.validate_connection_prereqs(
            server_config.num_receive_streams,
            server_config.num_send_streams,
        )?;
        self.source.0 = server_config.addr.clone();
        PacketManager::init_server_helper(
            server_config,
            &self.runtime,
            &self.new_rxs,
            &self.new_send_streams,
            &self.remote_connections,
            &mut self.source.1,
        )
        .await
    }

    fn validate_connection_prereqs(
        &self,
        num_incoming_streams: u32,
        num_outgoing_streams: u32,
    ) -> Result<(), ConnectionError> {
        let num_receive_packets = self.receive_packets.len() as u32;
        if num_receive_packets != num_incoming_streams {
            return Err(ConnectionError::new(format!("num_incoming_streams={} does not match number of registered receive packets={}.  Did you forget to call register_receive_packet()?", num_incoming_streams, num_receive_packets)));
        }
        let num_send_packets = self.send_packets.len() as u32;
        if num_send_packets != num_outgoing_streams {
            return Err(ConnectionError::new(format!("num_outgoing_streams={} does not match number of registered send packets={}.  Did you forget to call register_send_packet()?", num_incoming_streams, num_send_packets)));
        }
        Ok(())
    }

    async fn init_server_helper(
        server_config: ServerConfig,
        runtime: &Arc<Option<Runtime>>,
        new_rxs: &Arc<RwLock<Vec<(u32, HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>)>>>,
        new_send_streams: &Arc<RwLock<Vec<(u32, HashMap<u32, RwLock<SendStream>>)>>>,
        client_connections: &Arc<RwLock<HashMap<u32, (String, Connection)>>>,
        source_endpoint: &mut Option<Arc<Endpoint>>,
    ) -> Result<(), ConnectionError> {
        debug!("Initiating server with {:?}", server_config);

        let (endpoint, server_cert) = make_server_endpoint(
            server_config.addr.parse()?,
            server_config.keep_alive_interval,
            server_config.idle_timeout,
            server_config.alpn_protocols,
        )?;
        let endpoint = Arc::new(endpoint);
        let _ = source_endpoint.insert(Arc::clone(&endpoint));
        let num_receive_streams = server_config.num_receive_streams;
        let num_send_streams = server_config.num_send_streams;

        // TODO: use synchronous blocks during read and write of client_connections
        // Single connection
        for i in 0..server_config.wait_for_clients {
            let incoming_conn = endpoint.accept().await.unwrap();
            let conn = incoming_conn.await?;
            let addr = conn.remote_address();
            if client_connections.read().await.contains_key(&i) {
                panic!(
                    "[server] Client with addr={} was already connected as remote_id={}",
                    addr, i
                );
            }
            println!(
                "[server] connection accepted: addr={}, remote_id={}",
                conn.remote_address(),
                i
            );
            let (server_send_streams, recv_streams) = PacketManager::open_streams_for_connection(
                i,
                &conn,
                num_receive_streams,
                num_send_streams,
            )
            .await?;
            let res = PacketManager::spawn_receive_thread(i, recv_streams, runtime.as_ref())?;
            new_rxs.write().await.push((i, res));
            new_send_streams.write().await.push((i, server_send_streams));
            client_connections.write().await.insert(i, (addr.to_string(), conn));
        }

        if server_config.total_expected_clients.is_none()
            || server_config.total_expected_clients.unwrap() > server_config.wait_for_clients
        {
            let remote_id = Arc::new(Mutex::new(server_config.wait_for_clients));

            // TODO: save this value
            for i in 0..server_config.max_concurrent_accepts {
                debug!("Spinning up client connection accept thread #{}", i);

                let client_connections = client_connections.clone();
                let arc_send_streams = new_send_streams.clone();
                let arc_rx = new_rxs.clone();
                let arc_runtime = Arc::clone(runtime);
                let endpoint = Arc::clone(&endpoint);
                let remote_id_clone = Arc::clone(&remote_id);

                // TODO: refactor
                let accept_client_task = async move {
                    match server_config.total_expected_clients {
                        None => loop {
                            debug!("[server] Waiting for more clients...");
                            let incoming_conn = endpoint.accept().await.unwrap();
                            let conn = incoming_conn.await?;
                            let addr = conn.remote_address();
                            let mut id = remote_id_clone.lock().await;
                            if client_connections.read().await.contains_key(&*id) {
                                panic!("[server] Client with addr={} was already connected as remote_id={}", addr, id);
                            }
                            debug!("[server] connection accepted: addr={}", conn.remote_address());
                            let (send_streams, recv_streams) =
                                PacketManager::open_streams_for_connection(
                                    *id,
                                    &conn,
                                    num_receive_streams,
                                    num_send_streams,
                                )
                                .await?;
                            let res = PacketManager::spawn_receive_thread(
                                *id,
                                recv_streams,
                                arc_runtime.as_ref(),
                            )?;
                            arc_rx.write().await.push((*id, res));
                            arc_send_streams.write().await.push((*id, send_streams));
                            client_connections.write().await.insert(*id, (addr.to_string(), conn));
                            *id += 1;
                        },
                        Some(expected_num_clients) => {
                            for i in 0..(expected_num_clients - server_config.wait_for_clients) {
                                debug!(
                                    "[server] Waiting for client #{}",
                                    i + server_config.wait_for_clients
                                );
                                let incoming_conn = endpoint.accept().await.unwrap();
                                let conn = incoming_conn.await?;
                                let addr = conn.remote_address();
                                let mut id = remote_id_clone.lock().await;
                                if client_connections.read().await.contains_key(&*id) {
                                    panic!(
                                        "[server] Client with addr={} was already connected as remote_id={}",
                                        addr, id
                                    );
                                }
                                debug!(
                                    "[server] connection accepted: addr={}",
                                    conn.remote_address()
                                );
                                let (send_streams, recv_streams) =
                                    PacketManager::open_streams_for_connection(
                                        *id,
                                        &conn,
                                        num_receive_streams,
                                        num_send_streams,
                                    )
                                    .await?;
                                let res = PacketManager::spawn_receive_thread(
                                    *id,
                                    recv_streams,
                                    arc_runtime.as_ref(),
                                )?;
                                arc_rx.write().await.push((*id, res));
                                arc_send_streams.write().await.push((*id, send_streams));
                                client_connections
                                    .write()
                                    .await
                                    .insert(*id, (addr.to_string(), conn));
                                *id += 1;
                            }
                        }
                    }
                    Ok::<(), Box<ConnectionError>>(())
                };

                let accept_client_thread = match runtime.as_ref() {
                    None => tokio::spawn(accept_client_task),
                    Some(runtime) => runtime.spawn(accept_client_task),
                };
            }
        }

        Ok(())
    }

    // TODO: Add support for creating a client PacketManager using an existing endpoint
    async fn init_client_helper(
        client_config: ClientConfig,
        runtime: &Arc<Option<Runtime>>,
        new_rxs: &Arc<RwLock<Vec<(u32, HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>)>>>,
        new_send_streams: &Arc<RwLock<Vec<(u32, HashMap<u32, RwLock<SendStream>>)>>>,
        // For bookkeeping, but technically the "client_connection" will be the server only
        client_connections: &Arc<RwLock<HashMap<u32, (String, Connection)>>>,
        source_endpoint: &mut Option<Arc<Endpoint>>,
    ) -> Result<(), ConnectionError> {
        debug!("Initiating client with {:?}", client_config);
        // Bind this endpoint to a UDP socket on the given client address.
        let endpoint = make_client_endpoint(
            client_config.addr.parse()?,
            &[],
            client_config.keep_alive_interval,
            client_config.idle_timeout,
            client_config.alpn_protocols,
        )?;
        let endpoint = Arc::new(endpoint);
        let _ = source_endpoint.insert(Arc::clone(&endpoint));

        // Connect to the server passing in the server name which is supposed to be in the server certificate.
        let conn = endpoint.connect(client_config.server_addr.parse()?, "server")?.await?;
        let addr = conn.remote_address();
        debug!("[client] connected: addr={}", addr);
        let (client_send_streams, recv_streams) = PacketManager::open_streams_for_connection(
            0,
            &conn,
            client_config.num_receive_streams,
            client_config.num_send_streams,
        )
        .await?;
        let res = PacketManager::spawn_receive_thread(0, recv_streams, runtime.as_ref())?;
        // Client side defaults to only having the 1 server at "remote_id" 0
        new_rxs.write().await.push((0, res));
        new_send_streams.write().await.push((0, client_send_streams));
        client_connections.write().await.insert(0, (addr.to_string(), conn));
        Ok(())
    }

    async fn open_streams_for_connection(
        remote_id: u32,
        conn: &Connection,
        num_incoming_streams: u32,
        num_outgoing_streams: u32,
    ) -> Result<(HashMap<u32, RwLock<SendStream>>, HashMap<u32, RecvStream>), Box<dyn Error>> {
        let mut send_streams = HashMap::new();
        let mut recv_streams = HashMap::new();
        // Note: Packets are not sent immediately upon the write.  The thread needs to be kept
        // open so that the packets can actually be sent over the wire to the client.
        for i in 0..num_outgoing_streams {
            trace!("Opening outgoing stream for remote_id={} packet id={}", remote_id, i);
            let mut send_stream = conn.open_uni().await?;
            trace!("Writing packet to {} for packet id {}", remote_id, i);
            send_stream.write_u32(i).await?;
            send_streams.insert(i, RwLock::new(send_stream));
        }

        for i in 0..num_incoming_streams {
            trace!("Accepting incoming stream from {} for packet id {}", remote_id, i);
            let mut recv = conn.accept_uni().await?;
            trace!("Validating incoming packet from {} id {}", remote_id, i);
            let id = recv.read_u32().await?;
            trace!("Received incoming packet from {} with packet id {}", remote_id, id);
            // if id >= self.next_receive_id {
            //     return Err(Box::new(ConnectionError::new(format!("Received unexpected packet ID {} from server", id))));
            // }

            recv_streams.insert(i, recv);
        }

        Ok((send_streams, recv_streams))
    }

    fn spawn_receive_thread(
        remote_id: u32,
        recv_streams: HashMap<u32, RecvStream>,
        runtime: &Option<Runtime>,
    ) -> Result<HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>, Box<dyn Error>> {
        let mut rxs = HashMap::new();
        trace!(
            "Spawning receive thread for remote_id={} for {} ids",
            remote_id,
            recv_streams.len()
        );
        for (id, mut recv_stream) in recv_streams.into_iter() {
            let (tx, rx) = mpsc::channel(100);

            let task = async move {
                let mut partial_chunk: Option<Bytes> = None;
                loop {
                    // TODO: relay error message
                    // TODO: configurable size limit
                    let chunk = recv_stream.read_chunk(usize::MAX, true).await;
                    if let Err(e) = &chunk {
                        match e {
                            ReadError::Reset(_) => {}
                            ReadError::ConnectionLost(_) => {
                                warn!("Receive stream for remote_id={}, packet id={} errored.  This may mean the connection was closed prematurely: {:?}", remote_id, id, e);
                                break;
                            }
                            ReadError::UnknownStream => {}
                            ReadError::IllegalOrderedRead => {}
                            ReadError::ZeroRttRejected => {}
                        }
                    }
                    match chunk.unwrap() {
                        None => {
                            // TODO: Error
                            debug!(
                                "Receive stream for remote_id={}, packet id={} is finished, got None when reading chunks",
                                remote_id, id
                            );
                            break;
                        }
                        Some(chunk) => {
                            trace!(
                                "Received chunked packets for id={}, length={}",
                                id,
                                chunk.bytes.len()
                            );
                            let bytes;
                            match partial_chunk.take() {
                                None => {
                                    bytes = chunk.bytes;
                                }
                                Some(part) => {
                                    bytes = Bytes::from([part, chunk.bytes].concat());
                                    trace!(
                                        "Concatenated saved part and chunked packet: {:?}",
                                        bytes
                                    );
                                }
                            }

                            // TODO: Make trace log
                            trace!("Received bytes: {:?}", bytes);
                            let boundaries: Vec<usize> = bytes
                                .windows(FRAME_BOUNDARY.len())
                                .enumerate()
                                .filter(|(_, w)| matches!(*w, FRAME_BOUNDARY))
                                .map(|(i, _)| i)
                                .collect();
                            let mut offset = 0;
                            for i in boundaries.iter() {
                                // Reached end of bytes
                                if offset >= bytes.len() {
                                    break;
                                }
                                let frame = bytes.slice(offset..*i);
                                match partial_chunk.take() {
                                    None => {
                                        if matches!(frame.as_ref(), FRAME_BOUNDARY) {
                                            error!("Found a dangling FRAME_BOUNDARY in packet frame.  This most likely is a bug in durian")
                                        } else {
                                            trace!(
                                                "Transmitting received bytes of length {}",
                                                frame.len()
                                            );
                                            // Should never have FRAME_BOUNDARY at this point
                                            tx.send(frame).await.unwrap();
                                        }
                                    }
                                    Some(part) => {
                                        let reconstructed_frame =
                                            Bytes::from([part, frame].concat());
                                        if matches!(reconstructed_frame.as_ref(), FRAME_BOUNDARY) {
                                            error!("Found a dangling FRAME_BOUNDARY in packet frame.  This most likely is a bug in durian")
                                        } else {
                                            trace!(
                                                "Transmitting reconstructed received bytes of length {}",
                                                reconstructed_frame.len()
                                            );
                                            // Remove boundary if at beginning of reconstructed frame
                                            if reconstructed_frame.starts_with(FRAME_BOUNDARY) {
                                                tx.send(reconstructed_frame.slice(
                                                    FRAME_BOUNDARY.len() - 1
                                                        ..reconstructed_frame.len(),
                                                ))
                                                .await
                                                .unwrap();
                                            } else {
                                                tx.send(reconstructed_frame).await.unwrap();
                                            }
                                        }
                                    }
                                }
                                offset = i + FRAME_BOUNDARY.len();
                            }

                            // We got a partial chunk if there were no boundaries found, so the chunk couldn't be
                            // split to be sent to tx, or there is a leftover slice at the end that doesn't have the
                            // ending frame signaling end of a send()
                            if boundaries.is_empty()
                                || (offset + FRAME_BOUNDARY.len() != bytes.len() - 1
                                    || !bytes.ends_with(FRAME_BOUNDARY))
                            {
                                let prefix_part = bytes.slice(offset..bytes.len());
                                match partial_chunk.take() {
                                    None => {
                                        partial_chunk = Some(prefix_part);
                                    }
                                    Some(part) => {
                                        partial_chunk =
                                            Some(Bytes::from([part, prefix_part].concat()))
                                    }
                                }
                            }
                        }
                    }
                }
            };

            let receive_thread: JoinHandle<()> = match runtime.as_ref() {
                None => tokio::spawn(task),
                Some(runtime) => runtime.spawn(task),
            };

            rxs.insert(id, (RwLock::new(rx), receive_thread));
        }

        Ok(rxs)
    }

    /// Returns the source address and Endpoint of this PacketManager as a Tuple (String, Option<Endpoint>)
    ///
    /// # Returns
    /// Source address is the server address if this PacketManager is for a server, else the client address.
    /// Endpoint is listening Endpoint at the source Socket Address
    pub fn get_source(&self) -> &(String, Option<Arc<Endpoint>>) {
        &self.source
    }

    /// Register a [`Packet`] on a `receive` stream/channel, and its associated [`PacketBuilder`]
    ///
    /// # Returns
    /// A [`Result`] containing `()` for success, [`ReceiveError`] if registration failed.
    pub fn register_receive_packet<T: Packet + 'static>(
        &mut self,
        packet_builder: impl PacketBuilder<T> + 'static + Sync + Send,
    ) -> Result<(), ReceiveError> {
        if self.receive_packets.contains_right(&TypeId::of::<T>()) {
            return Err(ReceiveError::new(format!(
                "Type '{}' was already registered as a Receive packet",
                type_name::<T>()
            )));
        }

        let packet_type_id = TypeId::of::<T>();
        self.receive_packets.insert(self.next_receive_id, packet_type_id);
        self.recv_packet_builders.insert(packet_type_id, Box::new(packet_builder));
        debug!(
            "Registered Receive packet with id={}, type={}",
            self.next_receive_id,
            type_name::<T>()
        );
        self.next_receive_id += 1;
        Ok(())
    }

    /// Register a [`Packet`] on a `send` stream/channel
    ///
    /// # Returns
    /// A [`Result`] containing `()` for success, [`SendError`] if registration failed.
    pub fn register_send_packet<T: Packet + 'static>(&mut self) -> Result<(), SendError> {
        if self.send_packets.contains_right(&TypeId::of::<T>()) {
            return Err(SendError::new(format!(
                "Type '{}' was already registered as a Send packet",
                type_name::<T>()
            )));
        }

        self.send_packets.insert(self.next_send_id, TypeId::of::<T>());
        debug!("Registered Send packet with id={}, type={}", self.next_send_id, type_name::<T>());
        self.next_send_id += 1;
        Ok(())
    }

    /// Fetches all received packets from all destination addresses
    ///
    /// This reads from all `receive` channels and deserializes the [`Packets`](`Packet`) requested.  Any channel that
    /// is found disconnected will be removed from the stream queue and a warning will be logged.  If a reading from a
    /// receive channel ran into an unexpected error, this function will stop reading from other channels and return
    /// the error.
    ///
    /// # Type Arguments
    /// * `T: Packet + 'static` - The [`Packet`] type to request
    /// * `U: PacketBuilder<T> + 'static` - The [`PacketBuilder`] for this packet, used to deserialize bytes into
    ///     the [`Packet`] type requested
    ///
    /// # Arguments
    /// * `blocking` - `true` to make this a blocking call, waiting for __ALL__ destination addresses to send at least
    ///     one Packet of type `T`.  `false` will make this non-blocking, and any destination addresses that did not
    ///     send the Packet will return [`None`] paired with it.  __Warning: be careful about making this a blocking
    ///     call, as if any of the Packets don't come from any destination address, it could hang your application__
    ///
    /// # Returns
    /// A [`Result`] containing a [`Vec`] of pair tuples with the first element being the destination socket address,
    /// and the second element will have a `None` if `blocking` was set to `false` and the associated destination address
    /// did not send the Packet type when this call queried for it, or [`Some`] containing a [`Vec`] of the Packets
    /// type `T` that was requested from the associated destination address.  
    ///
    /// [`ReceiveError`] if there was an error fetching received packets.  If a channel was found disconnected, no
    /// Error will be returned with it, but instead it will be removed from the stream queue and output.
    ///
    /// # Panics
    /// If the `PacketManager` was created via [`new_for_async()`](`PacketManager::new_for_async()`)
    pub fn received_all<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        &mut self,
        blocking: bool,
    ) -> Result<Vec<(u32, Option<Vec<T>>)>, ReceiveError> {
        self.validate_for_received::<T>(true)?;
        self.update_new_receivers();
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_received()?");
            }
            Some(runtime) => {
                let res = runtime.block_on(async {
                    let mut res = vec![];
                    let mut err: Option<ReceiveError> = None;
                    // If we find any streams are closed, save them to cleanup and close the connections after iterating
                    let mut remote_id_to_close = Vec::new();
                    for (remote_id, rxs) in self.rx.iter() {
                        let received = PacketManager::async_received_helper::<T, U>(
                            blocking,
                            *remote_id,
                            &self.receive_packets,
                            &self.recv_packet_builders,
                            rxs,
                        )
                        .await;

                        match received {
                            Ok(received) => {
                                res.push((*remote_id, received));
                            }
                            Err(e) => match e.error_type {
                                ErrorType::Unexpected => {
                                    err = Some(e);
                                    break;
                                }
                                ErrorType::Disconnected => {
                                    remote_id_to_close.push(*remote_id);
                                }
                            },
                        }
                    }

                    if let Some(e) = err {
                        return (remote_id_to_close, Err(e));
                    }
                    (remote_id_to_close, Ok(res))
                });

                for remote_id in res.0.iter() {
                    warn!("Receive stream for remote_id={} disconnected.  Removing it from the receive queue and continuing as normal.", remote_id);
                    self.close_connection(*remote_id).unwrap_or_else(|_| {
                        panic!("Could not close connection for remote_id={}", remote_id)
                    });
                }

                res.1
            }
        }
    }

    /// Fetches all received packets from all destination addresses
    ///
    /// Same as [`received_all()`](`PacketManager::received_all()`), except it returns a `Future` and can be called
    /// from an async context.
    ///
    /// # Panics
    /// If the `PacketManager` was created via [`new()`](`PacketManager::new()`)
    pub async fn async_received_all<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        &mut self,
        blocking: bool,
    ) -> Result<Vec<(u32, Option<Vec<T>>)>, ReceiveError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_received(), make sure you create the PacketManager using new_for_async(), not new()");
        }
        self.async_validate_for_received::<T>(true).await?;
        self.async_update_new_receivers().await;
        let mut res = vec![];
        let mut err: Option<ReceiveError> = None;
        // If we find any streams are closed, save them to cleanup and close the connections after iterating
        let mut remote_id_to_close = Vec::new();
        for (remote_id, rxs) in self.rx.iter() {
            let received = PacketManager::async_received_helper::<T, U>(
                blocking,
                *remote_id,
                &self.receive_packets,
                &self.recv_packet_builders,
                rxs,
            )
            .await;

            match received {
                Ok(received) => {
                    res.push((*remote_id, received));
                }
                Err(e) => match e.error_type {
                    ErrorType::Unexpected => {
                        err = Some(e);
                        break;
                    }
                    ErrorType::Disconnected => {
                        remote_id_to_close.push(*remote_id);
                    }
                },
            }
        }

        for remote_id in remote_id_to_close.iter() {
            warn!("Receive stream for remote_id={} disconnected.  Removing it from the receive queue and continuing as normal.", remote_id);
            self.async_close_connection(*remote_id).await.unwrap_or_else(|_| {
                panic!("Could not close connection for remote_id={}", remote_id)
            });
        }

        if let Some(e) = err {
            return Err(e);
        }
        Ok(res)
    }

    /// Fetches all received packets from a single destination address.  This should only be called if there is __only__
    /// one destination address, particularly convenient if this is for a client which connects to a single server.
    ///
    /// This reads from the single `receive` channel and deserializes the [`Packets`](`Packet`) requested.
    ///
    /// # Type Arguments
    /// * `T: Packet + 'static` - The [`Packet`] type to request
    /// * `U: PacketBuilder<T> + 'static` - The [`PacketBuilder`] for this packet, used to deserialize bytes into
    ///     the [`Packet`] type requested
    ///
    /// # Arguments
    /// * `blocking` - `true` to make this a blocking call, waiting for the destination address to send at least
    ///     one Packet of type `T`.  `false` will make this non-blocking, and if destination address did not
    ///     send the Packet, it will return [`None`].  __Warning: be careful about making this a blocking
    ///     call, as if Packets don't arrive exactly as you expect, it could hang your application__
    ///
    /// # Returns
    /// A [`Result`] containing [`None`] if the destination address did not send any Packets of this type, else [`Some`]
    /// containing a [`Vec`] of those received packets.
    ///
    /// [`ReceiveError`] if error occurred fetching received packets.
    ///
    /// # Panics
    /// If the [`PacketManager`] was created via [`new_for_async()`](`PacketManager::new_for_async()`)
    pub fn received<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        &mut self,
        blocking: bool,
    ) -> Result<Option<Vec<T>>, ReceiveError> {
        self.validate_for_received::<T>(false)?;
        self.update_new_receivers();
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_received()?");
            }
            Some(runtime) => runtime.block_on({
                let rxs = self.rx.first().unwrap();
                PacketManager::async_received_helper::<T, U>(
                    blocking,
                    *rxs.0,
                    &self.receive_packets,
                    &self.recv_packet_builders,
                    rxs.1,
                )
            }),
        }
    }

    /// Fetches all received packets from a single destination address.  This should only be called if there is __only__
    /// one destination address, particularly convenient if this is for a client which connects to a single server.
    ///
    /// Same as [`received()`](`PacketManager::received()`), except it returns a `Future` and can be called
    /// from an async context.
    ///
    /// # Panics
    /// If the `PacketManager` was created via [`new()`](`PacketManager::new()`)
    pub async fn async_received<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        &mut self,
        blocking: bool,
    ) -> Result<Option<Vec<T>>, ReceiveError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_received(), make sure you create the PacketManager using new_for_async(), not new()");
        }
        self.async_validate_for_received::<T>(false).await?;
        self.async_update_new_receivers().await;
        let rxs = self.rx.first().unwrap();
        PacketManager::async_received_helper::<T, U>(
            blocking,
            *rxs.0,
            &self.receive_packets,
            &self.recv_packet_builders,
            rxs.1,
        )
        .await
    }

    // Assumes does not have more than one client to send to, should be checked by callers
    // TODO: Handle connections dropped, if joinhandle failed, close the connection and return error, etc.
    async fn async_received_helper<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        blocking: bool,
        remote_id: u32,
        receive_packets: &BiMap<u32, TypeId>,
        recv_packet_builders: &HashMap<TypeId, Box<dyn Any + Send + Sync>>,
        rxs: &HashMap<u32, (RwLock<Receiver<Bytes>>, JoinHandle<()>)>,
    ) -> Result<Option<Vec<T>>, ReceiveError> {
        let packet_type_id = TypeId::of::<T>();
        let id = receive_packets.get_by_right(&packet_type_id).unwrap();
        let (rx_lock, _receive_thread) = rxs.get(id).unwrap();
        let mut rx = rx_lock.write().await;
        let mut res: Vec<T> = Vec::new();
        let packet_builder: &U =
            recv_packet_builders.get(&TypeId::of::<T>()).unwrap().downcast_ref::<U>().unwrap();

        // If blocking, wait for the first packet
        if blocking {
            match rx.recv().await {
                None => {
                    return Err(ReceiveError::new_with_type(
                        format!(
                            "Receiver channel for packet type {} was disconnected",
                            type_name::<T>()
                        ),
                        ErrorType::Disconnected,
                    ));
                }
                Some(bytes) => {
                    PacketManager::receive_bytes::<T, U>(bytes, packet_builder, &mut res)?;
                }
            }
        }

        // Loop for any subsequent packets
        loop {
            match rx.try_recv() {
                Ok(bytes) => {
                    PacketManager::receive_bytes::<T, U>(bytes, packet_builder, &mut res)?;
                }
                Err(e) => match e {
                    TryRecvError::Empty => {
                        break;
                    }
                    TryRecvError::Disconnected => {
                        return Err(ReceiveError::new_with_type(
                            format!(
                                "Receiver channel for packet type {} was disconnected",
                                type_name::<T>()
                            ),
                            ErrorType::Disconnected,
                        ));
                    }
                },
            }
        }

        if res.is_empty() {
            return Ok(None);
        }
        debug!(
            "Fetched {} received packets of type={}, id={}, from remote_id={}",
            res.len(),
            type_name::<T>(),
            id,
            remote_id
        );
        Ok(Some(res))
    }

    fn validate_for_received<T: Packet + 'static>(
        &self,
        for_all: bool,
    ) -> Result<Option<Vec<T>>, ReceiveError> {
        if !for_all && self.has_more_than_one_remote() {
            return Err(ReceiveError::new(format!("async_received()/received() was called for packet {}, but there is more than one client.  Did you mean to call async_received_all()/received_all()?", type_name::<T>())));
        }

        if !self.receive_packets.contains_right(&TypeId::of::<T>()) {
            return Err(ReceiveError::new(format!(
                "Type '{}' was never registered!  Did you forget to call register_receive_packet()?",
                type_name::<T>()
            )));
        }
        Ok(None)
    }

    async fn async_validate_for_received<T: Packet + 'static>(
        &self,
        for_all: bool,
    ) -> Result<Option<Vec<T>>, ReceiveError> {
        if !for_all && self.async_has_more_than_one_remote().await {
            return Err(ReceiveError::new(format!("async_received()/received() was called for packet {}, but there is more than one client.  Did you mean to call async_received_all()/received_all()?", type_name::<T>())));
        }

        if !self.receive_packets.contains_right(&TypeId::of::<T>()) {
            return Err(ReceiveError::new(format!(
                "Type '{}' was never registered!  Did you forget to call register_receive_packet()?",
                type_name::<T>()
            )));
        }
        Ok(None)
    }

    fn update_new_receivers(&mut self) {
        let mut new_rx_lock = self.new_rxs.blocking_write();
        if !new_rx_lock.is_empty() {
            let new_rx_vec = std::mem::take(&mut *new_rx_lock);
            for (remote_id, val) in new_rx_vec.into_iter() {
                if self.rx.contains_key(&remote_id) {
                    panic!("Receive stream for remote_id={} already existed!  There cannot be multiple connections between the same Socket addresses for the same protocol.", remote_id);
                }
                self.rx.insert(remote_id, val);
            }
        }
    }

    async fn async_update_new_receivers(&mut self) {
        let mut new_rx_lock = self.new_rxs.write().await;
        if !new_rx_lock.is_empty() {
            let new_rx_vec = std::mem::take(&mut *new_rx_lock);
            for (remote_id, val) in new_rx_vec.into_iter() {
                if self.rx.contains_key(&remote_id) {
                    panic!("Receive stream for remote_id={} already existed!  There cannot be multiple connections between the same Socket addresses for the same protocol.", remote_id);
                }
                self.rx.insert(remote_id, val);
            }
        }
    }

    #[inline]
    fn receive_bytes<T: Packet + 'static, U: PacketBuilder<T> + 'static>(
        bytes: Bytes,
        packet_builder: &U,
        res: &mut Vec<T>,
    ) -> Result<(), ReceiveError> {
        if bytes.is_empty() {
            return Err(ReceiveError::new(format!(
                "Received empty bytes for packet type={}!",
                type_name::<T>()
            )));
        }
        debug!("Received packet with id={} for type={}", res.len(), type_name::<T>());
        let packet = match packet_builder.read(bytes) {
            Ok(p) => p,
            Err(e) => {
                let err_msg = format!(
                    "Could not build packet of type={} from bytes: {:?}",
                    type_name::<T>(),
                    e
                );
                error!("{}", err_msg);
                return Err(ReceiveError::new_with_type(err_msg, ErrorType::Unexpected));
            }
        };
        res.push(packet);
        Ok(())
    }

    /// Broadcast a Packet to all destination addresses
    ///
    /// If any `send` channels are disconnected, they will be removed from the send stream queue and a warning will be
    /// logged, and no error will be indicated from this function.  If there was an unexpected error, no further packets
    /// will be sent, and the function will stop to return an error.
    ///
    /// # Arguments
    /// * `packet` - The [`Packet`] to broadcast
    ///
    /// # Returns
    /// A [`Result`] containing `()` if Packet was sent, else [`SendError`].  If a channel was disconnected, it will be
    /// removed from the send stream queue and __no__ error will be returned.
    ///
    /// # Panics
    /// If the [`PacketManager`] was created via [`new_for_async()`](`PacketManager::new_for_async()`)
    pub fn broadcast<T: Packet + 'static>(&mut self, packet: T) -> Result<(), SendError> {
        self.validate_for_send::<T>(true)?;
        self.update_new_senders();
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_broadcast()?");
            }
            Some(runtime) => {
                let res = runtime.block_on(async {
                    let mut err: Option<SendError> = None;
                    // If we find any streams are closed, save them to cleanup and close the connections after iterating
                    let mut remote_id_to_close = Vec::new();
                    for (remote_id, send_streams) in self.send_streams.iter() {
                        let sent = PacketManager::async_send_helper::<T>(
                            &packet,
                            *remote_id,
                            &self.send_packets,
                            send_streams,
                        )
                        .await;

                        if let Err(e) = sent {
                            warn!("Ran into error during broadcast(): {}", e);
                            match e.error_type {
                                ErrorType::Unexpected => {
                                    err = Some(e);
                                    break;
                                }
                                ErrorType::Disconnected => {
                                    remote_id_to_close.push(*remote_id);
                                }
                            }
                        }
                    }

                    if let Some(e) = err {
                        return (remote_id_to_close, Err(e));
                    }

                    (remote_id_to_close, Ok(()))
                });

                for remote_id in res.0.iter() {
                    warn!("Send stream for remote_id={} disconnected.  Removing it from the send queue and continuing as normal.", remote_id);
                    self.close_connection(*remote_id).unwrap_or_else(|_| {
                        panic!("Could not close connection for remote_id={}", remote_id)
                    });
                }

                res.1
            }
        }
    }

    /// Broadcast a Packet to all destination addresses
    ///
    /// Same as [`broadcast()`](`PacketManager::broadcast()`), except it returns a `Future` and can be called from
    /// an async context.
    ///
    /// # Panics
    /// If the `PacketManager` was created via [`new()`](`PacketManager::new()`)
    pub async fn async_broadcast<T: Packet + 'static>(
        &mut self,
        packet: T,
    ) -> Result<(), SendError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_send(), make sure you create the PacketManager using new_async(), not new()");
        }
        self.async_validate_for_send::<T>(true).await?;
        self.async_update_new_senders().await;
        let mut err: Option<SendError> = None;
        // If we find any streams are closed, save them to cleanup and close the connections after iterating
        let mut remote_id_to_close = Vec::new();
        for (remote_id, send_streams) in self.send_streams.iter() {
            let sent = PacketManager::async_send_helper::<T>(
                &packet,
                *remote_id,
                &self.send_packets,
                send_streams,
            )
            .await;

            if let Err(e) = sent {
                warn!("Ran into error during async_broadcast(): {}", e);
                match e.error_type {
                    ErrorType::Unexpected => {
                        err = Some(e);
                        break;
                    }
                    ErrorType::Disconnected => {
                        remote_id_to_close.push(*remote_id);
                    }
                }
            }
        }

        for remote_id in remote_id_to_close.iter() {
            warn!(
                "Send stream for remote_id={} disconnected.  Removing it from the send queue and continuing as normal.",
                remote_id
            );
            self.async_close_connection(*remote_id).await.unwrap_or_else(|_| {
                panic!("Could not close connection for remote_id={}", remote_id)
            });
        }

        if let Some(e) = err {
            return Err(e);
        }

        Ok(())
    }

    /// Send a Packet to the single destination address.  This should __only__ be used if there is __only__ one
    /// destination address, particularly convenient if this is for a client which connects to a single server.
    ///
    /// # Arguments
    /// * `packet` - The [`Packet`] to broadcast
    ///
    /// # Returns
    /// A [`Result`] containing `()` if Packet was sent, else [`SendError`]
    ///
    /// # Panics
    /// If the [`PacketManager`] was created via [`new_for_async()`](`PacketManager::new_for_async()`)
    pub fn send<T: Packet + 'static>(&mut self, packet: T) -> Result<(), SendError> {
        self.validate_for_send::<T>(false)?;
        self.update_new_senders();
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_send()?");
            }
            Some(runtime) => {
                let res = runtime.block_on({
                    let first = self.send_streams.first().unwrap();
                    PacketManager::async_send_helper::<T>(
                        &packet,
                        *first.0,
                        &self.send_packets,
                        first.1,
                    )
                });
                if let Err(e) = &res {
                    warn!("Ran into error during async_send(): {}", e);
                }
                res
            }
        }
    }

    /// Send a Packet to the single destination address.  This should __only__ be used if there is __only__ one
    /// destination address, particularly convenient if this is for a client which connects to a single server.
    ///
    /// Same as [`send()`](`PacketManager::send()`)
    ///
    /// # Panics
    /// If the `PacketManager` was created via [`new()`](`PacketManager::new()`)
    pub async fn async_send<T: Packet + 'static>(&mut self, packet: T) -> Result<(), SendError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_send(), make sure you create the PacketManager using new_for_async(), not new()");
        }
        self.async_validate_for_send::<T>(false).await?;
        self.async_update_new_senders().await;
        let first = self.send_streams.first().unwrap();
        let res =
            PacketManager::async_send_helper::<T>(&packet, *first.0, &self.send_packets, first.1)
                .await;
        if let Err(e) = &res {
            warn!("Ran into error during async_send(): {}", e);
        }
        res
    }

    /// Send a Packet to a specified destination address.
    ///
    /// # Arguments
    /// * `remote_id` - The destination Remote Id to send the Packet to
    /// * `packet` - The [`Packet`] to broadcast
    ///
    /// # Returns
    /// A [`Result`] containing `()` if Packet was sent, else [`SendError`].  In contrast to [`broadcast()`](`PacketManager::broadcast()`),
    /// if the `send` channel is disconnected, this will return a [`SendError`] with `error_type == [`ErrorType::Disconnect`]`
    ///
    /// # Panics
    /// If the [`PacketManager`] was created via [`new_for_async()`](`PacketManager::new_for_async()`)
    pub fn send_to<T: Packet + 'static>(
        &mut self,
        remote_id: u32,
        packet: T,
    ) -> Result<(), SendError> {
        self.validate_for_send::<T>(true)?;
        self.update_new_senders();
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_send_to()?");
            }
            Some(runtime) => {
                let res = match self.send_streams.get(&remote_id) {
                    None => Err(SendError::new(format!(
                        "Could not find Send stream for remote_id={}",
                        remote_id
                    ))),
                    Some(send_streams) => runtime.block_on(PacketManager::async_send_helper::<T>(
                        &packet,
                        remote_id,
                        &self.send_packets,
                        send_streams,
                    )),
                };

                match res {
                    Ok(_) => Ok(()),
                    Err(e) => {
                        warn!("Ran into error during send_to(): {}", e);
                        match e.error_type {
                            ErrorType::Unexpected => Err(e),
                            ErrorType::Disconnected => {
                                warn!("Send stream for remote_id={} disconnected.  Removing it from the send queue and returning error.", remote_id);
                                self.close_connection(remote_id).unwrap_or_else(|_| {
                                    panic!("Could not close connection for remote_id={}", remote_id)
                                });
                                Err(e)
                            }
                        }
                    }
                }
            }
        }
    }

    /// Send a Packet to a specified destination address.
    ///
    /// Same as [`send_to()`](`PacketManager::send_to()`)
    ///
    /// # Panics
    /// - If the `PacketManager` was created via [`new()`](`PacketManager::new()`)
    pub async fn async_send_to<T: Packet + 'static>(
        &mut self,
        remote_id: u32,
        packet: T,
    ) -> Result<(), SendError> {
        if self.runtime.is_some() {
            panic!("PacketManager has a runtime instance associated with it.  If you are using async_send(), make sure you create the PacketManager using new_async(), not new()");
        }
        self.async_validate_for_send::<T>(true).await?;
        self.async_update_new_senders().await;
        let res = match self.send_streams.get(&remote_id) {
            None => Err(SendError::new(format!(
                "Could not find Send stream for remote_id={}",
                remote_id
            ))),
            Some(send_streams) => {
                PacketManager::async_send_helper::<T>(
                    &packet,
                    remote_id,
                    &self.send_packets,
                    send_streams,
                )
                .await
            }
        };

        match res {
            Ok(_) => Ok(()),
            Err(e) => {
                warn!("Ran into error during async_send_to(): {}", e);
                match e.error_type {
                    ErrorType::Unexpected => Err(e),
                    ErrorType::Disconnected => {
                        warn!("Send stream for remote_id={} disconnected.  Removing it from the send queue and returning error.", remote_id);
                        self.async_close_connection(remote_id).await.unwrap_or_else(|_| {
                            panic!("Could not close connection for remote_id={}", remote_id)
                        });
                        Err(e)
                    }
                }
            }
        }
    }

    fn update_new_senders(&mut self) {
        let mut new_send_stream_lock = self.new_send_streams.blocking_write();
        if !new_send_stream_lock.is_empty() {
            let new_send_streams_vec = std::mem::take(&mut *new_send_stream_lock);
            for (remote_id, val) in new_send_streams_vec.into_iter() {
                if self.send_streams.contains_key(&remote_id) {
                    panic!("Send stream for {} already existed!  There cannot be multiple connections between the same Socket addresses for the same protocol.", remote_id);
                }
                self.send_streams.insert(remote_id, val);
            }
        }
    }

    async fn async_update_new_senders(&mut self) {
        let mut new_send_stream_lock = self.new_send_streams.write().await;
        if !new_send_stream_lock.is_empty() {
            let new_send_streams_vec = std::mem::take(&mut *new_send_stream_lock);
            for (remote_id, val) in new_send_streams_vec.into_iter() {
                if self.send_streams.contains_key(&remote_id) {
                    panic!("Send stream for {} already existed!  There cannot be multiple connections between the same Socket addresses for the same protocol.", remote_id);
                }
                self.send_streams.insert(remote_id, val);
            }
        }
    }

    // TODO: handle connection dropped
    async fn async_send_helper<T: Packet + 'static>(
        packet: &T,
        remote_id: u32,
        send_packets: &BiMap<u32, TypeId>,
        send_streams: &HashMap<u32, RwLock<SendStream>>,
    ) -> Result<(), SendError> {
        let bytes = packet.as_bytes();
        let packet_type_id = TypeId::of::<T>();
        let id = send_packets.get_by_right(&packet_type_id).unwrap();
        let mut send_stream = send_streams.get(id).unwrap().write().await;
        debug!("Sending bytes to remote_id={} with len {}", remote_id, bytes.len());
        trace!("Sending bytes {:?}", bytes);
        if let Err(e) = send_stream.write_chunk(bytes).await {
            return match e {
                WriteError::ConnectionLost(e) => Err(SendError::new_with_type(
                    format!(
                        "Send stream for remote_id={}, packet id={} is disconnected: {:?}",
                        remote_id, id, e
                    ),
                    ErrorType::Disconnected,
                )),
                _ => Err(SendError::new_with_type(
                    format!(
                        "Send stream for remote_id={}, packet id={} ran into unexpected error: {:?}",
                        remote_id, id, e
                    ),
                    ErrorType::Unexpected,
                )),
            };
        }
        trace!("Sending FRAME_BOUNDARY");
        if let Err(e) = send_stream.write_all(FRAME_BOUNDARY).await {
            return match e {
                WriteError::ConnectionLost(e) => {
                    Err(SendError::new_with_type(
                        format!("Send stream for remote_id={}, packet id={} is disconnected: {:?}", remote_id, id, e),
                        ErrorType::Disconnected,
                    ))
                },
                _ => { Err(SendError::new_with_type(format!("Send stream for remote_id={}, packet id={} ran into unexpected error when writing frame boundary: {:?}", remote_id, id, e), ErrorType::Unexpected)) }
            };
        }
        debug!("Sent packet to remote_id={} with id={}, type={}", remote_id, id, type_name::<T>());
        Ok(())
    }

    /// Returns the number of clients attached to this server.  This will always return `0` if on a client side.
    pub fn get_num_clients(&self) -> u32 {
        self.remote_connections.blocking_read().len() as u32
    }

    /// Returns the number of clients attached to this server.  This will always return `0` if on a client side.
    pub async fn async_get_num_clients(&self) -> u32 {
        self.remote_connections.read().await.len() as u32
    }

    /// Returns the client connections in tuples of (remote Id, Socket Address)
    pub fn get_remote_connections(&self) -> Vec<(u32, String)> {
        self.remote_connections
            .blocking_read()
            .iter()
            .map(|(remote_id, con)| (*remote_id, con.0.clone()))
            .collect()
    }

    /// Returns the client connections in tuples of (remote Id, Socket Address)
    pub async fn async_get_remote_connections(&self) -> Vec<(u32, String)> {
        self.remote_connections
            .read()
            .await
            .iter()
            .map(|(remote_id, con)| (*remote_id, con.0.clone()))
            .collect()
    }

    /// Returns the Socket IP Address for a remote Id.  Remote Ids start from 0 and are incremented in the order each
    /// remote client is connected to the current connection.  This can be helpful to associated some sort of numerical ID with clients.
    ///
    /// # Returns
    /// [`None`] if the remote Id was not a valid Id in remote connections.  [`Some`] containing the Remote address
    /// for the requested remote Id.
    pub fn get_remote_address(&self, remote_id: u32) -> Option<String> {
        let remote_connections = self.remote_connections.blocking_read();
        if !remote_connections.contains_key(&remote_id) {
            return None;
        }
        Some(remote_connections.get(&remote_id).unwrap().0.clone())
    }

    /// Returns the Socket IP Address for a remote Id.  Remote Ids start from 0 and are incremented in the order each
    /// remote client is connected to the current connection.  This can be helpful to associated some sort of numerical ID with clients.
    ///
    /// Same as [`get_remote_id`](`PacketManager::get_remote_id()`), except returns a `Future` and can be called from
    /// an async context.
    pub async fn async_get_remote_address(&self, remote_id: u32) -> Option<String> {
        let client_connections = self.remote_connections.read().await;
        if !client_connections.contains_key(&remote_id) {
            return None;
        }
        Some(client_connections.get(&remote_id).unwrap().0.clone())
    }

    /// Close the connection with a destination remote Id
    ///
    /// __Warning: this will forcefully close the connection, causing any Packets in stream queues that haven't already
    /// sent over the wire to be dropped.__
    ///
    /// Cleans up resources associated with the connection internal to [`PacketManager`].  A Packet will be sent to the
    /// destination address detailing the reason of connection closure.
    ///
    /// # Returns
    /// A [`Result`] containing `()` on success, [`CloseError`] if error occurred while closing the connection.  Note:
    /// connection resources may be partially closed.
    pub fn close_connection(&mut self, remote_id: u32) -> Result<(), CloseError> {
        // Update senders and receivers before checking what to remove
        self.update_new_senders();
        self.update_new_receivers();
        let mut client_connections = self.remote_connections.blocking_write();
        if let Some((addr, conn)) = client_connections.get(&remote_id) {
            debug!(
                "Forcefully closing connection for remote addr={}, remote_id={}",
                addr, remote_id
            );
            conn.close(
                VarInt::from(1_u8),
                "PacketManager::close_connection() called for this connection".as_bytes(),
            );
        }
        client_connections.remove(&remote_id);
        // Note: We don't gracefully shut down the streams individually, as they should shut down on their own eventually
        self.send_streams.remove(&remote_id);
        self.rx.remove(&remote_id);
        Ok(())
    }

    /// Close the connection with a destination Remote Id
    ///
    /// __Warning: this will forcefully close the connection, causing any Packets in stream queues that haven't already
    /// sent over the wire to be dropped.__
    ///
    /// Same as [`close_connection()`](`PacketManager::close_connection()`), except returns a Future and can be called
    /// from an async context.
    pub async fn async_close_connection(&mut self, remote_id: u32) -> Result<(), CloseError> {
        // Update senders and receivers before checking what to remove
        self.async_update_new_senders().await;
        self.async_update_new_receivers().await;
        let mut client_connections = self.remote_connections.write().await;
        if let Some((addr, conn)) = client_connections.get(&remote_id) {
            debug!(
                "Forcefully closing connection for remote addr={}, remote_id={}",
                addr, remote_id
            );
            conn.close(
                VarInt::from(1_u8),
                "PacketManager::close_connection() called for this connection".as_bytes(),
            );
        }
        client_connections.remove(&remote_id);
        // Note: We don't gracefully shut down the streams individually, as they should shut down on their own eventually
        self.send_streams.remove(&remote_id);
        self.rx.remove(&remote_id);
        Ok(())
    }

    /// Closes the connection with a destination Remote Id after flushing all send streams gracefully.
    ///
    /// This differs from [`close_connection()`](`PacketManager::close_connection()`) in that it gracefully flushes all
    /// send streams, and waits for acks on each before closing the connection.
    ///
    /// Cleans up resources associated with the connection internal to [`PacketManager`].  A Packet will be sent to the
    /// destination address detailing the reason of connection closure.
    ///
    /// # Returns
    /// A [`Result`] containing `()` on success, [`CloseError`] if error occurred while closing the connection.  Note:
    /// connection resources may be partially closed.
    pub fn finish_connection(&mut self, remote_id: u32) -> Result<(), CloseError> {
        // Update senders and receivers before checking what to remove
        self.update_new_senders();
        self.update_new_receivers();
        // Finish all send streams
        match self.runtime.as_ref() {
            None => {
                panic!("PacketManager does not have a runtime instance associated with it.  Did you mean to call async_finish_connection()?");
            }
            Some(runtime) => runtime.block_on(async {
                if let Some(send_streams) = self.send_streams.get(&remote_id) {
                    for send_stream in send_streams.values() {
                        if let Err(e) = send_stream.write().await.finish().await {
                            debug!(
                                "Could not finish send stream for remote_id={}.  Continuing to close connection: {:?}",
                                remote_id, e
                            );
                        }
                    }
                }
            }),
        };
        self.send_streams.remove(&remote_id);
        let mut client_connections = self.remote_connections.blocking_write();
        if let Some((addr, conn)) = client_connections.get(&remote_id) {
            debug!("Finishing connection for remote addr={}, remote_id={}", addr, remote_id);
            conn.close(
                VarInt::from(1_u8),
                "PacketManager::finish_connection() called for this connection".as_bytes(),
            );
        }
        client_connections.remove(&remote_id);
        self.rx.remove(&remote_id);
        Ok(())
    }

    /// Closes the connection with a destination Remote Id after flushing all send streams gracefully.
    ///
    /// Same as [`finish_connection()`](`PacketManager::finish_connection()`), except returns a Future and can be called
    /// from an async context.
    pub async fn async_finish_connection(&mut self, remote_id: u32) -> Result<(), CloseError> {
        // Update senders and receivers before checking what to remove
        self.async_update_new_senders().await;
        self.async_update_new_receivers().await;
        // Finish all send streams
        if let Some(send_streams) = self.send_streams.get(&remote_id) {
            for send_stream in send_streams.values() {
                if let Err(e) = send_stream.write().await.finish().await {
                    debug!("Could not finish send stream for remote_id={}.  Continuing to close connection: {:?}", remote_id, e);
                }
            }
        }
        self.send_streams.remove(&remote_id);
        let mut client_connections = self.remote_connections.write().await;
        if let Some((addr, conn)) = client_connections.get(&remote_id) {
            debug!("Finishing connection for remote addr={}, remote_id={}", addr, remote_id);
            conn.close(
                VarInt::from(1_u8),
                "PacketManager::finish_connection() called for this connection".as_bytes(),
            );
        }
        client_connections.remove(&remote_id);
        self.rx.remove(&remote_id);
        Ok(())
    }

    fn validate_for_send<T: Packet + 'static>(&self, for_all: bool) -> Result<(), SendError> {
        if !for_all && self.has_more_than_one_remote() {
            return Err(SendError::new(format!("send() was called for packet {}, but there is more than one client.  Did you mean to call broadcast()?", type_name::<T>())));
        }

        if !self.send_packets.contains_right(&TypeId::of::<T>()) {
            return Err(SendError::new(format!(
                "Type '{}' was never registered!  Did you forget to call register_send_packet()?",
                type_name::<T>()
            )));
        }

        Ok(())
    }

    async fn async_validate_for_send<T: Packet + 'static>(
        &self,
        for_all: bool,
    ) -> Result<(), SendError> {
        if !for_all && self.async_has_more_than_one_remote().await {
            return Err(SendError::new(format!("async_send() was called for packet {}, but there is more than one client.  Did you mean to call async_broadcast()?", type_name::<T>())));
        }

        if !self.send_packets.contains_right(&TypeId::of::<T>()) {
            return Err(SendError::new(format!(
                "Type '{}' was never registered!  Did you forget to call register_send_packet()?",
                type_name::<T>()
            )));
        }

        Ok(())
    }

    // Either we are a single client talking to a single server, or a server talking to potentially multiple clients
    #[inline]
    fn has_more_than_one_remote(&self) -> bool {
        self.remote_connections.blocking_read().len() > 1
    }

    #[inline]
    async fn async_has_more_than_one_remote(&self) -> bool {
        self.remote_connections.read().await.len() > 1
    }
}