@@ -8,10 +8,12 @@ use delay_map::HashMapDelay;
8
8
use futures:: StreamExt ;
9
9
use tokio:: sync:: { mpsc, oneshot, Notify } ;
10
10
11
- use crate :: cid:: { ConnectionId , ConnectionPeer } ;
11
+ use crate :: cid:: ConnectionId ;
12
12
use crate :: congestion;
13
13
use crate :: event:: { SocketEvent , StreamEvent } ;
14
14
use crate :: packet:: { Packet , PacketBuilder , PacketType , SelectiveAck } ;
15
+ use crate :: peer:: ConnectionPeer ;
16
+ use crate :: peer:: Peer ;
15
17
use crate :: recv:: ReceiveBuffer ;
16
18
use crate :: send:: SendBuffer ;
17
19
use crate :: sent:: { SentPackets , SentPacketsError } ;
@@ -167,9 +169,10 @@ impl From<ConnectionConfig> for congestion::Config {
167
169
}
168
170
}
169
171
170
- pub struct Connection < const N : usize , P > {
172
+ pub struct Connection < const N : usize , P : ConnectionPeer > {
171
173
state : State < N > ,
172
- cid : ConnectionId < P > ,
174
+ cid : ConnectionId < P :: Id > ,
175
+ peer : Peer < P > ,
173
176
config : ConnectionConfig ,
174
177
endpoint : Endpoint ,
175
178
peer_ts_diff : Duration ,
@@ -185,7 +188,8 @@ pub struct Connection<const N: usize, P> {
185
188
186
189
impl < const N : usize , P : ConnectionPeer > Connection < N , P > {
187
190
pub fn new (
188
- cid : ConnectionId < P > ,
191
+ cid : ConnectionId < P :: Id > ,
192
+ peer : Peer < P > ,
189
193
config : ConnectionConfig ,
190
194
syn : Option < Packet > ,
191
195
connected : oneshot:: Sender < io:: Result < ( ) > > ,
@@ -212,6 +216,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
212
216
Self {
213
217
state : State :: Connecting ( Some ( connected) ) ,
214
218
cid,
219
+ peer,
215
220
config,
216
221
endpoint,
217
222
peer_ts_diff,
@@ -232,15 +237,15 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
232
237
mut writes : mpsc:: UnboundedReceiver < Write > ,
233
238
mut shutdown : oneshot:: Receiver < ( ) > ,
234
239
) -> io:: Result < ( ) > {
235
- tracing:: debug!( "uTP conn starting... {:?}" , self . cid . peer) ;
240
+ tracing:: debug!( "uTP conn starting... {:?}" , self . peer) ;
236
241
237
242
// If we are the initiating endpoint, then send the SYN. If we are the accepting endpoint,
238
243
// then send the SYN-ACK.
239
244
match self . endpoint {
240
245
Endpoint :: Initiator ( ( syn_seq_num, ..) ) => {
241
246
let syn = self . syn_packet ( syn_seq_num) ;
242
247
self . socket_events
243
- . send ( SocketEvent :: Outgoing ( ( syn. clone ( ) , self . cid . peer . clone ( ) ) ) )
248
+ . send ( SocketEvent :: Outgoing ( ( syn. clone ( ) , self . peer . clone ( ) ) ) )
244
249
. unwrap ( ) ;
245
250
self . unacked
246
251
. insert_at ( syn_seq_num, syn, self . config . initial_timeout ) ;
@@ -250,7 +255,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
250
255
Endpoint :: Acceptor ( ( syn, syn_ack) ) => {
251
256
let state = self . state_packet ( ) . unwrap ( ) ;
252
257
self . socket_events
253
- . send ( SocketEvent :: Outgoing ( ( state, self . cid . peer . clone ( ) ) ) )
258
+ . send ( SocketEvent :: Outgoing ( ( state, self . peer . clone ( ) ) ) )
254
259
. unwrap ( ) ;
255
260
256
261
let recv_buf = ReceiveBuffer :: new ( syn) ;
@@ -409,7 +414,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
409
414
& mut self . unacked ,
410
415
& mut self . socket_events ,
411
416
fin,
412
- & self . cid . peer ,
417
+ & self . peer ,
413
418
Instant :: now ( ) ,
414
419
) ;
415
420
}
@@ -441,7 +446,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
441
446
& mut self . unacked ,
442
447
& mut self . socket_events ,
443
448
fin,
444
- & self . cid . peer ,
449
+ & self . peer ,
445
450
Instant :: now ( ) ,
446
451
) ;
447
452
}
@@ -542,7 +547,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
542
547
& mut self . unacked ,
543
548
& mut self . socket_events ,
544
549
packet,
545
- & self . cid . peer ,
550
+ & self . peer ,
546
551
now,
547
552
) ;
548
553
seq_num = seq_num. wrapping_add ( 1 ) ;
@@ -680,7 +685,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
680
685
let packet = self . syn_packet ( seq) ;
681
686
let _ = self
682
687
. socket_events
683
- . send ( SocketEvent :: Outgoing ( ( packet, self . cid . peer . clone ( ) ) ) ) ;
688
+ . send ( SocketEvent :: Outgoing ( ( packet, self . peer . clone ( ) ) ) ) ;
684
689
}
685
690
}
686
691
Endpoint :: Acceptor ( ..) => { }
@@ -728,7 +733,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
728
733
& mut self . unacked ,
729
734
& mut self . socket_events ,
730
735
packet,
731
- & self . cid . peer ,
736
+ & self . peer ,
732
737
now,
733
738
) ;
734
739
}
@@ -784,7 +789,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
784
789
match packet. packet_type ( ) {
785
790
PacketType :: Syn | PacketType :: Fin | PacketType :: Data => {
786
791
if let Some ( state) = self . state_packet ( ) {
787
- let event = SocketEvent :: Outgoing ( ( state, self . cid . peer . clone ( ) ) ) ;
792
+ let event = SocketEvent :: Outgoing ( ( state, self . peer . clone ( ) ) ) ;
788
793
if self . socket_events . send ( event) . is_err ( ) {
789
794
tracing:: warn!( "Cannot transmit state packet: socket closed channel" ) ;
790
795
return ;
@@ -1156,7 +1161,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
1156
1161
& mut self . unacked ,
1157
1162
& mut self . socket_events ,
1158
1163
packet,
1159
- & self . cid . peer ,
1164
+ & self . peer ,
1160
1165
now,
1161
1166
) ;
1162
1167
}
@@ -1167,7 +1172,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
1167
1172
unacked : & mut HashMapDelay < u16 , Packet > ,
1168
1173
socket_events : & mut mpsc:: UnboundedSender < SocketEvent < P > > ,
1169
1174
packet : Packet ,
1170
- dest : & P ,
1175
+ peer : & Peer < P > ,
1171
1176
now : Instant ,
1172
1177
) {
1173
1178
let ( payload, len) = if packet. payload ( ) . is_empty ( ) {
@@ -1189,7 +1194,7 @@ impl<const N: usize, P: ConnectionPeer> Connection<N, P> {
1189
1194
1190
1195
sent_packets. on_transmit ( packet. seq_num ( ) , packet. packet_type ( ) , payload, len, now) ;
1191
1196
unacked. insert_at ( packet. seq_num ( ) , packet. clone ( ) , sent_packets. timeout ( ) ) ;
1192
- let outbound = SocketEvent :: Outgoing ( ( packet, dest . clone ( ) ) ) ;
1197
+ let outbound = SocketEvent :: Outgoing ( ( packet, peer . clone ( ) ) ) ;
1193
1198
if socket_events. send ( outbound) . is_err ( ) {
1194
1199
tracing:: warn!( "Cannot transmit packet: socket closed channel" ) ;
1195
1200
}
@@ -1214,12 +1219,13 @@ mod test {
1214
1219
let cid = ConnectionId {
1215
1220
send : 101 ,
1216
1221
recv : 100 ,
1217
- peer,
1222
+ peer_id : peer,
1218
1223
} ;
1219
1224
1220
1225
Connection {
1221
1226
state : State :: Connecting ( Some ( connected) ) ,
1222
1227
cid,
1228
+ peer : Peer :: new ( peer) ,
1223
1229
config : ConnectionConfig :: default ( ) ,
1224
1230
endpoint,
1225
1231
peer_ts_diff : Duration :: from_millis ( 100 ) ,
0 commit comments