Skip to content

Commit

Permalink
feat(socketio): switch from to manual impl for number insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Totodore committed Oct 29, 2023
1 parent c13fd50 commit eb20f85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
27 changes: 18 additions & 9 deletions socketioxide/src/packet.rs
Expand Up @@ -204,10 +204,10 @@ impl<'a> PacketData<'a> {

/// Set the ack id for the packet
/// It will only set the ack id for the packets that support it
pub(crate) fn set_ack_id(&mut self, ack_id: i64) {
pub(crate) fn set_ack_id(&mut self, ack_id: u32) {
match self {
PacketData::Event(_, _, ack) | PacketData::BinaryEvent(_, _, ack) => {
*ack = Some(ack_id)
*ack = Some(ack_id as i64)
}
_ => {}
};
Expand Down Expand Up @@ -327,6 +327,15 @@ impl<'a> TryInto<String> for Packet<'a> {
}
};

fn insert_number(mut v: usize, res: &mut String) {
debug_assert!(v > 0);
while v > 0 {
let n = (v % 10) as u8;
v /= 10;
res.push((n + 0x30) as char);
}
}

if !self.inner.is_binary() {
push_nsp(&mut res);
}
Expand All @@ -336,35 +345,35 @@ impl<'a> TryInto<String> for Packet<'a> {
PacketData::Disconnect | PacketData::Connect(None) => (),
PacketData::Event(_, _, ack) => {
if let Some(ack) = ack {
res.push_str(&ack.to_string());
insert_number(ack as usize, &mut res);
}

res.push_str(&data.unwrap())
}
PacketData::EventAck(_, ack) => {
res.push_str(&ack.to_string());
insert_number(ack as usize, &mut res);
res.push_str(&data.unwrap())
}
PacketData::ConnectError => res.push_str("{\"message\":\"Invalid namespace\"}"),
PacketData::BinaryEvent(_, bin, ack) => {
res.push_str(&bin.payload_count.to_string());
insert_number(bin.payload_count, &mut res);
res.push('-');

push_nsp(&mut res);

if let Some(ack) = ack {
res.push_str(&ack.to_string());
insert_number(ack as usize, &mut res);
}

res.push_str(&data.unwrap())
}
PacketData::BinaryAck(packet, ack) => {
res.push_str(&packet.payload_count.to_string());
PacketData::BinaryAck(bin, ack) => {
insert_number(bin.payload_count, &mut res);
res.push('-');

push_nsp(&mut res);
insert_number(ack as usize, &mut res);

res.push_str(&ack.to_string());
res.push_str(&data.unwrap())
}
};
Expand Down
17 changes: 7 additions & 10 deletions socketioxide/src/socket.rs
Expand Up @@ -2,11 +2,8 @@ use std::{
borrow::Cow,
collections::HashMap,
fmt::Debug,
sync::Mutex,
sync::{
atomic::{AtomicI64, Ordering},
Arc, RwLock,
},
sync::{atomic::AtomicU32, Mutex},
sync::{atomic::Ordering, Arc, RwLock},
time::Duration,
};

Expand Down Expand Up @@ -103,8 +100,8 @@ pub struct Socket<A: Adapter> {
ns: Arc<Namespace<A>>,
message_handlers: RwLock<HashMap<String, BoxedMessageHandler<A>>>,
disconnect_handler: Mutex<Option<DisconnectCallback<A>>>,
ack_message: Mutex<HashMap<i64, oneshot::Sender<AckResponse<Value>>>>,
ack_counter: AtomicI64,
ack_message: Mutex<HashMap<u32, oneshot::Sender<AckResponse<Value>>>>,
ack_counter: AtomicU32,
pub id: Sid,

#[cfg(feature = "extensions")]
Expand All @@ -124,7 +121,7 @@ impl<A: Adapter> Socket<A> {
message_handlers: RwLock::new(HashMap::new()),
disconnect_handler: Mutex::new(None),
ack_message: Mutex::new(HashMap::new()),
ack_counter: AtomicI64::new(0),
ack_counter: AtomicU32::new(0),
id: sid,
#[cfg(feature = "extensions")]
extensions: Extensions::new(),
Expand Down Expand Up @@ -561,14 +558,14 @@ impl<A: Adapter> Socket<A> {
}

fn recv_ack(self: Arc<Self>, data: Value, ack: i64) -> Result<(), Error> {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&ack) {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&(ack as u32)) {
tx.send((data, vec![])).ok();
}
Ok(())
}

fn recv_bin_ack(self: Arc<Self>, packet: BinaryPacket, ack: i64) -> Result<(), Error> {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&ack) {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&(ack as u32)) {
tx.send((packet.data, packet.bin)).ok();
}
Ok(())
Expand Down

0 comments on commit eb20f85

Please sign in to comment.