-
Notifications
You must be signed in to change notification settings - Fork 121
feat(net): incoming stream #759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
74e3c4b
feat(net): incoming
Berrysoft cab808e
feat(net): tcp & unix incoming
Berrysoft f1012d3
test(net): incoming
Berrysoft 2068a8c
feat(net): incoming on windows
Berrysoft c9666b9
feat(net): impl FusedStream for *Incoming
Berrysoft a164bb2
test(net): incoming uds
Berrysoft b155ace
fix(net): deps
Berrysoft 3c6149d
refactor(net): move incoming to a separate mod
Berrysoft 02d0fcd
fix(net): don't issue new AcceptMulti before next poll
Berrysoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| cfg_if::cfg_if! { | ||
| if #[cfg(windows)] { | ||
| #[path = "windows.rs"] | ||
| mod sys; | ||
| } else if #[cfg(unix)] { | ||
| #[path = "unix.rs"] | ||
| mod sys; | ||
| } | ||
| } | ||
|
|
||
| pub use sys::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use std::{ | ||
| io, | ||
| os::fd::FromRawFd, | ||
| pin::Pin, | ||
| task::{Context, Poll, ready}, | ||
| }; | ||
|
|
||
| use compio_buf::{BufResult, IntoInner}; | ||
| use compio_driver::{SharedFd, ToSharedFd, op::AcceptMulti}; | ||
| use compio_runtime::SubmitMulti; | ||
| use futures_util::{Stream, StreamExt, stream::FusedStream}; | ||
| use socket2::Socket as Socket2; | ||
|
|
||
| use crate::Socket; | ||
|
|
||
| pub struct Incoming<'a> { | ||
| listener: &'a Socket, | ||
| op: Option<SubmitMulti<AcceptMulti<SharedFd<Socket2>>>>, | ||
| } | ||
|
|
||
| impl<'a> Incoming<'a> { | ||
| pub fn new(listener: &'a Socket) -> Self { | ||
| Self { listener, op: None } | ||
| } | ||
| } | ||
|
|
||
| impl Stream for Incoming<'_> { | ||
| type Item = io::Result<Socket>; | ||
|
|
||
| fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| let this = self.get_mut(); | ||
| loop { | ||
| if let Some(op) = &mut this.op { | ||
| let res = ready!(op.poll_next_unpin(cx)); | ||
| if let Some(BufResult(res, _)) = res { | ||
| let socket = if op.is_terminated() && res.is_ok() { | ||
| let Some(op) = this.op.take() else { | ||
| // SAFETY: op is guaranteed to be Some at this point. | ||
| unsafe { std::hint::unreachable_unchecked() } | ||
| }; | ||
| op.try_take() | ||
| .map_err(|_| ()) | ||
| .expect("AcceptMulti has not completed") | ||
| .into_inner() | ||
| } else { | ||
| unsafe { Socket2::from_raw_fd(res? as _) } | ||
| }; | ||
| return Poll::Ready(Some(Socket::from_socket2(socket))); | ||
| } else { | ||
| this.op = None; | ||
| } | ||
| } else { | ||
| this.op = Some(compio_runtime::submit_multi(AcceptMulti::new( | ||
| this.listener.to_shared_fd(), | ||
| ))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FusedStream for Incoming<'_> { | ||
| fn is_terminated(&self) -> bool { | ||
| false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| use std::{ | ||
| io, | ||
| pin::Pin, | ||
| task::{Context, Poll, ready}, | ||
| }; | ||
|
|
||
| use compio_buf::BufResult; | ||
| use compio_driver::{SharedFd, ToSharedFd, op::Accept}; | ||
| use compio_runtime::{JoinHandle, Submit}; | ||
| use futures_util::{FutureExt, Stream, stream::FusedStream}; | ||
| use socket2::Socket as Socket2; | ||
|
|
||
| use crate::Socket; | ||
|
|
||
| #[allow(clippy::large_enum_variant)] | ||
| enum IncomingState { | ||
| Idle, | ||
| CreatingSocket(JoinHandle<io::Result<Socket2>>), | ||
| Accepting(Submit<Accept<SharedFd<Socket2>>>), | ||
| } | ||
|
|
||
| pub struct Incoming<'a> { | ||
| listener: &'a Socket, | ||
| state: IncomingState, | ||
| } | ||
|
|
||
| impl<'a> Incoming<'a> { | ||
| pub fn new(listener: &'a Socket) -> Self { | ||
| Self { | ||
| listener, | ||
| state: IncomingState::Idle, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Stream for Incoming<'_> { | ||
| type Item = io::Result<Socket>; | ||
|
|
||
| fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| let this = self.get_mut(); | ||
| loop { | ||
| match &mut this.state { | ||
| IncomingState::Idle => { | ||
| let domain = this.listener.local_addr().map(|addr| addr.domain())?; | ||
| let ty = this.listener.socket.r#type()?; | ||
| let protocol = this.listener.socket.protocol()?; | ||
| let handle = | ||
| compio_runtime::spawn_blocking(move || Socket2::new(domain, ty, protocol)); | ||
| this.state = IncomingState::CreatingSocket(handle); | ||
| } | ||
| IncomingState::CreatingSocket(handle) => match ready!(handle.poll_unpin(cx)) { | ||
| Ok(Ok(socket)) => { | ||
| let op = compio_runtime::submit(Accept::new( | ||
| this.listener.to_shared_fd(), | ||
| socket, | ||
| )); | ||
| this.state = IncomingState::Accepting(op); | ||
| } | ||
| Ok(Err(e)) => { | ||
| this.state = IncomingState::Idle; | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| Err(e) => { | ||
| this.state = IncomingState::Idle; | ||
| std::panic::resume_unwind(e) | ||
| } | ||
| }, | ||
| IncomingState::Accepting(op) => { | ||
| let BufResult(res, op) = ready!(op.poll_unpin(cx)); | ||
| match res { | ||
| Ok(_) => { | ||
| this.state = IncomingState::Idle; | ||
| op.update_context()?; | ||
| let (accept_sock, _) = op.into_addr()?; | ||
| return Poll::Ready(Some(Ok(Socket::from_socket2(accept_sock)?))); | ||
| } | ||
| Err(e) => { | ||
| this.state = IncomingState::Idle; | ||
| return Poll::Ready(Some(Err(e))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FusedStream for Incoming<'_> { | ||
| fn is_terminated(&self) -> bool { | ||
| false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.