How to call "async" function (ex: write data to tokio::net::TcpStream) in C callback function?

I have a Rust program that needs to get data from a C callback function and push to a tokio::net::TcpStream. However, the 'write' function of tokio is async and it does not seem to work with C function

The code looks somewhat like this:

extern "C" fn data_callback(callback_handler: *mut c_void, data: *mut u8, length: i32) {
    unsafe {        
        let mut socket = (&mut *(callback_handler as *mut CallbackHandler)).socket.as_mut().unwrap();
        // Prepare data [u8] from data and length
        // ....
        // Send to socket (await)

Is there any workaround for that? Thanks.

You can move a Handle into the callback function and call Handle::block_on.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.