Skip to content

Commit 0f9e0c1

Browse files
authored
Merge pull request #123 from Totodore/fix-different-sid-for-socketio-ns
fix(socketio/socket): create a different socket id for each ns
2 parents 260231d + 72ba75b commit 0f9e0c1

File tree

5 files changed

+39
-38
lines changed

5 files changed

+39
-38
lines changed

engineioxide/src/socket.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,12 @@ impl<D> Socket<D>
399399
where
400400
D: Default + Send + Sync + 'static,
401401
{
402-
pub fn new_dummy(
403-
sid: Sid,
404-
close_fn: Box<dyn Fn(Sid, DisconnectReason) + Send + Sync>,
405-
) -> Socket<D> {
402+
pub fn new_dummy(close_fn: Box<dyn Fn(Sid, DisconnectReason) + Send + Sync>) -> Socket<D> {
406403
let (internal_tx, internal_rx) = mpsc::channel(200);
407404
let (heartbeat_tx, heartbeat_rx) = mpsc::channel(1);
408405

409406
Self {
410-
id: sid,
407+
id: Sid::new(),
411408
protocol: ProtocolVersion::V4,
412409
transport: AtomicU8::new(TransportType::Websocket as u8),
413410

socketioxide/src/client.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,13 @@ impl<A: Adapter> Client<A> {
6060
auth: Option<String>,
6161
ns_path: String,
6262
esocket: &Arc<engineioxide::Socket<SocketData>>,
63-
) -> Result<(), serde_json::Error> {
63+
) -> Result<(), Error> {
6464
debug!("auth: {:?}", auth);
6565
let sid = esocket.id;
6666
if let Some(ns) = self.get_ns(&ns_path) {
67-
let protocol: ProtocolVersion = esocket.protocol.into();
67+
ns.connect(sid, esocket.clone(), auth, self.config.clone())?;
6868

6969
// cancel the connect timeout task for v5
70-
#[cfg(feature = "v5")]
71-
if let Some(tx) = esocket.data.connect_recv_tx.lock().unwrap().take() {
72-
tx.send(()).unwrap();
73-
}
74-
75-
let connect_packet = Packet::connect(ns_path, sid, protocol);
76-
if let Err(err) = esocket.emit(connect_packet.try_into()?) {
77-
debug!("sending error during socket connection: {err:?}");
78-
}
79-
ns.connect(sid, esocket.clone(), auth, self.config.clone())?;
8070
if let Some(tx) = esocket.data.connect_recv_tx.lock().unwrap().take() {
8171
tx.send(()).unwrap();
8272
}
@@ -108,12 +98,9 @@ impl<A: Adapter> Client<A> {
10898

10999
/// Propagate a packet to a its target namespace
110100
fn sock_propagate_packet(&self, packet: Packet, sid: Sid) -> Result<(), Error> {
111-
if let Some(ns) = self.get_ns(&packet.ns) {
112-
ns.recv(sid, packet.inner)
113-
} else {
114-
debug!("invalid namespace requested: {}", packet.ns);
115-
Ok(())
116-
}
101+
self.get_ns(&packet.ns)
102+
.ok_or(Error::InvalidNamespace(packet.ns))?
103+
.recv(sid, packet.inner)
117104
}
118105

119106
/// Spawn a task that will close the socket if it is not connected to a namespace

socketioxide/src/errors.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ pub enum Error {
1919
InvalidEventName,
2020

2121
#[error("invalid namespace")]
22-
InvalidNamespace,
22+
InvalidNamespace(String),
2323

2424
#[error("cannot find socketio socket")]
2525
SocketGone(Sid),
2626

27+
#[error("send error: {0}")]
28+
SendError(#[from] SendError),
29+
2730
/// An engineio error
2831
#[error("engineio error: {0}")]
2932
EngineIoError(#[from] engineioxide::errors::Error),
@@ -44,7 +47,7 @@ impl From<&Error> for Option<EIoDisconnectReason> {
4447
Error::SerializeError(_) | Error::InvalidPacketType | Error::InvalidEventName => {
4548
Some(PacketParsingError)
4649
}
47-
Error::Adapter(_) | Error::InvalidNamespace => None,
50+
Error::Adapter(_) | Error::InvalidNamespace(_) | Error::SendError(_) => None,
4851
}
4952
}
5053
}

socketioxide/src/ns.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use crate::{
77
adapter::Adapter,
88
errors::Error,
99
handler::{BoxedNamespaceHandler, CallbackHandler},
10-
packet::PacketData,
10+
packet::{Packet, PacketData},
1111
socket::Socket,
12-
SocketIoConfig,
12+
ProtocolVersion, SocketIoConfig,
1313
};
1414
use crate::{client::SocketData, errors::AdapterError};
1515
use engineioxide::sid::Sid;
@@ -46,10 +46,15 @@ impl<A: Adapter> Namespace<A> {
4646
esocket: Arc<engineioxide::Socket<SocketData>>,
4747
auth: Option<String>,
4848
config: Arc<SocketIoConfig>,
49-
) -> Result<(), serde_json::Error> {
50-
let socket: Arc<Socket<A>> = Socket::new(sid, self.clone(), esocket, config).into();
49+
) -> Result<(), Error> {
50+
let protocol: ProtocolVersion = esocket.protocol.into();
51+
let socket: Arc<Socket<A>> = Socket::new(self.clone(), esocket, config).into();
5152
self.sockets.write().unwrap().insert(sid, socket.clone());
52-
self.handler.call(socket, auth)
53+
54+
socket.send(Packet::connect(self.path.clone(), socket.id, protocol))?;
55+
56+
self.handler.call(socket, auth)?;
57+
Ok(())
5358
}
5459

5560
/// Remove a socket from a namespace and propagate the event to the adapter

socketioxide/src/socket.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
time::Duration,
1010
};
1111

12-
use engineioxide::{sid::Sid, socket::DisconnectReason as EIoDisconnectReason};
12+
use engineioxide::{sid::Sid, socket::DisconnectReason as EIoDisconnectReason, ProtocolVersion};
1313
use futures::{future::BoxFuture, Future};
1414
use serde::{de::DeserializeOwned, Serialize};
1515
use serde_json::Value;
@@ -110,18 +110,22 @@ pub struct Socket<A: Adapter> {
110110

111111
impl<A: Adapter> Socket<A> {
112112
pub(crate) fn new(
113-
sid: Sid,
114113
ns: Arc<Namespace<A>>,
115114
esocket: Arc<engineioxide::Socket<SocketData>>,
116115
config: Arc<SocketIoConfig>,
117116
) -> Self {
117+
let id = if esocket.protocol == ProtocolVersion::V3 {
118+
esocket.id
119+
} else {
120+
Sid::new()
121+
};
118122
Self {
119123
ns,
120124
message_handlers: RwLock::new(HashMap::new()),
121125
disconnect_handler: Mutex::new(None),
122126
ack_message: Mutex::new(HashMap::new()),
123127
ack_counter: AtomicI64::new(0),
124-
id: sid,
128+
id,
125129
extensions: Extensions::new(),
126130
config,
127131
esocket,
@@ -583,11 +587,16 @@ impl<A: Adapter> Debug for Socket<A> {
583587
impl<A: Adapter> Socket<A> {
584588
pub fn new_dummy(sid: Sid, ns: Arc<Namespace<A>>) -> Socket<A> {
585589
let close_fn = Box::new(move |_, _| ());
586-
Socket::new(
587-
sid,
590+
Socket {
591+
id: sid,
588592
ns,
589-
engineioxide::Socket::new_dummy(sid, close_fn).into(),
590-
Arc::new(SocketIoConfig::default()),
591-
)
593+
ack_counter: AtomicI64::new(0),
594+
ack_message: Mutex::new(HashMap::new()),
595+
message_handlers: RwLock::new(HashMap::new()),
596+
disconnect_handler: Mutex::new(None),
597+
config: Arc::new(SocketIoConfig::default()),
598+
extensions: Extensions::new(),
599+
esocket: engineioxide::Socket::new_dummy(close_fn).into(),
600+
}
592601
}
593602
}

0 commit comments

Comments
 (0)