-
-
Notifications
You must be signed in to change notification settings - Fork 75
feat(engineio-client): wip Stream + Sink based client implementation
#555
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
Draft
Totodore
wants to merge
16
commits into
main
Choose a base branch
from
engineioxide-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b293bd1
wip
Totodore b1c6445
Merge branch 'main' into engineioxide-client
Totodore a387107
Merge remote-tracking branch 'origin/main' into engineioxide-client
Totodore cb13f2f
feat(engineio): refactor shared types to core
Totodore 7266fc8
feat(engineio): refactor shared types to core
Totodore a7f23e0
feat(client): wip
Totodore 63011e0
feat(client): wip
Totodore 60d2788
feat(client): wip
Totodore 765d055
Merge remote-tracking branch 'origin/main' into engineioxide-client
Totodore 9dd5988
fix(engineio): add content type to decoder
Totodore 24ea9c5
feat(client): wip
Totodore c27e3b1
Merge remote-tracking branch 'origin/main' into engineioxide-client
Totodore d111347
Merge branch 'main' into engineioxide-client
Totodore edc3354
Merge branch 'main' into engineioxide-client
Totodore 1fac3aa
wip
Totodore 4a4439f
wip
Totodore 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,44 @@ | ||
| [package] | ||
| name = "engineioxide-client" | ||
| description = "Engine IO client implementation in rust" | ||
| version = "0.17.0" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| authors.workspace = true | ||
| repository.workspace = true | ||
| homepage.workspace = true | ||
| keywords.workspace = true | ||
| categories.workspace = true | ||
| license.workspace = true | ||
| readme = "README.md" | ||
|
|
||
| [dependencies] | ||
| engineioxide-core = { path = "../engineioxide-core", version = "0.2" } | ||
| bytes.workspace = true | ||
| futures-core.workspace = true | ||
| futures-util.workspace = true | ||
| http.workspace = true | ||
| http-body.workspace = true | ||
| serde.workspace = true | ||
| serde_json.workspace = true | ||
| thiserror.workspace = true | ||
| tokio = { workspace = true, features = ["rt", "time"] } | ||
| hyper = { workspace = true, features = ["client", "http1"] } | ||
| tokio-tungstenite.workspace = true | ||
| http-body-util.workspace = true | ||
| pin-project-lite.workspace = true | ||
| smallvec.workspace = true | ||
| hyper-util = { workspace = true, features = ["tokio"] } | ||
|
|
||
| # Tracing | ||
| tracing = { workspace = true, optional = true } | ||
|
|
||
| [dev-dependencies] | ||
| tokio = { workspace = true, features = ["macros", "parking_lot"] } | ||
| tracing-subscriber = { workspace = true, features = ["env-filter"] } | ||
| engineioxide = { path = "../engineioxide", features = ["tracing", "v3"] } | ||
|
|
||
| [features] | ||
| v3 = ["engineioxide-core/v3"] | ||
| tracing = ["dep:tracing", "engineioxide-core/tracing"] | ||
| __test_harness = [] |
Empty file.
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,124 @@ | ||
| use std::{ | ||
| fmt, | ||
| pin::Pin, | ||
| sync::Mutex, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use engineioxide_core::{Packet, PacketBuf, PacketParseError, Sid}; | ||
| use futures_core::Stream; | ||
| use futures_util::{ | ||
| Sink, StreamExt, | ||
| stream::{SplitSink, SplitStream}, | ||
| }; | ||
| use tokio::sync::mpsc::{self, error::TrySendError}; | ||
|
|
||
| use crate::{ | ||
| HttpClient, poll, | ||
| transport::{Transport, polling::PollingSvc}, | ||
| }; | ||
|
|
||
| pin_project_lite::pin_project! { | ||
| pub struct Client<S: PollingSvc> { | ||
| #[pin] | ||
| pub transport_rx: SplitStream<Transport<S>>, | ||
| // TODO: is this the right implementation? We need something that can be driven itself. | ||
| // Otherwise we need a way to drive the transport_tx. Normally it should be driven by the user. | ||
| // But what if we need to send a PONG packet from the inner lib? | ||
| #[pin] | ||
| pub transport_tx: SplitSink<Transport<S>, Packet>, | ||
| pub sid: Sid, | ||
| pub tx: mpsc::Sender<PacketBuf>, | ||
| pub(crate) rx: Mutex<mpsc::Receiver<PacketBuf>>, | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Client<S> | ||
| where | ||
| S::Error: fmt::Debug, | ||
| <S::Body as http_body::Body>::Error: fmt::Debug, | ||
| { | ||
| pub async fn connect(svc: S) -> Result<Self, ()> { | ||
| let (tx, rx) = mpsc::channel(255); | ||
| let mut inner = HttpClient::new(svc); | ||
| let packet = inner.handshake().await.unwrap(); | ||
|
|
||
| let transport = Transport::Polling { inner }; | ||
| let (transport_tx, transport_rx) = transport.split(); | ||
| let client = Client { | ||
| transport_tx, | ||
| transport_rx, | ||
| sid: packet.sid, | ||
| tx, | ||
| rx: Mutex::new(rx), | ||
| }; | ||
|
|
||
| Ok(client) | ||
| } | ||
|
|
||
| pub fn connected(&self) -> bool { | ||
| self.rx.try_lock().is_ok() | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Sink<PacketBuf> for Client<S> { | ||
| type Error = TrySendError<PacketBuf>; | ||
|
|
||
| fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn start_send(self: Pin<&mut Self>, packets: PacketBuf) -> Result<(), Self::Error> { | ||
| self.tx.try_send(packets) | ||
| } | ||
|
|
||
| fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| Poll::Ready(Ok(())) | ||
| } | ||
|
|
||
| fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
|
||
| Poll::Ready(Ok(())) | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Stream for Client<S> | ||
| where | ||
| S::Error: fmt::Debug, | ||
| <S::Body as http_body::Body>::Error: fmt::Debug, | ||
| { | ||
| type Item = Result<Packet, PacketParseError>; | ||
| fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| let project = self.project(); | ||
| match poll!(project.transport_rx.poll_next(cx)) { | ||
| Some(Ok(Packet::Ping)) => { | ||
| cx.waker().wake_by_ref(); | ||
| Poll::Pending | ||
| } | ||
| packet => Poll::Ready(packet), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Sink<Packet> for Client<S> | ||
| where | ||
| S::Error: fmt::Debug, | ||
| <S::Body as http_body::Body>::Error: fmt::Debug, | ||
| { | ||
| type Error = (); | ||
|
|
||
| fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.project().transport_tx.poll_ready(cx) | ||
| } | ||
|
|
||
| fn start_send(self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> { | ||
| self.project().transport_tx.start_send(item) | ||
| } | ||
|
|
||
| fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.project().transport_tx.poll_flush(cx) | ||
| } | ||
|
|
||
| fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| self.project().transport_tx.poll_close(cx) | ||
| } | ||
| } | ||
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 @@ | ||
|
|
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,19 @@ | ||
| // #![warn(clippy::pedantic)] | ||
| #![allow(clippy::similar_names)] | ||
| //! Engine.IO client library for Rust. | ||
|
|
||
| mod client; | ||
| mod io; | ||
| mod transport; | ||
| pub use crate::client::Client; | ||
| pub use crate::transport::polling::HttpClient; | ||
|
|
||
| #[macro_export] | ||
| macro_rules! poll { | ||
| ($expr:expr) => { | ||
| match $expr { | ||
| std::task::Poll::Pending => return std::task::Poll::Pending, | ||
| std::task::Poll::Ready(value) => value, | ||
| } | ||
| }; | ||
| } |
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,81 @@ | ||
| use std::{ | ||
| fmt, | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| use engineioxide_core::{Packet, PacketParseError, TransportType}; | ||
| use futures_core::Stream; | ||
| use futures_util::Sink; | ||
|
|
||
| use crate::{HttpClient, transport::polling::PollingSvc}; | ||
|
|
||
| pub mod polling; | ||
|
|
||
| pin_project_lite::pin_project! { | ||
| #[project = TransportProj] | ||
| pub enum Transport<S: PollingSvc> { | ||
| Polling { | ||
| #[pin] | ||
| inner: HttpClient<S> | ||
| }, | ||
| Websocket { | ||
| #[pin] | ||
| inner: HttpClient<S> | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Transport<S> { | ||
| pub fn transport_type(&self) -> TransportType { | ||
| match self { | ||
| Transport::Polling { .. } => TransportType::Polling, | ||
| Transport::Websocket { .. } => TransportType::Websocket, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S: PollingSvc> Stream for Transport<S> | ||
| where | ||
| S::Error: fmt::Debug, | ||
| <S::Body as http_body::Body>::Error: fmt::Debug, | ||
| { | ||
| type Item = Result<Packet, PacketParseError>; | ||
| fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| match self.as_mut().project() { | ||
| TransportProj::Polling { inner } => inner.poll_next(cx), | ||
| TransportProj::Websocket { inner } => inner.poll_next(cx), | ||
| } | ||
| } | ||
| } | ||
| impl<S: PollingSvc> Sink<Packet> for Transport<S> { | ||
| type Error = (); | ||
|
|
||
| fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| match self.project() { | ||
| TransportProj::Polling { inner } => inner.poll_ready(cx), | ||
| TransportProj::Websocket { inner } => inner.poll_ready(cx), | ||
| } | ||
| } | ||
|
|
||
| fn start_send(self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> { | ||
| match self.project() { | ||
| TransportProj::Polling { inner } => inner.start_send(item), | ||
| TransportProj::Websocket { inner } => inner.start_send(item), | ||
| } | ||
| } | ||
|
|
||
| fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| match self.project() { | ||
| TransportProj::Polling { inner } => inner.poll_flush(cx), | ||
| TransportProj::Websocket { inner } => inner.poll_flush(cx), | ||
| } | ||
| } | ||
|
|
||
| fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
| match self.project() { | ||
| TransportProj::Polling { inner } => inner.poll_close(cx), | ||
| TransportProj::Websocket { inner } => inner.poll_close(cx), | ||
| } | ||
| } | ||
| } |
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.