1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
use std::{
fmt::Debug,
ops::DerefMut,
pin::Pin,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use crate::{
ack::Ack,
callback::Callback,
error::Result,
packet::{AckIdGenerator, Packet, PacketType},
payload::RawPayload,
AckId, Error, Event, Payload,
};
use async_stream::try_stream;
use bytes::Bytes;
use dashmap::DashMap;
use engineio_rs::{
Packet as EnginePacket, PacketType as EnginePacketType, Socket as EngineSocket, StreamGenerator,
};
use futures_util::{future::BoxFuture, Stream, StreamExt};
use serde::Serialize;
use serde_json::{json, Value};
use tokio::{
sync::{Mutex, RwLock},
time::Instant,
};
use tracing::error;
use tracing::{trace, warn};
/// A socket which handles communication with the server. It's initialized with
/// a specific address as well as an optional namespace to connect to. If `None`
/// is given the client will connect to the default namespace `"/"`. Both client side
/// and server side, use this Client.
#[derive(Clone)]
pub struct Socket<C> {
// namespace, for multiplexing messages
pub(crate) nsp: String,
/// The inner socket client to delegate the methods to.
socket: RawSocket,
on: Arc<DashMap<Event, Callback<C>>>,
outstanding_acks: Arc<RwLock<Vec<Ack<C>>>>,
is_connected: Arc<AtomicBool>,
callback_client_fn: Arc<dyn Fn(Self) -> C + Send + Sync>,
ack_id_gen: Arc<AckIdGenerator>,
}
#[derive(Clone)]
pub(crate) struct RawSocket {
engine_client: Arc<EngineSocket>,
generator: Arc<Mutex<StreamGenerator<Packet, Error>>>,
is_server: bool,
}
#[derive(Serialize)]
struct BinaryPlaceHolder {
_placeholder: bool,
num: usize,
}
impl BinaryPlaceHolder {
fn new(num: usize) -> Self {
Self {
_placeholder: true,
num,
}
}
}
impl<C: Clone + Send + 'static> Socket<C> {
/// Creates a socket with a certain address to connect to as well as a
/// namespace. If `None` is passed in as namespace, the default namespace
/// `"/"` is taken.
/// ```
pub(crate) fn new<T: Into<String>>(
socket: RawSocket,
namespace: T,
on: Arc<DashMap<Event, Callback<C>>>,
callback_client_fn: Arc<dyn Fn(Self) -> C + Send + Sync>,
) -> Self {
Socket {
socket,
nsp: namespace.into(),
on,
outstanding_acks: Arc::new(RwLock::new(Vec::new())),
is_connected: Arc::new(AtomicBool::new(true)),
callback_client_fn,
ack_id_gen: Default::default(),
}
}
/// Connects the client to a server. Afterwards the `emit_*` methods can be
/// called to interact with the server.
#[cfg(feature = "client")]
pub(crate) async fn connect(&self) -> Result<()> {
// Connect the underlying socket
self.socket.connect().await?;
// construct the opening packet
let open_packet = Packet::new(PacketType::Connect, self.nsp.clone(), None, None, 0, None);
self.socket.send(open_packet).await?;
Ok(())
}
/// Sends a message to the server using the underlying `engine.io` protocol.
/// This message takes an event, which could either be one of the common
/// events like "message" or "error" or a custom event like "foo". But be
/// careful, the data string needs to be valid JSON. It's recommended to use
/// a library like `serde_json` to serialize the data properly.
///
/// # Example
/// ```no_run
/// use socketio_rs::{ClientBuilder, Socket, AckId, Payload};
/// use serde_json::json;
/// use futures_util::FutureExt;
///
/// #[tokio::main]
/// async fn main() {
/// let mut socket = ClientBuilder::new("http://localhost:4200/")
/// .on("test", |payload: Option<Payload>, socket: Socket, need_ack: Option<AckId>| {
/// async move {
/// println!("Received: {:?}", payload);
/// socket.emit("test", json!({"hello": true})).await.expect("Server unreachable");
/// }.boxed()
/// })
/// .connect()
/// .await
/// .expect("connection failed");
///
/// let json_payload = json!({"token": 123});
///
/// let result = socket.emit("foo", json_payload).await;
///
/// assert!(result.is_ok());
/// }
/// ```
#[inline]
pub async fn emit<E, D>(&self, event: E, data: D) -> Result<()>
where
E: Into<Event>,
D: Into<Payload>,
{
if !self.is_connected.load(Ordering::Acquire) {
return Err(Error::IllegalActionBeforeOpen());
}
self.socket.emit(&self.nsp, event.into(), data.into()).await
}
#[inline]
pub async fn ack<D>(&self, id: usize, data: D) -> Result<()>
where
D: Into<Payload>,
{
if !self.is_connected.load(Ordering::Acquire) {
return Err(Error::IllegalActionBeforeOpen());
}
self.socket.ack(&self.nsp, id, data.into()).await
}
#[cfg(feature = "server")]
#[inline]
pub(crate) async fn handshake(&self, data: Value) -> Result<()> {
if !self.is_connected.load(Ordering::Acquire) {
return Err(Error::IllegalActionBeforeOpen());
}
self.socket.handshake(&self.nsp, data).await
}
/// Disconnects this client from the server by sending a `socket.io` closing
/// packet.
/// # Example
/// ```no_run
/// use socketio_rs::{ClientBuilder, Socket, AckId, Payload};
/// use serde_json::json;
/// use futures_util::{FutureExt, future::BoxFuture};
///
/// #[tokio::main]
/// async fn main() {
/// // apparently the syntax for functions is a bit verbose as rust currently doesn't
/// // support an `AsyncFnMut` type that conform with async functions
/// fn handle_test(payload: Option<Payload>, socket: Socket, need_ack: Option<AckId>) -> BoxFuture<'static, ()> {
/// async move {
/// println!("Received: {:?}", payload);
/// socket.emit("test", json!({"hello": true})).await.expect("Server unreachable");
/// }.boxed()
/// }
///
/// let mut socket = ClientBuilder::new("http://localhost:4200/")
/// .on("test", handle_test)
/// .connect()
/// .await
/// .expect("connection failed");
///
/// let json_payload = json!({"token": 123});
///
/// socket.emit("foo", json_payload).await;
///
/// // disconnect from the server
/// socket.disconnect().await;
/// }
/// ```
pub async fn disconnect(&self) -> Result<()> {
if self.is_connected.load(Ordering::Acquire) {
self.is_connected.store(false, Ordering::Release);
}
let disconnect_packet = Packet::new(
PacketType::Disconnect,
self.nsp.clone(),
None,
None,
0,
None,
);
self.socket.send(disconnect_packet).await?;
self.socket.disconnect().await?;
Ok(())
}
/// Sends a message to the server but `alloc`s an `ack` to check whether the
/// server responded in a given time span. This message takes an event, which
/// could either be one of the common events like "message" or "error" or a
/// custom event like "foo", as well as a data parameter.
/// It also requires a timeout `Duration` in which the client needs to answer.
/// If the ack is acked in the correct time span, the specified callback is
/// called. The callback consumes a [`Payload`] which represents the data send
/// by the server.
///
/// Please note that the requirements on the provided callbacks are similar to the ones
/// for [`crate::asynchronous::ClientBuilder::on`].
/// # Example
/// ```no_run
/// use socketio_rs::{ClientBuilder, Socket, Payload};
/// use serde_json::json;
/// use std::time::Duration;
/// use std::thread::sleep;
/// use futures_util::FutureExt;
///
/// #[tokio::main]
/// async fn main() {
/// let mut socket = ClientBuilder::new("http://localhost:4200/")
/// .on("foo", |payload: Option<Payload>, _, _| async move { println!("Received: {:#?}", payload) }.boxed())
/// .connect()
/// .await
/// .expect("connection failed");
///
/// let ack_callback = |message: Option<Payload>, socket: Socket, _| {
/// async move {
/// println!("{:?}", message);
/// }.boxed()
/// };
///
///
/// let payload = json!({"token": 123});
/// socket.emit_with_ack("foo", payload, Duration::from_secs(2), ack_callback).await.unwrap();
///
/// sleep(Duration::from_secs(2));
/// }
/// ```
#[inline]
pub async fn emit_with_ack<F, E, D>(
&self,
event: E,
data: D,
timeout: Duration,
callback: F,
) -> Result<()>
where
F: for<'a> std::ops::FnMut(Option<Payload>, C, Option<AckId>) -> BoxFuture<'static, ()>
+ 'static
+ Send
+ Sync,
E: Into<Event>,
D: Into<Payload>,
{
if !self.is_connected.load(Ordering::Acquire) {
return Err(Error::IllegalActionBeforeOpen());
}
let id = self.ack_id_gen.generate();
let packet = RawSocket::build_packet_for_payload(
data.into(),
Some(event.into()),
&self.nsp,
Some(id),
false,
)?;
let ack = Ack {
id,
time_started: Instant::now(),
timeout,
callback: Callback::new(callback),
};
// add the ack to the tuple of outstanding acks
self.outstanding_acks.write().await.push(ack);
trace!("socket emit_with_ack {:?}", packet);
self.socket.send(packet).await
}
async fn callback(&self, event: &Event, payload: Option<Payload>, need_ack: Option<AckId>) {
let self_clone = self.clone();
let event = event.to_owned();
tokio::spawn(async move {
if let Some(mut callback) = self_clone.on.get_mut(&event) {
let c = (self_clone.callback_client_fn)((self_clone).clone());
trace!("do callback {:?}", event);
callback(payload, c, need_ack).await;
trace!("done callback {:?}", event);
}
});
}
/// Handles the incoming acks and classifies what callbacks to call and how.
#[inline]
async fn handle_ack(&self, packet: &Packet, is_binary: bool) -> Result<()> {
//TODO: fix multi
let mut to_be_removed = Vec::new();
if let Some(id) = packet.id {
for (index, ack) in self.outstanding_acks.write().await.iter_mut().enumerate() {
if ack.id == id {
to_be_removed.push(index);
if ack.time_started.elapsed() < ack.timeout {
trace!("ack packet {:?}", packet);
let payload = if is_binary {
Self::decode_binary_payload(&packet.data, &packet.attachments, false)
} else {
Self::decode_event_payload(packet, false)
};
trace!("decode ack payload {:?}", payload);
ack.callback.deref_mut()(
payload,
(self.callback_client_fn)(self.clone()),
None,
)
.await;
} else {
trace!("Received an Ack that is now timed out (elapsed time was longer than specified duration)");
}
}
}
for index in to_be_removed {
self.outstanding_acks.write().await.remove(index);
}
}
Ok(())
}
/// Handles a binary event.
#[inline]
async fn handle_binary_event(&self, packet: &Packet) -> Result<()> {
trace!("handle_binary_event {:?}", packet);
let event = match &packet.data {
Some(Value::String(e)) => Event::from(e.to_owned()),
Some(Value::Array(array)) => match array.first() {
Some(Value::String(e)) => Event::from(e.to_owned()),
_ => Event::Message,
},
_ => Event::Message,
};
let payload = Self::decode_binary_payload(&packet.data, &packet.attachments, true);
self.callback(&event, payload, packet.id).await;
Ok(())
}
fn decode_binary_payload(
data: &Option<Value>,
attachments: &Option<Vec<Bytes>>,
skip_event: bool,
) -> Option<Payload> {
match &data {
Some(Value::Array(vec)) => match Self::decode_binary(vec, attachments, skip_event) {
Ok(payload) => Some(payload),
Err(e) => {
error!("decode binary event: {}", e.to_string());
None
}
},
_ => None,
}
}
fn decode_binary(
vec: &Vec<Value>,
attachments: &Option<Vec<Bytes>>,
skip_event: bool,
) -> Result<Payload> {
trace!(
"do_decode_binary_payload vec {:?}, attachments {:?}",
vec,
attachments
);
let mut vec_payload = vec![];
for (index, value) in vec.iter().enumerate() {
if skip_event && index == 0 {
continue;
}
trace!("do_decode_binary_payload {:?}", value);
if value.get("_placeholder").is_some() {
let index = value
.get("num")
.ok_or(Error::InvalidPacket())?
.as_u64()
.ok_or(Error::InvalidPacket())? as usize;
if let Some(atts) = attachments {
let b = atts.get(index).ok_or(Error::InvalidPacket())?.to_owned();
vec_payload.push(RawPayload::Binary(b));
} else {
return Err(Error::InvalidPacket());
}
} else {
vec_payload.push(RawPayload::Json(value.to_owned()))
}
}
if vec_payload.len() == 1 {
// SAFETY: len checked before
let payload = vec_payload.pop().unwrap();
return Ok(payload.into());
}
Ok(Payload::Multi(vec_payload))
}
/// A method for handling the Event Client Packets.
// this could only be called with an event
async fn handle_event(&self, packet: &Packet) -> Result<()> {
// the string must be a valid json array with the event at index 0 and the
// payload at index 1. if no event is specified, the message callback is used
if let Some(serde_json::Value::Array(contents)) = &packet.data {
let event: Event = if contents.len() > 1 {
contents
.first()
.map(|value| match value {
serde_json::Value::String(ev) => ev,
_ => "message",
})
.unwrap_or("message")
.into()
} else {
Event::Message
};
let payload = Self::decode_event_payload(packet, true);
self.callback(&event, payload, packet.id).await;
} else {
warn!("handle_event invalid packet data {:?}", packet.data);
}
Ok(())
}
fn decode_event_payload(packet: &Packet, skip_event: bool) -> Option<Payload> {
match packet.data {
Some(serde_json::Value::Array(ref contents)) if contents.is_empty() => None,
Some(serde_json::Value::Array(ref contents)) if contents.len() == 1 => {
// SAFETY: len checked before
Some(Payload::Json(contents.get(0).unwrap().clone()))
}
Some(serde_json::Value::Array(ref contents)) if contents.len() == 2 => {
if skip_event {
// SAFETY: len checked before
Some(Payload::Json(contents.get(1).unwrap().clone()))
} else {
Some(Payload::Multi(
contents.iter().cloned().map(RawPayload::from).collect(),
))
}
}
Some(serde_json::Value::Array(ref contents)) => {
let payload = if skip_event {
Payload::Multi(
contents
.iter()
.skip(1)
.cloned()
.map(RawPayload::from)
.collect(),
)
} else {
Payload::Multi(contents.iter().cloned().map(RawPayload::from).collect())
};
Some(payload)
}
_ => None,
}
}
pub(crate) async fn handle_connect(&self, packet: Option<&Packet>) -> Result<()> {
self.is_connected.store(true, Ordering::Release);
trace!("callback connect {:?}", packet);
let payload = packet.map(|p| p.data.clone().into());
self.callback(&Event::Connect, payload, None).await;
Ok(())
}
/// Handles the incoming messages and classifies what callbacks to call and how.
/// This method is later registered as the callback for the `on_data` event of the
/// engineio client.
#[inline]
async fn handle_socketio_packet(&self, packet: &Packet) -> Result<()> {
trace!("handle_socketio_packet {:?}", packet);
if packet.nsp == self.nsp {
match packet.ptype {
PacketType::Ack => {
if let Err(err) = self.handle_ack(packet, false).await {
self.callback(&Event::Error, None, None).await;
return Err(err);
}
}
PacketType::BinaryAck => {
if let Err(err) = self.handle_ack(packet, true).await {
self.callback(&Event::Error, None, None).await;
return Err(err);
}
}
PacketType::Event => {
if let Err(err) = self.handle_event(packet).await {
self.callback(&Event::Error, Some(json!(err.to_string()).into()), None)
.await;
}
}
PacketType::BinaryEvent => {
if let Err(err) = self.handle_binary_event(packet).await {
self.callback(&Event::Error, Some(json!(err.to_string()).into()), None)
.await;
}
}
PacketType::Connect => self.handle_connect(Some(packet)).await?,
PacketType::Disconnect => {
self.is_connected.store(false, Ordering::Release);
self.callback(&Event::Close, None, None).await;
}
PacketType::ConnectError => {
self.is_connected.store(false, Ordering::Release);
self.callback(
&Event::Error,
Some(
json!(format!("Received an ConnectError frame: {:?}", packet.data))
.into(),
),
None,
)
.await;
}
}
}
Ok(())
}
pub(crate) async fn poll_packet(&self) -> Option<Result<Packet>> {
loop {
// poll for the next payload
let next = self.socket.poll_packet().await;
match next {
None => {
// end the stream if the underlying one is closed
return None;
}
Some(Err(err)) => {
// call the error callback
self.callback(&Event::Error, Some(json!(err.to_string()).into()), None)
.await;
return Some(Err(err));
}
Some(Ok(packet)) => {
// if this packet is not meant for the current namespace, skip it an poll for the next one
if packet.nsp == self.nsp {
let _ = self.handle_socketio_packet(&packet).await;
return Some(Ok(packet));
}
}
}
}
}
}
impl RawSocket {
/// Creates an instance of `Socket`.
#[cfg(feature = "client")]
pub(super) fn client_end(engine_client: EngineSocket) -> Self {
RawSocket {
engine_client: Arc::new(engine_client.clone()),
generator: Arc::new(Mutex::new(StreamGenerator::new(Self::stream(
engine_client,
)))),
is_server: false,
}
}
#[cfg(feature = "server")]
pub(super) fn server_end(engine_client: EngineSocket) -> Self {
RawSocket {
engine_client: Arc::new(engine_client.clone()),
generator: Arc::new(Mutex::new(StreamGenerator::new(Self::stream(
engine_client,
)))),
is_server: true,
}
}
/// Connects to the server. This includes a connection of the underlying
/// engine.io client and afterwards an opening socket.io request.
#[cfg(feature = "client")]
pub async fn connect(&self) -> Result<()> {
if !self.is_server {
self.engine_client.connect().await?;
}
Ok(())
}
/// Disconnects from the server by sending a socket.io `Disconnect` packet. This results
/// in the underlying engine.io transport to get closed as well.
pub async fn disconnect(&self) -> Result<()> {
if self.is_engineio_connected() {
self.engine_client.disconnect().await?;
}
Ok(())
}
/// Sends a `socket.io` packet to the server using the `engine.io` client.
pub async fn send(&self, packet: Packet) -> Result<()> {
if !self.is_engineio_connected() {
trace!("socket emit before open {:?}", packet);
return Err(Error::IllegalActionBeforeOpen());
}
// the packet, encoded as an engine.io message packet
let engine_packet = EnginePacket::new(EnginePacketType::Message, Bytes::from(&packet));
match packet.attachments {
None => {
self.engine_client.emit(engine_packet).await?;
}
Some(attachments) if attachments.is_empty() => {
self.engine_client.emit(engine_packet).await?;
}
Some(attachments) => {
let mut packets = vec![engine_packet];
for attachment in attachments {
let engine_packet =
EnginePacket::new(EnginePacketType::MessageBinary, attachment);
packets.push(engine_packet);
}
// atomic send attachments
self.engine_client.emit_multi(packets).await?;
}
}
Ok(())
}
pub async fn ack(&self, nsp: &str, id: usize, data: Payload) -> Result<()> {
let packet = RawSocket::build_packet_for_payload(data, None, nsp, Some(id), true)?;
trace!("socket ack {:?}", packet);
self.send(packet).await
}
/// Emits to certain event with given data. The data needs to be JSON,
/// otherwise this returns an `InvalidJson` error.
pub async fn emit(&self, nsp: &str, event: Event, data: Payload) -> Result<()> {
let packet = RawSocket::build_packet_for_payload(data, Some(event), nsp, None, false)?;
self.send(packet).await
}
#[cfg(feature = "server")]
pub(crate) async fn handshake(&self, nsp: &str, data: Value) -> Result<()> {
let packet = Packet::new(
PacketType::Connect,
nsp.to_owned(),
Some(data),
None,
0,
None,
);
self.send(packet).await
}
/// Returns a packet for a payload, could be used for bot binary and non binary
/// events and acks. Convenance method.
#[inline]
pub(crate) fn build_packet_for_payload(
payload: Payload,
event: Option<Event>,
nsp: &str,
id: Option<usize>,
is_ack: bool,
) -> Result<Packet> {
let (data, attachments) = Self::encode_data(event, payload);
let packet_type = match attachments.is_empty() {
true if is_ack => PacketType::Ack,
true => PacketType::Event,
false if is_ack => PacketType::BinaryAck,
false => PacketType::BinaryEvent,
};
Ok(Packet::new(
packet_type,
nsp.to_owned(),
Some(data),
id,
attachments.len() as u8,
Some(attachments),
))
}
fn encode_data(event: Option<Event>, payload: Payload) -> (Value, Vec<Bytes>) {
let mut attachments = vec![];
let mut data: Vec<Value> = vec![];
if let Some(event) = event {
data.push(json!(String::from(event)));
}
Self::process_payload(&mut data, payload, &mut attachments);
let data = Value::Array(data);
(data, attachments)
}
fn process_payload(data: &mut Vec<Value>, payload: Payload, attachments: &mut Vec<Bytes>) {
match payload {
Payload::Binary(bin_data) => Self::process_binary(data, bin_data, attachments),
Payload::Json(value) => data.push(value),
Payload::Multi(payloads) => {
for payload in payloads {
match payload {
RawPayload::Binary(bin) => Self::process_binary(data, bin, attachments),
RawPayload::Json(value) => data.push(value),
}
}
}
};
}
fn process_binary(data: &mut Vec<Value>, bin_data: Bytes, attachments: &mut Vec<Bytes>) {
let place_holder = BinaryPlaceHolder::new(attachments.len());
data.push(json!(&place_holder));
attachments.push(bin_data);
}
pub(crate) async fn poll_packet(&self) -> Option<Result<Packet>> {
let mut generator = self.generator.lock().await;
generator.next().await
}
fn stream(client: EngineSocket) -> Pin<Box<impl Stream<Item = Result<Packet>> + Send>> {
Box::pin(try_stream! {
for await received_data in client.clone() {
let packet = received_data?;
if packet.ptype == EnginePacketType::Message || packet.ptype == EnginePacketType::MessageBinary {
let packet = Self::handle_engineio_packet(packet, client.clone()).await?;
yield packet;
}
}
})
}
/// Handles new incoming engineio packets
async fn handle_engineio_packet(
packet: EnginePacket,
mut client: EngineSocket,
) -> Result<Packet> {
let mut packet = Packet::try_from(&packet.data)?;
// Only handle attachments if there are any
if packet.attachment_count > 0 {
let mut attachments_left = packet.attachment_count;
let mut attachments = Vec::new();
while attachments_left > 0 {
// TODO: This is not nice! Find a different way to peek the next element while mapping the stream
let next = client.next().await.unwrap();
match next {
Err(err) => return Err(err.into()),
Ok(packet) => match packet.ptype {
EnginePacketType::MessageBinary | EnginePacketType::Message => {
attachments.push(packet.data);
attachments_left -= 1;
}
_ => {
return Err(Error::InvalidAttachmentPacketType(packet.ptype.into()));
}
},
}
}
packet.attachments = Some(attachments);
}
Ok(packet)
}
fn is_engineio_connected(&self) -> bool {
self.engine_client.is_connected()
}
}
impl Debug for RawSocket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Socket")
.field("engine_client", &self.engine_client)
.field("is_server", &self.is_server)
.field("connected", &self.is_engineio_connected())
.finish()
}
}