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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
//! The application's handle on a connection.
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use serde::Serialize;
use tokio::sync::{mpsc, oneshot, watch};
#[cfg(target_arch = "wasm32")]
use crate::browser::Driver;
use crate::config::Config;
#[cfg(not(target_arch = "wasm32"))]
use crate::connection::Driver;
use crate::delivery::{self, Gate, TopicState};
use crate::error::Error;
use crate::handler::{Event, Handler, StorageFailed};
use crate::message::Message;
use crate::outbox::{Outbox, Room};
use crate::security::Keys;
/// Where the client stands.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Status {
/// Making the first connection.
Connecting,
/// The server took the connection.
Connected,
/// The connection was lost; making it again.
Reconnecting,
/// Paused by the application: no connection until it resumes.
Paused,
/// Stopped for good: closed by the application (`None`), or the reason.
Closed(Option<Error>),
}
/// What the application asks of the connection, besides its messages.
pub(crate) enum Control {
Subscribe(Vec<String>),
Unsubscribe(Vec<String>),
Reconnect,
Pause(String),
Resume(String),
/// The delivery's gate made something for the handler (held messages
/// handed on): it goes to the handler now.
Drain,
/// The user signed out: the outbox is emptied.
Clear,
Close,
}
/// Where what the delivery's gate makes outside the messages goes to the
/// application.
#[derive(Clone)]
pub(crate) enum Wake {
/// The handler, from the connection's task.
Session,
/// The [`Events`](crate::Events) stream, at its next read.
Reader(mpsc::UnboundedSender<()>),
}
/// A message the application sent, on its way to the connection's task.
pub(crate) struct Queued {
pub(crate) text: String,
/// The user it was sent as ([`Shared::users`]): it goes nowhere once the
/// server took a connection for another user.
pub(crate) user: u64,
/// A signed or live message goes on the connection it was sent on
/// ([`Shared::connections`]), or not at all.
pub(crate) only_on: Option<u64>,
/// Topics that must each be continuous when it goes: until then it
/// waits, in order with the others that wait.
pub(crate) requires: Option<Box<[String]>>,
/// Its id: the application's own, or a random one; what its outcome
/// names.
pub(crate) id: Box<str>,
/// Until when it may wait before it is first sent, in milliseconds
/// since the Unix epoch, from its send on (waiting for its topics
/// included); None: the outbox's `max_age`.
pub(crate) until: Option<u64>,
/// It took a place in the outbox's room, given back when it goes.
pub(crate) counted: bool,
/// The outbox's clears before it ([`Shared::clears`]): it goes nowhere
/// once the application cleared the outbox after it.
pub(crate) clears: u64,
/// The bridge owner's window that sent it ([`crate::outbox::Tags`]): 0
/// for the application's own.
pub(crate) tag: u64,
}
impl Queued {
/// Its size as the application sent it: its place in the outbox's room
/// (the room for its number, before it, not counted).
pub(crate) fn size(&self) -> usize {
if self.counted {
self.text.len().saturating_sub(crate::outbox::HEADROOM)
} else {
self.text.len()
}
}
}
/// The user and connection a message was sent for.
struct SentFor {
user: u64,
/// What a signed or live message goes on.
connection: u64,
/// The server took the current connection.
ready: bool,
clears: u64,
}
impl SentFor {
fn frame(&self, id: Box<str>, text: String) -> Queued {
Queued {
text,
user: self.user,
only_on: None,
requires: None,
id,
until: None,
counted: false,
clears: self.clears,
tag: 0,
}
}
/// For this connection only.
fn live(&self, text: String) -> Queued {
Queued {
only_on: Some(self.connection),
..self.frame(Box::default(), text)
}
}
}
/// How a message goes ([`Client::send_with`]).
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct SendOptions {
/// Its id: the application's own (one it keeps in its own outbox, say),
/// or a random UUID. The outcome events name it.
pub id: Option<String>,
/// How long it may wait before it is first sent: past it, it is dropped
/// and the application told ([`Event::NotDelivered`](crate::Event),
/// `expired`). Once written it waits for the server's answer whatever
/// its age. None: the outbox's `max_age`.
pub ttl: Option<std::time::Duration>,
/// Topics that must each be continuous when it goes (see
/// [`Client::send_requiring`]).
pub requires: Vec<String>,
/// The id the application's reply names (`cid`): its request and its
/// response.
pub correlation_id: Option<String>,
/// How urgent the application says it is (`pri`; the other clients
/// name 10 critical, 8 high, 5 normal, 3 low, 1 background). It is for
/// the server's application: the outbox keeps the order messages were
/// sent in.
pub priority: Option<u8>,
/// On the connection that is ready now, or not at all, as
/// [`Client::try_send_live`]: never kept in the outbox. It waits for
/// nothing, so it takes no `ttl` and requires no topics.
pub live: bool,
}
/// What a message carries besides its type, payload and id.
#[derive(Clone, Copy, Default)]
struct Extra<'a> {
cid: Option<&'a str>,
pri: Option<u8>,
}
impl<'a> Extra<'a> {
fn of(options: &'a SendOptions) -> Self {
Self {
cid: options.correlation_id.as_deref(),
pri: options.priority,
}
}
}
/// A message as it goes: `{"t", "p", "id"}`, and its `cid` and `pri` when
/// it has them.
#[derive(Serialize)]
struct Out<'a, P: ?Sized> {
t: &'a str,
p: &'a P,
id: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
cid: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pri: Option<u8>,
}
/// What the handles and the connection's task share.
pub(crate) struct Shared {
/// The server took the current connection: messages may go.
pub(crate) ready: AtomicBool,
/// Counts the times the server took a connection for another user than
/// the one before.
pub(crate) users: AtomicU64,
/// Counts the connections the server took.
pub(crate) connections: AtomicU64,
/// The current connection's id and user.
pub(crate) connection: Mutex<Option<(String, Option<String>)>>,
/// The token for the next connection attempt.
pub(crate) token: Mutex<Option<String>>,
/// The server's signing key: what signed messages are for.
pub(crate) server_kid: Mutex<Option<String>>,
/// Stops the connection's task where it stands: a close past its
/// deadline.
stop: Mutex<Option<futures_util::future::AbortHandle>>,
/// The outbox's room ([`Config::offline`]): a send takes a place in it
/// before it is queued.
pub(crate) room: Option<Arc<Room>>,
/// Counts the times the application cleared the outbox.
pub(crate) clears: AtomicU64,
/// A bridge owner of this crate: its windows' sends' outcomes, and
/// whether a window holds one back.
pub(crate) tags: Option<Arc<dyn crate::outbox::Tags>>,
}
impl Shared {
pub(crate) fn new(token: Option<String>, room: Option<Arc<Room>>) -> Self {
Self {
ready: AtomicBool::new(false),
users: AtomicU64::new(0),
connections: AtomicU64::new(0),
connection: Mutex::new(None),
token: Mutex::new(token),
server_kid: Mutex::new(None),
stop: Mutex::new(None),
room,
clears: AtomicU64::new(0),
tags: None,
}
}
}
/// A connection to a WSE server, made again when it is lost.
///
/// Clones share the connection, which lives as long as the application
/// holds a handle (or until [`close`](Self::close)); the client a handler
/// is given by [`connect_with`](Self::connect_with) does not keep it.
/// Sends go out in the order they were made; what the server sends goes to
/// the [`Handler`] the client was made with, or to the
/// [`Events`](crate::Events) of [`connect`](crate::connect).
///
/// A message still queued when the connection is lost goes out on the next
/// one, unless the server takes that one for another user: then it goes
/// nowhere, and the topics start over. A signed message goes out on the
/// connection it was signed on, or not at all.
#[derive(Clone)]
pub struct Client {
shared: Arc<Shared>,
#[cfg_attr(not(feature = "aws-lc"), allow(dead_code))]
keys: Arc<Keys>,
frames: mpsc::Sender<Queued>,
control: mpsc::UnboundedSender<Control>,
status: watch::Receiver<Status>,
/// The delivery: the topics' states, what was held and committed.
gate: delivery::Shared,
wake: Wake,
/// The application's hold on the connection; none for a handler's.
_owner: Option<Arc<Owner>>,
}
/// The application's handles hold one of these: when the last goes, the
/// connection is closed.
struct Owner(mpsc::UnboundedSender<Control>);
impl Drop for Owner {
fn drop(&mut self) {
let _ = self.0.send(Control::Close);
}
}
impl Client {
/// Connects, and returns once the server took the connection. What it
/// sends goes to `handler`.
///
/// The first connection is not made again when it fails: the error is
/// returned. Later ones are, as [`Config::reconnect`] says.
pub async fn connect<H: Handler>(config: Config, handler: H) -> Result<Self, Error> {
Self::connect_with(config, |_| handler).await
}
/// [`connect`](Self::connect) with a handler that is given the client,
/// to send from it.
pub async fn connect_with<H, F>(config: Config, make: F) -> Result<Self, Error>
where
H: Handler,
F: FnOnce(Client) -> H,
{
Self::launch(config, make, None, true)
.await
.map(|(client, _)| client)
}
/// Starts, and returns at once: the connection is made in the
/// background, a failed first attempt made again as any later one
/// ([`Config::reconnect`]). What is sent meanwhile waits in the outbox.
/// For an application that may start without a network; a client that
/// gives up says so in [`Client::closed`].
pub async fn start<H: Handler>(config: Config, handler: H) -> Result<Self, Error> {
Self::start_with(config, |_| handler).await
}
/// [`start`](Self::start) with a handler that is given the client, to
/// send from it.
pub async fn start_with<H, F>(config: Config, make: F) -> Result<Self, Error>
where
H: Handler,
F: FnOnce(Client) -> H,
{
Self::launch(config, make, None, false)
.await
.map(|(client, _)| client)
}
/// Starts the client, and returns once the server took the first
/// connection (`wait`) or at once. The delivery's gate stands before the
/// handler, or, with `reader` (the [`Events`](crate::Events) stream's
/// wake), where the stream is read: it is returned for it.
pub(crate) async fn launch<H, F>(
mut config: Config,
make: F,
reader: Option<mpsc::UnboundedSender<()>>,
wait: bool,
) -> Result<(Self, delivery::Shared), Error>
where
H: Handler,
F: FnOnce(Client) -> H,
{
config.check()?;
let gate = Gate::new(config.delivery.clone(), &config.url, &config.topics)?;
let gate = Arc::new(std::sync::Mutex::new(gate));
// Recovery starts from what the application committed.
let committed = Arc::clone(&gate);
config.committed = Some(Arc::new(move |user| {
delivery::lock(&committed).committed(user)
}));
let before_handler = reader.is_none();
let (control_tx, control_rx) = mpsc::unbounded_channel();
let (room, outbox) = match &config.offline {
Some(offline) => {
let room = Arc::new(Room::new(offline));
let report = storage_report(&gate, &control_tx, reader.clone());
let outbox =
Outbox::open(offline, Arc::clone(&room), report, outbox_wake(&control_tx));
(Some(room), Some(outbox))
}
None => (None, None),
};
let wake = match reader {
Some(reader) => Wake::Reader(reader),
None => Wake::Session,
};
let keys = Arc::new(Keys::from_config(&config)?);
// Read into the keys: the bytes of this copy go.
#[cfg(feature = "aws-lc")]
if let Some(mut key) = config.signing_key.take() {
zeroize::Zeroize::zeroize(&mut key);
}
let mut shared = Shared::new(config.token.clone(), room);
shared.tags = config.tags.take();
let shared = Arc::new(shared);
let (frames_tx, frames_rx) = mpsc::channel(config.send_queue);
let (status_tx, status_rx) = watch::channel(Status::Connecting);
let for_handler = Client {
shared: Arc::clone(&shared),
keys: Arc::clone(&keys),
frames: frames_tx,
control: control_tx.clone(),
status: status_rx,
gate: Arc::clone(&gate),
wake,
_owner: None,
};
let client = Client {
_owner: Some(Arc::new(Owner(control_tx))),
..for_handler.clone()
};
let handler = make(for_handler);
// Connect waits for the first connection's answer; start retries it.
let (first_tx, first_rx) = oneshot::channel();
let first = wait.then_some(first_tx);
let ended = (Arc::clone(&shared), status_tx.clone());
let (stop, stopped) = futures_util::future::AbortHandle::new_pair();
if let Ok(mut slot) = shared.stop.lock() {
*slot = Some(stop);
}
let handler = crate::delivery::Delivered::new(handler, Arc::clone(&gate), before_handler);
let driver = Driver::new(
config, handler, shared, keys, frames_rx, control_rx, status_tx,
)
.with_outbox(outbox);
let run = async move {
use futures_util::FutureExt;
let driven = futures_util::future::Abortable::new(
std::panic::AssertUnwindSafe(driver.run(first)).catch_unwind(),
stopped,
)
.await;
let (shared, status) = ended;
// A send that waits for a place in the outbox waits no longer,
// however the client ended (a handler that panicked, a close
// past its deadline).
if let Some(room) = &shared.room {
room.close();
}
match driven {
Ok(Ok(())) => {}
// A handler that panics ends the client, which says so.
Ok(Err(_)) => {
shared.ready.store(false, Ordering::Release);
status.send_replace(Status::Closed(Some(Error::Panicked)));
}
// Stopped by a close past its deadline: the connection went
// with the task.
Err(_) => {
shared.ready.store(false, Ordering::Release);
status.send_replace(Status::Closed(None));
}
}
};
#[cfg(not(target_arch = "wasm32"))]
tokio::spawn(run);
#[cfg(target_arch = "wasm32")]
wasm_bindgen_futures::spawn_local(run);
if !wait {
return Ok((client, gate));
}
match first_rx.await {
Ok(Ok(())) => Ok((client, gate)),
Ok(Err(error)) => Err(error),
Err(_) => Err(Error::Closed),
}
}
/// Sends a message `{"t": kind, "p": payload}`; fails at once when the
/// outbox or the queue is full.
pub fn try_send<P: Serialize + ?Sized>(&self, kind: &str, payload: &P) -> Result<(), Error> {
let frame = self.outgoing(kind, payload, &SendOptions::default())?;
self.try_put(frame)
}
/// Sends a message `{"t": kind, "p": payload}`, waiting for room in the
/// queue. It waits in the outbox ([`Config::offline`], in memory by
/// default) until the server answers for it, and goes again after a
/// reconnect; with no outbox, a send with no connection fails with
/// [`Error::NotConnected`].
pub async fn send<P: Serialize + ?Sized>(&self, kind: &str, payload: &P) -> Result<(), Error> {
let frame = self.outgoing(kind, payload, &SendOptions::default())?;
self.put(frame).await
}
/// [`Client::send`] with its `options`: its own id, how long it may
/// wait, the topics it requires, its correlation id and priority, or
/// live. Returns its id, which its outcome names
/// ([`Event::Delivered`](crate::Event::Delivered),
/// [`Event::NotDelivered`](crate::Event::NotDelivered)).
pub async fn send_with<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
options: &SendOptions,
) -> Result<String, Error> {
if options.live {
let (id, frame) = self.live_frame(kind, payload, options)?;
self.frames.send(frame).await.map_err(|_| Error::Closed)?;
return Ok(id);
}
let frame = self.outgoing(kind, payload, options)?;
let id = frame.id.to_string();
self.put(frame).await?;
Ok(id)
}
/// [`Client::send_with`], failing at once when the outbox or the queue
/// is full.
pub fn try_send_with<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
options: &SendOptions,
) -> Result<String, Error> {
if options.live {
let (id, frame) = self.live_frame(kind, payload, options)?;
self.frames.try_send(frame).map_err(|e| match e {
mpsc::error::TrySendError::Full(_) => Error::Full,
mpsc::error::TrySendError::Closed(_) => Error::Closed,
})?;
return Ok(id);
}
let frame = self.outgoing(kind, payload, options)?;
let id = frame.id.to_string();
self.try_put(frame)?;
Ok(id)
}
/// [`Client::try_send_with`] for window `tag` of a bridge owner: the
/// message carries it in the outbox, and what became of it goes to the
/// owner ([`crate::outbox::Tags`]) with it, not to the handler.
#[cfg(all(feature = "bridge", not(target_arch = "wasm32")))]
pub(crate) fn try_send_tagged<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
options: &SendOptions,
tag: u64,
) -> Result<String, Error> {
let mut frame = self.outgoing(kind, payload, options)?;
frame.tag = tag;
let id = frame.id.to_string();
self.try_put(frame)?;
Ok(id)
}
/// Sends a message `{"t": kind, "p": payload}` on the connection that is
/// ready now, or not at all: one this connection did not write is
/// dropped, never sent on a later connection, and it never waits in the
/// outbox. For commands that must not run late (a valve, a door, an
/// order). Fails at once when the queue is full.
pub fn try_send_live<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
) -> Result<(), Error> {
let for_now = self.ready()?;
let text = encode(kind, payload, &message_id(), Extra::default())?;
self.frames
.try_send(for_now.live(text))
.map_err(|e| match e {
mpsc::error::TrySendError::Full(_) => Error::Full,
mpsc::error::TrySendError::Closed(_) => Error::Closed,
})
}
/// [`Client::try_send_live`], waiting for room in the queue.
pub async fn send_live<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
) -> Result<(), Error> {
let for_now = self.ready()?;
let text = encode(kind, payload, &message_id(), Extra::default())?;
self.frames
.send(for_now.live(text))
.await
.map_err(|_| Error::Closed)
}
/// Sends a message `{"t": kind, "p": payload}` once each of `topics` is
/// continuous on the connection (the delivery's
/// [`TopicState`](crate::TopicState)): until then it waits, in order with
/// the others that wait, while sends that require nothing go past it.
/// It is checked as it is taken to go out, so one waiting does not go
/// after a reconnect before the resync; it is the user's who made it, and
/// goes nowhere once the server takes a connection for another. Waits
/// for room in the queue.
pub async fn send_requiring<P, I, T>(
&self,
kind: &str,
payload: &P,
topics: I,
) -> Result<(), Error>
where
P: Serialize + ?Sized,
I: IntoIterator<Item = T>,
T: Into<String>,
{
let options = SendOptions {
requires: topics.into_iter().map(Into::into).collect(),
..SendOptions::default()
};
self.send_with(kind, payload, &options).await.map(drop)
}
/// [`Client::send_requiring`], failing at once when the outbox or the
/// queue is full.
pub fn try_send_requiring<P, I, T>(
&self,
kind: &str,
payload: &P,
topics: I,
) -> Result<(), Error>
where
P: Serialize + ?Sized,
I: IntoIterator<Item = T>,
T: Into<String>,
{
let options = SendOptions {
requires: topics.into_iter().map(Into::into).collect(),
..SendOptions::default()
};
self.try_send_with(kind, payload, &options).map(drop)
}
/// Sends JSON text as it is: a message the application built itself, a
/// JSON object. It waits in the outbox as [`Client::send`] does.
pub async fn send_raw(&self, json: impl Into<String>) -> Result<(), Error> {
let json = json.into();
let body = json.trim_start();
if !body.starts_with('{') {
return Err(Error::Encode("a message is a JSON object".to_owned()));
}
// Whole: the outbox keeps it, and its file holds it as it is.
serde_json::from_str::<serde::de::IgnoredAny>(body)
.map_err(|e| Error::Encode(format!("not JSON: {e}")))?;
let text = if self.shared.room.is_some() {
let mut text = String::with_capacity(crate::outbox::HEADROOM + body.len());
text.extend(std::iter::repeat_n(' ', crate::outbox::HEADROOM));
text.push_str(body);
text
} else {
json
};
let frame = self.queued(message_id().into_boxed_str(), text)?;
self.put(frame).await
}
/// Sends a signed message `{"t": kind, "p": payload}`: signed with
/// [`Config::signing_key`] for the server's signing key, so whoever
/// keeps it can show who sent it. `n` names it once, 1 to 128
/// characters (an order id, say; a random one when `None`). Returns the
/// `n` it went with: the server's signed `receipt`, or its error
/// `SIGNATURE_REFUSED`, names it.
#[cfg(feature = "aws-lc")]
pub async fn send_signed<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
n: Option<&str>,
) -> Result<String, Error> {
let for_now = self.ready()?;
let audience = self
.shared
.server_kid
.lock()
.ok()
.and_then(|kid| kid.clone())
.ok_or_else(|| Error::Signing("the server named no signing key".to_owned()))?;
let payload =
serde_json::value::to_raw_value(payload).map_err(|e| Error::Encode(e.to_string()))?;
let n = match n {
Some(n)
if n.is_empty() || n.chars().count() > wse::protocol::signing::MAX_NONCE_LEN =>
{
return Err(Error::Signing(format!(
"n must be 1 to {} characters",
wse::protocol::signing::MAX_NONCE_LEN
)));
}
Some(n) => n.to_owned(),
None => {
let mut bytes = [0u8; 16];
wse::protocol::crypto::fill_random(&mut bytes)
.map_err(|_| Error::Signing("no random bytes".to_owned()))?;
bytes.iter().map(|b| format!("{b:02x}")).collect::<String>()
}
};
let text = self
.keys
.sign(kind, &payload, &audience, &n, crate::session::now_ms())?;
// Signed for this connection: it goes on it or not at all.
self.frames
.send(for_now.live(text))
.await
.map_err(|_| Error::Closed)?;
Ok(n)
}
/// Ends the connection for `reason` (`"background"`, `"offline"`,
/// ...) and makes no attempt while any reason holds: topics and
/// positions stay, and what is queued goes out first. The last reason
/// cleared with [`resume`](Self::resume), the client connects at once
/// and recovers what it missed.
pub fn pause(&self, reason: impl Into<String>) -> Result<(), Error> {
self.control(Control::Pause(reason.into()))
}
/// Clears `reason`; once none is left the client connects again.
pub fn resume(&self, reason: impl Into<String>) -> Result<(), Error> {
self.control(Control::Resume(reason.into()))
}
/// Subscribes to topics, on this connection and every later one; after
/// a reconnect the client asks for them from where it left off.
pub fn subscribe<I, T>(&self, topics: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
let topics: Vec<String> = topics.into_iter().map(Into::into).collect();
delivery::lock(&self.gate).track(&topics);
self.control(Control::Subscribe(topics))
}
/// Unsubscribes, and forgets where the client stood in the topics.
pub fn unsubscribe<I, T>(&self, topics: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
let topics: Vec<String> = topics.into_iter().map(Into::into).collect();
delivery::lock(&self.gate).untrack(&topics);
self.control(Control::Unsubscribe(topics))
}
/// How many messages wait in the outbox for the server's answer
/// ([`Config::offline`]).
pub fn kept(&self) -> usize {
self.shared.room.as_ref().map_or(0, |room| room.count())
}
/// The user signs out: every message in the outbox, and every one sent
/// before this call, goes ([`Event::NotDelivered`](crate::Event),
/// `cleared`), and the user is forgotten, so nothing of theirs is sent as
/// the next one. One the server has in hand already is delivered if it
/// confirms it on the current connection.
pub fn clear(&self) -> Result<(), Error> {
self.shared.clears.fetch_add(1, Ordering::AcqRel);
self.control(Control::Clear)
}
/// Each topic held: whether the server took it, and whether its
/// messages run without a break.
pub fn topic_states(&self) -> Vec<TopicState> {
delivery::lock(&self.gate).topic_states()
}
/// The consumer applied `message` (a topic message): the checkpoint
/// moves on through what was applied without a hole. A message handed
/// out before the events showed another user counts for nobody. An
/// [`OwnedMessage`](crate::OwnedMessage) commits as `owned.as_message()`.
pub fn commit(&self, message: &Message<'_>) {
if let (Some(topic), Some(at)) = (message.topic(), message.position()) {
self.commit_at(topic, at.epoch, at.offset, message.generation);
}
}
pub(crate) fn commit_at(&self, topic: &str, epoch: u32, offset: u64, generation: u32) {
delivery::lock(&self.gate).commit(topic, epoch, offset, generation);
}
/// What the consumer committed, bound to the realm, the user, the
/// consumer and the schema: to save with the application's own state and
/// give back as [`Delivery::checkpoint`](crate::Delivery::checkpoint).
pub fn checkpoint(&self) -> String {
delivery::lock(&self.gate).checkpoint()
}
/// recover-or-resync: the application brought the topics of `round`
/// ([`Event::Resync`](crate::Event::Resync)) up to date; what they held
/// comes next.
pub fn resynced(&self, round: u64) -> Result<(), Error> {
delivery::lock(&self.gate).resynced(round);
self.wake()
}
/// Bring `topics` up to date: in recover-or-resync a resync round asks
/// for it; in best effort the application did so itself, and they read
/// continuous again.
pub fn resync<I, T>(&self, topics: I) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: Into<String>,
{
let topics: Vec<String> = topics.into_iter().map(Into::into).collect();
delivery::lock(&self.gate).resync(&topics);
self.wake()
}
/// What the gate made goes to the application, and the sends that wait
/// for their topics are looked at again.
pub(crate) fn wake(&self) -> Result<(), Error> {
if let Wake::Reader(reader) = &self.wake {
// A stream nobody reads any more has nobody to tell.
let _ = reader.send(());
}
self.control(Control::Drain)
}
/// Drops the connection and makes it again at once.
pub fn reconnect(&self) -> Result<(), Error> {
self.control(Control::Reconnect)
}
/// The token for the next connection attempt.
pub fn set_token(&self, token: impl Into<String>) {
if let Ok(mut current) = self.shared.token.lock() {
*current = Some(token.into());
}
}
/// Closes the connection for good, and waits until it is.
pub async fn close(&self) {
let _ = self.control.send(Control::Close);
self.closed().await;
}
/// Closes the client, giving what is queued `deadline` to go out: past
/// it the connection is dropped where it stands (a link that takes
/// nothing holds a close for as long as the system keeps the
/// connection).
#[cfg(not(target_arch = "wasm32"))]
pub async fn close_within(&self, deadline: std::time::Duration) {
let _ = self.control.send(Control::Close);
if tokio::time::timeout(deadline, self.closed()).await.is_err() {
if let Some(stop) = self.shared.stop.lock().ok().and_then(|slot| slot.clone()) {
stop.abort();
}
self.closed().await;
}
}
/// Waits until the client stops for good; the reason, unless the
/// application closed it.
pub async fn closed(&self) -> Option<Error> {
let mut status = self.status.clone();
let closed = status
.wait_for(|s| matches!(s, Status::Closed(_)))
.await
.map(|s| s.clone());
match closed {
Ok(Status::Closed(error)) => error,
_ => None,
}
}
pub fn status(&self) -> Status {
self.status.borrow().clone()
}
/// The server took the current connection: messages go out.
pub fn is_connected(&self) -> bool {
self.shared.ready.load(Ordering::Acquire)
}
/// The current connection's id, as the server named it.
pub fn connection_id(&self) -> Option<String> {
let connection = self.shared.connection.lock().ok()?;
connection.as_ref().map(|(id, _)| id.clone())
}
/// The user the server took the connection for.
pub fn user_id(&self) -> Option<String> {
let connection = self.shared.connection.lock().ok()?;
connection.as_ref().and_then(|(_, user)| user.clone())
}
/// The user and connection a message is sent for, read before the flag:
/// one taken in between leaves the message with those before it, which
/// it does not go out for.
/// Who and what a message is sent for now; the client is not closed.
fn sent_for(&self) -> Result<SentFor, Error> {
let sent_for = SentFor {
user: self.shared.users.load(Ordering::Acquire),
connection: self.shared.connections.load(Ordering::Acquire),
ready: self.shared.ready.load(Ordering::Acquire),
clears: self.shared.clears.load(Ordering::Acquire),
};
if !sent_for.ready && matches!(*self.status.borrow(), Status::Closed(_)) {
return Err(Error::Closed);
}
Ok(sent_for)
}
/// [`Client::sent_for`] on a connection the server took.
fn ready(&self) -> Result<SentFor, Error> {
let sent_for = self.sent_for()?;
if sent_for.ready {
Ok(sent_for)
} else {
Err(Error::NotConnected)
}
}
/// A message to send: its id and its frame.
fn outgoing<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
options: &SendOptions,
) -> Result<Queued, Error> {
let id = options.id.clone().unwrap_or_else(message_id);
// For the outbox: after the room for the message's number.
let room = if self.shared.room.is_some() {
crate::outbox::HEADROOM
} else {
0
};
let extra = Extra::of(options);
let text = if room == 0 {
encode(kind, payload, &id, extra)?
} else {
encode_after(room, kind, payload, &id, extra)?
};
let mut frame = self.queued(id.into_boxed_str(), text)?;
frame.until = options.ttl.map(|ttl| {
crate::outbox::now_ms()
.saturating_add(u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX))
});
if !options.requires.is_empty() {
frame.requires = Some(options.requires.iter().cloned().collect());
}
Ok(frame)
}
/// A live message as `options` say, for the connection that is ready
/// now; and its id.
fn live_frame<P: Serialize + ?Sized>(
&self,
kind: &str,
payload: &P,
options: &SendOptions,
) -> Result<(String, Queued), Error> {
if !options.requires.is_empty() || options.ttl.is_some() {
return Err(Error::Config(
"a live message waits for nothing: it takes no ttl and requires no topics"
.to_owned(),
));
}
let for_now = self.ready()?;
let id = options.id.clone().unwrap_or_else(message_id);
let text = encode(kind, payload, &id, Extra::of(options))?;
Ok((id, for_now.live(text)))
}
/// A message for the outbox (its place in the room taken as it is put;
/// its text after the room for its number); without an outbox, for the
/// connection that is ready now.
fn queued(&self, id: Box<str>, text: String) -> Result<Queued, Error> {
let for_now = self.sent_for()?;
match &self.shared.room {
Some(_) => Ok(Queued {
counted: true,
..for_now.frame(id, text)
}),
None if for_now.ready => Ok(for_now.frame(id, text)),
None => Err(Error::NotConnected),
}
}
/// Queues `frame`, waiting for a place in the outbox and room in the
/// queue. A send dropped while it waits (its future cancelled) gives its
/// place back.
async fn put(&self, frame: Queued) -> Result<(), Error> {
/// A place taken in the room, given back unless the frame got into
/// the queue.
struct Taken<'a> {
room: Option<&'a crate::outbox::Room>,
size: usize,
}
impl Drop for Taken<'_> {
fn drop(&mut self) {
if let Some(room) = self.room {
room.give_back(self.size);
}
}
}
let room = self.shared.room.as_deref().filter(|_| frame.counted);
let size = frame.size();
if let Some(room) = room {
room.wait_take(size).await?;
}
let mut taken = Taken { room, size };
let sent = self.frames.send(frame).await;
if sent.is_ok() {
// The session gives it back when the message goes.
taken.room = None;
}
sent.map_err(|_| Error::Closed)
}
/// Queues `frame`, or fails at once when the outbox or the queue is full.
fn try_put(&self, frame: Queued) -> Result<(), Error> {
let room = self.shared.room.as_ref().filter(|_| frame.counted);
if let Some(room) = room
&& !room.take(frame.size())
{
return Err(Error::Full);
}
self.frames.try_send(frame).map_err(|e| {
let (frame, error) = match e {
mpsc::error::TrySendError::Full(frame) => (frame, Error::Full),
mpsc::error::TrySendError::Closed(frame) => (frame, Error::Closed),
};
if let Some(room) = room {
room.give_back(frame.size());
}
error
})
}
fn control(&self, control: Control) -> Result<(), Error> {
self.control.send(control).map_err(|_| Error::Closed)
}
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client")
.field("status", &self.status())
.field("connection_id", &self.connection_id())
.finish()
}
}
/// `{"t": kind, "p": payload, "id": id}` and its extras, in one pass.
fn encode<P: Serialize + ?Sized>(
kind: &str,
payload: &P,
id: &str,
extra: Extra<'_>,
) -> Result<String, Error> {
serde_json::to_string(&Out {
t: kind,
p: payload,
id,
cid: extra.cid,
pri: extra.pri,
})
.map_err(|e| Error::Encode(e.to_string()))
}
/// [`encode`] after `room` spaces, in one buffer: the outbox writes the
/// message's number in them.
fn encode_after<P: Serialize + ?Sized>(
room: usize,
kind: &str,
payload: &P,
id: &str,
extra: Extra<'_>,
) -> Result<String, Error> {
let mut out =
Vec::with_capacity(room + 80 + kind.len() + id.len() + extra.cid.map_or(0, str::len));
out.resize(room, b' ');
serde_json::to_writer(
&mut out,
&Out {
t: kind,
p: payload,
id,
cid: extra.cid,
pri: extra.pri,
},
)
.map_err(|e| Error::Encode(e.to_string()))?;
// SAFETY: spaces, then what serde_json wrote, which is UTF-8 (its own
// `to_string` takes it so).
Ok(unsafe { String::from_utf8_unchecked(out) })
}
/// A message's id: a random UUID (version 4), as the TypeScript and Python
/// clients give theirs.
pub(crate) fn message_id() -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let (hi, lo) = random_pair();
// Version 4, variant 10.
let hi = (hi & 0xffff_ffff_ffff_0fff) | 0x0000_0000_0000_4000;
let lo = (lo & 0x3fff_ffff_ffff_ffff) | 0x8000_0000_0000_0000;
let bytes = ((u128::from(hi) << 64) | u128::from(lo)).to_be_bytes();
let mut out = [0u8; 36];
let mut at = 0;
for (i, byte) in bytes.iter().enumerate() {
if matches!(i, 4 | 6 | 8 | 10) {
out[at] = b'-';
at += 1;
}
out[at] = HEX[usize::from(byte >> 4)];
out[at + 1] = HEX[usize::from(byte & 0x0f)];
at += 2;
}
String::from_utf8_lossy(&out).into_owned()
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn random_pair() -> (u64, u64) {
(fastrand::u64(..), fastrand::u64(..))
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn random_pair() -> (u64, u64) {
// 52 random bits a call: four calls fill both.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let bits = || (js_sys::Math::random() * (1u64 << 52) as f64) as u64;
(bits() << 32 ^ bits(), bits() << 32 ^ bits())
}
/// What the offline queue's file says goes to the application as an
/// [`Event::StorageFailed`], with what the delivery's gate makes.
/// Wakes the connection's task once the outbox's store wrote what messages
/// waited for: they go.
fn outbox_wake(control: &mpsc::UnboundedSender<Control>) -> crate::outbox::Wake {
let control = control.downgrade();
Arc::new(move || {
if let Some(control) = control.upgrade() {
let _ = control.send(Control::Drain);
}
})
}
fn storage_report(
gate: &delivery::Shared,
control: &mpsc::UnboundedSender<Control>,
reader: Option<mpsc::UnboundedSender<()>>,
) -> crate::outbox::Report {
let gate = Arc::clone(gate);
let control = control.downgrade();
Arc::new(move |operation, message| {
tracing::error!("[WSE] the outbox's file: {operation} failed: {message}");
delivery::lock(&gate).tell(Event::StorageFailed(StorageFailed {
operation: operation.to_owned(),
message,
}));
if let Some(reader) = &reader {
let _ = reader.send(());
}
if let Some(control) = control.upgrade() {
let _ = control.send(Control::Drain);
}
})
}