Skip to content

Commit aff92eb

Browse files
committed
Return owned Connection on NewConnection and rely on close for cleanup
1 parent 58d833d commit aff92eb

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

src/rs/lib.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -867,9 +867,6 @@ extern "C" fn raw_conn_callback(
867867
event: *mut ffi::QUIC_CONNECTION_EVENT,
868868
) -> QUIC_STATUS {
869869
let event_ref = unsafe { event.as_ref().expect("cannot get connection event") };
870-
let cleanup_ctx =
871-
event_ref.Type == ffi::QUIC_CONNECTION_EVENT_TYPE_QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE;
872-
873870
let status = match unsafe { (context as *mut Box<ConnectionCallback>).as_mut() } {
874871
Some(f) => {
875872
let event = ConnectionEvent::from(event_ref);
@@ -883,11 +880,6 @@ extern "C" fn raw_conn_callback(
883880
None => StatusCode::QUIC_STATUS_SUCCESS.into(),
884881
};
885882

886-
if cleanup_ctx {
887-
let conn = unsafe { ConnectionRef::from_raw(connection) };
888-
conn.consume_callback_ctx();
889-
}
890-
891883
status
892884
}
893885

@@ -1058,14 +1050,12 @@ extern "C" fn raw_listener_callback(
10581050
) -> QUIC_STATUS {
10591051
let listner_ref = unsafe { ListenerRef::from_raw(listener) };
10601052
let event = ListenerEvent::from(unsafe { event.as_ref().expect("fail to get listener event") });
1061-
let f = unsafe {
1062-
(context as *mut Box<ListenerCallback>)
1063-
.as_ref() // allow mutation
1064-
.expect("cannot get ListenerCallback from ctx")
1065-
};
1066-
match f(listner_ref, event) {
1067-
Ok(_) => StatusCode::QUIC_STATUS_SUCCESS.into(),
1068-
Err(e) => e.0,
1053+
match unsafe { (context as *mut Box<ListenerCallback>).as_ref() } {
1054+
Some(f) => match f(listner_ref, event) {
1055+
Ok(_) => StatusCode::QUIC_STATUS_SUCCESS.into(),
1056+
Err(e) => e.0,
1057+
},
1058+
None => StatusCode::QUIC_STATUS_SUCCESS.into(),
10691059
}
10701060
}
10711061

src/rs/server_client_test.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ fn test_server_client() {
180180
} => {
181181
connection.set_callback_handler(conn_handler.clone());
182182
connection.set_configuration(&config_cp)?;
183+
// Keep the connection alive; will be closed on ShutdownComplete.
184+
let _ = unsafe { connection.into_raw() };
183185
}
184186
crate::ListenerEvent::StopComplete {
185187
app_close_in_progress: _,
@@ -345,6 +347,8 @@ fn connection_ref_callback_cleanup() {
345347
Ok(())
346348
});
347349
connection.set_configuration(&server_config)?;
350+
// Keep connection alive until ShutdownComplete closes it.
351+
let _ = unsafe { connection.into_raw() };
348352
}
349353
Ok(())
350354
}
@@ -391,6 +395,8 @@ fn connection_ref_callback_cleanup() {
391395
let raw_conn = server_handle_rx
392396
.recv_timeout(Duration::from_secs(5))
393397
.expect("Server did not receive shutdown event");
398+
// Close now to drop the server-side callback context.
399+
unsafe { Connection::from_raw(raw_conn) };
394400
client_done_rx
395401
.recv_timeout(Duration::from_secs(5))
396402
.expect("Client did not complete shutdown");
@@ -405,7 +411,5 @@ fn connection_ref_callback_cleanup() {
405411
1,
406412
"ConnectionRef callback context was not cleaned up"
407413
);
408-
409-
unsafe { Connection::from_raw(raw_conn) };
410414
listener.stop();
411415
}

src/rs/types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ pub enum ListenerEvent<'a> {
1212
/// User app needs to take ownership of this new connection.
1313
/// User app needs to set configuration for this connection
1414
/// before returning from the callback.
15-
/// TODO: Make this Connection type.
16-
connection: crate::ConnectionRef,
15+
connection: crate::Connection,
1716
},
1817
StopComplete {
1918
app_close_in_progress: bool,
@@ -67,7 +66,7 @@ impl<'a> From<&'a crate::ffi::QUIC_LISTENER_EVENT> for ListenerEvent<'a> {
6766
let ev = unsafe { &value.__bindgen_anon_1.NEW_CONNECTION };
6867
Self::NewConnection {
6968
info: NewConnectionInfo::from(unsafe { ev.Info.as_ref().unwrap() }),
70-
connection: unsafe { crate::ConnectionRef::from_raw(ev.Connection) },
69+
connection: unsafe { crate::Connection::from_raw(ev.Connection) },
7170
}
7271
}
7372
crate::ffi::QUIC_LISTENER_EVENT_TYPE_QUIC_LISTENER_EVENT_STOP_COMPLETE => {

0 commit comments

Comments
 (0)