Skip to content

Commit

Permalink
Revert "feat(socketio): switch from to manual impl for number insertion"
Browse files Browse the repository at this point in the history
This reverts commit eb20f85.
  • Loading branch information
Totodore committed Oct 30, 2023
1 parent eb20f85 commit 53db020
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
27 changes: 9 additions & 18 deletions socketioxide/src/packet.rs
Original file line number Diff line number Diff line change
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: u32) {
pub(crate) fn set_ack_id(&mut self, ack_id: i64) {
match self {
PacketData::Event(_, _, ack) | PacketData::BinaryEvent(_, _, ack) => {
*ack = Some(ack_id as i64)
*ack = Some(ack_id)
}
_ => {}
};
Expand Down Expand Up @@ -327,15 +327,6 @@ 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 @@ -345,35 +336,35 @@ impl<'a> TryInto<String> for Packet<'a> {
PacketData::Disconnect | PacketData::Connect(None) => (),
PacketData::Event(_, _, ack) => {
if let Some(ack) = ack {
insert_number(ack as usize, &mut res);
res.push_str(&ack.to_string());
}

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

push_nsp(&mut res);

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

res.push_str(&data.unwrap())
}
PacketData::BinaryAck(bin, ack) => {
insert_number(bin.payload_count, &mut res);
PacketData::BinaryAck(packet, ack) => {
res.push_str(&packet.payload_count.to_string());
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: 10 additions & 7 deletions socketioxide/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::{
borrow::Cow,
collections::HashMap,
fmt::Debug,
sync::{atomic::AtomicU32, Mutex},
sync::{atomic::Ordering, Arc, RwLock},
sync::Mutex,
sync::{
atomic::{AtomicI64, Ordering},
Arc, RwLock,
},
time::Duration,
};

Expand Down Expand Up @@ -100,8 +103,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<u32, oneshot::Sender<AckResponse<Value>>>>,
ack_counter: AtomicU32,
ack_message: Mutex<HashMap<i64, oneshot::Sender<AckResponse<Value>>>>,
ack_counter: AtomicI64,
pub id: Sid,

#[cfg(feature = "extensions")]
Expand All @@ -121,7 +124,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: AtomicU32::new(0),
ack_counter: AtomicI64::new(0),
id: sid,
#[cfg(feature = "extensions")]
extensions: Extensions::new(),
Expand Down Expand Up @@ -558,14 +561,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 as u32)) {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&ack) {
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 as u32)) {
if let Some(tx) = self.ack_message.lock().unwrap().remove(&ack) {
tx.send((packet.data, packet.bin)).ok();
}
Ok(())
Expand Down

0 comments on commit 53db020

Please sign in to comment.