Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Add support for concurrent send/recv apis for tun device #87

Merged
merged 3 commits into from
Jul 23, 2024

Conversation

kp-mariappan-ramasamy
Copy link

Tokio supports async api's for UDP socket which does not need &mut self

pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>

This makes it pretty easy to call it concurrently, without adding Arc<Mutex> to UdpSocket.

This PR tries to add support for similar apis to tun device in tun2 crate.

Reference:
https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#method.recv
https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#method.send

This PR does not take care of windows, which is not as straight-forward as posix variants.

src/platform/posix/split.rs Outdated Show resolved Hide resolved
src/platform/posix/split.rs Outdated Show resolved Hide resolved
Tokio supports async api's for UDP socket which does not need `&mut self`

pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>

This makes it pretty easy to call it concurrently, without adding Arc<Mutex<T>> to
UdpSocket.

This PR tries to add support for similar apis to tun device.
In this commit, only sync version of recv/send apis are added. Async version will be added in
further commit.

Reference:
https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#method.recv
https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#method.send
@kp-mariappan-ramasamy kp-mariappan-ramasamy force-pushed the async-send-recv-apis branch 2 times, most recently from ffc404d to 159781d Compare July 23, 2024 02:16
@kp-mariappan-ramasamy
Copy link
Author

Thanks @xmh0511 for the review. I have addressed the comments and tested in linux and android platform.
Also added unimplemented!() api for windows platform.

Can you please take a look again ?

@xmh0511
Copy link

xmh0511 commented Jul 23, 2024

Thanks @xmh0511 for the review. I have addressed the comments and tested in linux and android platform. Also added unimplemented!() api for windows platform.

Can you please take a look again ?

Everything sounds good to me, I will merge this PR after checking passed.

Since unix uses AsyncFd from tokio, it is easier to wait for Ready
on AsyncFd and call sync send/recv apis

This commit does not take care of windows, which is not as straight-forward as posix variants.
@xmh0511 xmh0511 merged commit 93af5c3 into ssrlive:v2 Jul 23, 2024
6 checks passed
@kp-mariappan-ramasamy kp-mariappan-ramasamy deleted the async-send-recv-apis branch July 23, 2024 02:53
kp-mariappan-ramasamy added a commit to kp-mariappan-ramasamy/rust-tun that referenced this pull request Jul 23, 2024
When adding support for async send/recv apis in ssrlive#87, ios and android builds got broken with following error

    error[E0433]: failed to resolve: use of undeclared crate or module `io`

Root cause:
Both ios and android platform code did not import std::io::self which caused it to fail.

Updated it to use full path to fix it
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants