use std::sync::atomic::{AtomicU64, Ordering}; use tokio::sync::mpsc; use crate::socket::packet::Packet; pub struct Socket { pub sid: String, pub namespace: String, pub engine_sid: String, ack_id: AtomicU64, tx: mpsc::Sender, } impl Socket { pub fn new( sid: String, namespace: String, engine_sid: String, tx: mpsc::Sender, ) -> Self { Self { sid, namespace, engine_sid, ack_id: AtomicU64::new(0), tx, } } pub fn next_ack_id(&self) -> u64 { self.ack_id.fetch_add(1, Ordering::SeqCst) } pub fn send_packet(&self, packet: &Packet) -> Result<(), mpsc::error::TrySendError> { self.tx.try_send(packet.clone()) } pub fn emit(&self, event: impl Into, data: serde_json::Value) -> Result<(), mpsc::error::TrySendError> { let packet = Packet::event( &self.namespace, serde_json::json!([event.into(), data]), None, ); self.send_packet(&packet) } pub fn emit_with_ack( &self, event: impl Into, data: serde_json::Value, ) -> Result> { let ack_id = self.next_ack_id(); let packet = Packet::event( &self.namespace, serde_json::json!([event.into(), data]), Some(ack_id), ); self.send_packet(&packet)?; Ok(ack_id) } pub fn disconnect(&self) -> Result<(), mpsc::error::TrySendError> { let packet = Packet::disconnect(&self.namespace); self.send_packet(&packet) } pub fn send_ack(&self, id: u64, data: serde_json::Value) -> Result<(), mpsc::error::TrySendError> { let packet = Packet::ack(&self.namespace, data, id); self.send_packet(&packet) } }