Skip to content
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

feat(socketio/ns): store ns path and rooms as Cow<'static, str> #124

Merged
merged 23 commits into from Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
49665c3
feat(socketio/ns): store ns path as `Cow<'static, str>`
Totodore Oct 21, 2023
14d00e7
Merge branch 'main' into feat-cow-namespace-and-rooms
Totodore Oct 21, 2023
20f89a5
fix(clippy): useless conversions and immediate dereference
Totodore Oct 28, 2023
6b4ed75
Merge remote-tracking branch 'origin/main' into feat-cow-namespace-an…
Totodore Oct 28, 2023
837e7b9
feat(socketio/packet): move event type from String to `Cow<'static, s…
Totodore Oct 28, 2023
8ae3e16
feat(socketio/packet): remove `ConnectErrorPacket`
Totodore Oct 28, 2023
c15bc61
feat(socketio/config): remove duplicated engine io path
Totodore Oct 28, 2023
c75e903
feat(socketio/adapter): move `Room` type from `String` to `Cow<'stati…
Totodore Oct 28, 2023
e2098d6
fix(socketio/packet): connect error layout
Totodore Oct 28, 2023
e494b93
fix(clippy): redundant closure
Totodore Oct 29, 2023
01890b9
fix(socketio/packet): connect error layout
Totodore Oct 29, 2023
e02a2a9
fix(socketio/packet): fix packet encode connect
Totodore Oct 29, 2023
4bd0b7b
fix(socketio/packet): remove useless connect error decode test
Totodore Oct 29, 2023
de543fa
Merge remote-tracking branch 'origin/main' into feat-cow-namespace-an…
Totodore Oct 29, 2023
2acd187
bench(socketio): fix bench to work with new packet impl
Totodore Oct 29, 2023
ceaaf67
feat(socketio/packet): preallocate a buffer when encoding a packet
Totodore Oct 29, 2023
e370fad
fix(socketio/packet): put a / in front of non /* nsps
Totodore Oct 29, 2023
d6f8f09
feat(socketio/packet): decode packets from bytes rather than char iter
Totodore Oct 29, 2023
21f0531
fix(clippy): `manual check for common ascii range`
Totodore Oct 29, 2023
576a1e0
test(socketio/packet): add a test for `get_size_hint`
Totodore Oct 29, 2023
c13fd50
fix(clippy): `single-character string constant used as pattern`
Totodore Oct 29, 2023
eb20f85
feat(socketio): switch from to manual impl for number insertion
Totodore Oct 29, 2023
53db020
Revert "feat(socketio): switch from to manual impl for number insert…
Totodore Oct 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions socketioxide/src/adapter.rs
Expand Up @@ -95,7 +95,7 @@ pub trait Adapter: std::fmt::Debug + Send + Sync + 'static {
/// Broadcast the packet to the sockets that match the [`BroadcastOptions`] and return a stream of ack responses.
fn broadcast_with_ack<V: DeserializeOwned>(
&self,
packet: Packet,
packet: Packet<'static>,
opts: BroadcastOptions,
) -> Result<BoxStream<'static, Result<AckResponse<V>, AckError>>, BroadcastError>;

Expand Down Expand Up @@ -207,7 +207,7 @@ impl Adapter for LocalAdapter {

fn broadcast_with_ack<V: DeserializeOwned>(
&self,
packet: Packet,
packet: Packet<'static>,
opts: BroadcastOptions,
) -> Result<BoxStream<'static, Result<AckResponse<V>, AckError>>, BroadcastError> {
let duration = opts.flags.iter().find_map(|flag| match flag {
Expand Down
17 changes: 9 additions & 8 deletions socketioxide/src/client.rs
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};

Expand All @@ -23,7 +24,7 @@
#[derive(Debug)]
pub struct Client<A: Adapter> {
pub(crate) config: Arc<SocketIoConfig>,
ns: RwLock<HashMap<String, Arc<Namespace<A>>>>,
ns: RwLock<HashMap<Cow<'static, str>, Arc<Namespace<A>>>>,
}

impl<A: Adapter> Client<A> {
Expand Down Expand Up @@ -58,7 +59,7 @@
fn sock_connect(
&self,
auth: Option<String>,
ns_path: String,
ns_path: &str,
esocket: &Arc<engineioxide::Socket<SocketData>>,
) -> Result<(), Error> {
debug!("auth: {:?}", auth);
Expand All @@ -78,7 +79,7 @@
esocket.close(EIoDisconnectReason::TransportClose);
Ok(())
} else {
let packet = Packet::invalid_namespace(ns_path).try_into().unwrap();
let packet = Packet::invalid_namespace(&ns_path).try_into().unwrap();
Fixed Show fixed Hide fixed
if let Err(e) = esocket.emit(packet) {
error!("error while sending invalid namespace packet: {}", e);
}
Expand All @@ -87,7 +88,7 @@
}

/// Cache-in the socket data until all the binary payloads are received
fn sock_recv_bin_packet(&self, socket: &EIoSocket<SocketData>, packet: Packet) {
fn sock_recv_bin_packet(&self, socket: &EIoSocket<SocketData>, packet: Packet<'static>) {
socket
.data
.partial_bin_packet
Expand All @@ -99,7 +100,7 @@
/// Propagate a packet to a its target namespace
fn sock_propagate_packet(&self, packet: Packet, sid: Sid) -> Result<(), Error> {
self.get_ns(&packet.ns)
.ok_or(Error::InvalidNamespace(packet.ns))?
.ok_or(Error::InvalidNamespace(packet.ns.to_string()))?
.recv(sid, packet.inner)
}

Expand All @@ -120,7 +121,7 @@
}

/// Add a new namespace handler
pub fn add_ns<C, F, V>(&self, path: String, callback: C)
pub fn add_ns<C, F, V>(&self, path: Cow<'static, str>, callback: C)
where
C: Fn(Arc<Socket<A>>, V) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + 'static,
Expand Down Expand Up @@ -155,7 +156,7 @@
pub struct SocketData {
/// Partial binary packet that is being received
/// Stored here until all the binary payloads are received
pub partial_bin_packet: Mutex<Option<Packet>>,
pub partial_bin_packet: Mutex<Option<Packet<'static>>>,

/// Channel used to notify the socket that it has been connected to a namespace
#[cfg(feature = "v5")]
Expand Down Expand Up @@ -217,7 +218,7 @@

let res: Result<(), Error> = match packet.inner {
PacketData::Connect(auth) => self
.sock_connect(auth, packet.ns, &socket)
.sock_connect(auth, &packet.ns, &socket)
.map_err(Into::into),
PacketData::BinaryEvent(_, _, _) | PacketData::BinaryAck(_, _) => {
self.sock_recv_bin_packet(&socket, packet);
Expand Down
6 changes: 3 additions & 3 deletions socketioxide/src/handler.rs
Expand Up @@ -142,7 +142,7 @@
/// Send the ack response to the client.
pub fn send(self, data: impl Serialize) -> Result<(), AckSenderError<A>> {
if let Some(ack_id) = self.ack_id {
let ns = self.socket.ns().clone();
let ns = self.socket.ns();
let data = match serde_json::to_value(&data) {
Err(err) => {
return Err(AckSenderError::SendError {
Expand All @@ -154,9 +154,9 @@
};

let packet = if self.binary.is_empty() {
Packet::ack(ns, data, ack_id)
Packet::ack(&ns, data, ack_id)
Fixed Show fixed Hide fixed
} else {
Packet::bin_ack(ns, data, self.binary, ack_id)
Packet::bin_ack(&ns, data, self.binary, ack_id)
Fixed Show fixed Hide fixed
};
self.socket
.send(packet)
Expand Down
4 changes: 2 additions & 2 deletions socketioxide/src/io.rs
@@ -1,4 +1,4 @@
use std::{sync::Arc, time::Duration};
use std::{borrow::Cow, sync::Arc, time::Duration};

use engineioxide::{
config::{EngineIoConfig, EngineIoConfigBuilder, TransportType},
Expand Down Expand Up @@ -320,7 +320,7 @@ impl<A: Adapter> SocketIo<A> {
///
/// ```
#[inline]
pub fn ns<C, F, V>(&self, path: impl Into<String>, callback: C)
pub fn ns<C, F, V>(&self, path: impl Into<Cow<'static, str>>, callback: C)
where
C: Fn(Arc<Socket<A>>, V) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + 'static,
Expand Down
9 changes: 5 additions & 4 deletions socketioxide/src/ns.rs
@@ -1,4 +1,5 @@
use std::{
borrow::Cow,
collections::HashMap,
sync::{Arc, RwLock},
};
Expand All @@ -17,14 +18,14 @@ use futures::Future;
use serde::de::DeserializeOwned;

pub struct Namespace<A: Adapter> {
pub path: String,
pub path: Cow<'static, str>,
pub(crate) adapter: A,
handler: BoxedNamespaceHandler<A>,
sockets: RwLock<HashMap<Sid, Arc<Socket<A>>>>,
}

impl<A: Adapter> Namespace<A> {
pub fn new<C, F, V>(path: String, callback: C) -> Arc<Self>
pub fn new<C, F, V>(path: Cow<'static, str>, callback: C) -> Arc<Self>
where
C: Fn(Arc<Socket<A>>, V) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + 'static,
Expand All @@ -51,7 +52,7 @@ impl<A: Adapter> Namespace<A> {
let socket: Arc<Socket<A>> = Socket::new(self.clone(), esocket, config).into();
self.sockets.write().unwrap().insert(sid, socket.clone());

socket.send(Packet::connect(self.path.clone(), socket.id, protocol))?;
socket.send(Packet::connect(&self.path, socket.id, protocol))?;

self.handler.call(socket, auth)?;
Ok(())
Expand Down Expand Up @@ -105,7 +106,7 @@ impl<A: Adapter> Namespace<A> {
#[cfg(test)]
impl<A: Adapter> Namespace<A> {
pub fn new_dummy<const S: usize>(sockets: [Sid; S]) -> Arc<Self> {
let ns = Namespace::new("/".to_string(), |_, _: ()| async {});
let ns = Namespace::new(Cow::Borrowed("/"), |_, _: ()| async {});
for sid in sockets {
ns.sockets
.write()
Expand Down
8 changes: 4 additions & 4 deletions socketioxide/src/operators.rs
Expand Up @@ -358,14 +358,14 @@ impl<A: Adapter> Operators<A> {
&mut self,
event: impl Into<String>,
data: impl serde::Serialize,
) -> Result<Packet, serde_json::Error> {
let ns = self.ns.clone();
) -> Result<Packet<'static>, serde_json::Error> {
let ns = self.ns.path.clone();
let data = serde_json::to_value(data)?;
let packet = if self.binary.is_empty() {
Packet::event(ns.path.clone(), event.into(), data)
Packet::event(ns, event.into(), data)
} else {
let binary = std::mem::take(&mut self.binary);
Packet::bin_event(ns.path.clone(), event.into(), data, binary)
Packet::bin_event(ns, event.into(), data, binary)
};
Ok(packet)
}
Expand Down