syncspin-client 0.1.2

Sendspin client: session loop, noise handshake, Sink trait and event stream
Documentation
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
//! Public client surface: events, handles and the connection entry point.

use std::fmt;
#[cfg(feature = "pairing-code")]
use std::sync::atomic::AtomicU32;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, bail, Context, Result};
use syncspin_core::pairing::token::encode_v0;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;

#[cfg(feature = "pairing-code")]
use crate::config::CodeFormat;
use crate::config::{ClientConfig, StoredPsk, VolumePolicy};
use crate::pairing::{load_or_draw_pairing_psk, PairingPsk};
use crate::session::{self, Carried, Transport};
use crate::sink::{AudioFormat, Sink};

/// How long the TCP and WebSocket handshake may take before it is treated as unreachable.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Delay before the first reconnect attempt, and the value the backoff resets to.
pub const INITIAL_BACKOFF: Duration = Duration::from_secs(1);

/// How long one pairing attempt may run, measured from `client/pair-init`.
///
/// `pairing.md` recommends two minutes. On expiry the client sends `pair/abort` with reason
/// `attempt_timeout` and reports [`Event::PairingFailed`]; a host that wants a tighter bound sets
/// [`ClientConfig::pairing_timeout`].
pub const DEFAULT_PAIRING_TIMEOUT: Duration = Duration::from_secs(120);

/// Highest level the protocol defines, in its 0-100 volume range.
pub(crate) const MAX_VOLUME: u8 = 100;

/// Playback speed the protocol defines as normal, in per-mille.
///
/// A metadata message that carries no speed plays at this rate, so the track event always names
/// one.
pub const NORMAL_PLAYBACK_SPEED: u32 = 1_000;

/// Returns the volume level to apply for `requested` while `current` is in force.
///
/// This is [`clamp_volume_with`] with [`VolumePolicy::default`]: a request to 40 or below is
/// applied as asked, and above 40 one request may raise the level by at most 20. Both values are
/// first limited to the protocol's 0-100 range, so the result is always a level the server
/// accepts.
///
/// ```
/// # use syncspin_client::clamp_volume;
/// assert_eq!(clamp_volume(40, 100), 60);
/// assert_eq!(clamp_volume(40, 10), 10);
/// assert_eq!(clamp_volume(100, 90), 90);
/// assert_eq!(clamp_volume(100, 150), 100);
/// ```
#[must_use]
pub fn clamp_volume(current: u8, requested: u8) -> u8 {
    clamp_volume_with(VolumePolicy::default(), current, requested)
}

/// Returns the volume level to apply for `requested` while `current` is in force, using `policy`.
///
/// Both values are first limited to the protocol's 0-100 range, so the result is always a level
/// the server accepts. A request at or below [`VolumePolicy::threshold`] is applied as asked,
/// whether it raises or lowers the level. Above the threshold, one request may raise the level by
/// at most [`VolumePolicy::max_step`]; a lower request is applied as asked, because the limit
/// bounds raises only. When [`VolumePolicy::threshold_floor`] is set, such a request may also rise
/// to the threshold itself, so a player at 1 reaches 40 in one command instead of 21.
///
/// A `policy` whose fields exceed 100 is not rejected here, because the configuration validation
/// rejects it before a connection exists; the values are limited to 100 so this function still
/// returns a level the server accepts.
///
/// ```
/// # use syncspin_client::{clamp_volume_with, VolumePolicy};
/// let policy = VolumePolicy {
///     threshold: 40,
///     max_step: 20,
///     threshold_floor: true,
/// };
/// assert_eq!(clamp_volume_with(policy, 1, 100), 40);
/// assert_eq!(clamp_volume_with(policy, 40, 100), 60);
/// assert_eq!(clamp_volume_with(policy, 100, 90), 90);
/// ```
#[must_use]
pub fn clamp_volume_with(policy: VolumePolicy, current: u8, requested: u8) -> u8 {
    let threshold = policy.threshold.min(MAX_VOLUME);
    let max_step = policy.max_step.min(MAX_VOLUME);
    let current = current.min(MAX_VOLUME);
    let requested = requested.min(MAX_VOLUME);
    if requested <= threshold {
        return requested;
    }
    let applied = requested.min(current.saturating_add(max_step));
    if policy.threshold_floor {
        applied.max(threshold)
    } else {
        applied
    }
}

/// Rejects a level outside the protocol's 0-100 range.
///
/// A reported level is not a request, so [`clamp_volume_with`] never sees it: a value the protocol
/// cannot carry is refused instead of being capped, and nothing is recorded or sent.
///
/// # Errors
/// Returns an error naming the value when it is above [`MAX_VOLUME`].
pub(crate) fn check_reported_level(level: u8) -> Result<()> {
    if level > MAX_VOLUME {
        bail!(
            "the reported level {level} is outside the 0-{MAX_VOLUME} range the protocol defines"
        );
    }
    Ok(())
}

/// Returns the delay to wait before the next connection attempt.
///
/// The delay starts at [`INITIAL_BACKOFF`] and doubles with every attempt that never reached
/// [`Event::Connected`], saturating at `max`. `previous` is the delay used before the failed
/// attempt, or [`Duration::ZERO`] for the first one. A `connected` session resets the delay: the
/// address and the handshake worked, so a later failure is retried without a growing penalty.
///
/// The crate keeps the sequence across the attempts it makes itself and reports each step in
/// [`Event::Disconnected`]. A host that drives its own loop, for example because it wants to stop
/// retrying, can use this function for the same policy.
///
/// ```
/// # use std::time::Duration;
/// # use syncspin_client::next_backoff;
/// let max = Duration::from_secs(30);
/// assert_eq!(next_backoff(Duration::ZERO, false, max), Duration::from_secs(1));
/// assert_eq!(next_backoff(Duration::from_secs(1), false, max), Duration::from_secs(2));
/// assert_eq!(next_backoff(Duration::from_secs(16), true, max), Duration::from_secs(1));
/// ```
#[must_use]
pub fn next_backoff(previous: Duration, connected: bool, max: Duration) -> Duration {
    let next = if connected || previous.is_zero() {
        INITIAL_BACKOFF
    } else {
        previous.saturating_mul(2)
    };
    next.min(max)
}

/// Why a player stream ended.
///
/// A seek is not an end: the stream stays open and later chunks still belong to it, so it is
/// reported as [`Event::StreamClear`] instead.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum StreamEndReason {
    /// The server sent `stream/end`, which pauses playback.
    End,
    /// The server removed `player@v1` from the active roles.
    RoleRemoved,
}

/// Why a server needs a pairing this client cannot satisfy with what it holds.
///
/// The value is the reason field of [`Event::PairingRequired`]. The enum is non-exhaustive: a later
/// revision may classify another situation the same way.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PairingRequiredReason {
    /// The server announced a `psk_id` this client does not hold, so the handshake fell back to
    /// the Sentinel key.
    ///
    /// `connection.md` calls the resulting handshake the server's authenticated
    /// credential-mismatch signal: the server still holds a record for this client, and the client
    /// has none it can use for it. The signal alone never removes or replaces a record - not the
    /// server's and not the client's - so the host decides whether to offer pairing again. A code
    /// flow or the Pairing PSK flow then replaces the record on both sides.
    CredentialMismatch,
}

/// Why a pairing attempt ended without a record.
///
/// Each value is the `reason` field of a `pair/abort`: the client sends the ones it causes and
/// reports the ones it receives. The enum is non-exhaustive: a later revision defines reasons this
/// one does not name, which arrive as [`Unrecognised`](PairingFailure::Unrecognised).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PairingFailure {
    /// The attempt did not complete within [`ClientConfig::pairing_timeout`].
    AttemptTimeout,
    /// The matched key and the requested method are not a permitted combination, or the client
    /// does not currently offer the method: it has no store, or the activation names a code method
    /// this build does not carry.
    MethodNotSupported,
    /// A key confirmation failed, so the code was wrong or a peer relayed between two handshakes.
    PairingCodeMismatch,
    /// The operator cancelled the pairing, on either side.
    UserCancelled,
    /// Another pairing attempt is already in progress with this client.
    ConcurrentAttempt,
    /// A reason this revision does not name, as it arrived on the wire.
    Unrecognised(String),
}

impl PairingFailure {
    /// Returns the reason as `pair/abort` spells it.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::AttemptTimeout => "attempt_timeout",
            Self::MethodNotSupported => "method_not_supported",
            Self::PairingCodeMismatch => "pairing_code_mismatch",
            Self::UserCancelled => "user_cancelled",
            Self::ConcurrentAttempt => "concurrent_attempt",
            Self::Unrecognised(reason) => reason,
        }
    }

    /// Decodes the `reason` of a received `pair/abort`.
    ///
    /// An unknown value is carried verbatim rather than rejected: the message is a wellformed
    /// abort from a peer that may speak a later revision, and ending the connection over it would
    /// be worse than reporting the reason the host cannot classify.
    pub(crate) fn from_wire(reason: &str) -> Self {
        match reason {
            "attempt_timeout" => Self::AttemptTimeout,
            "method_not_supported" => Self::MethodNotSupported,
            "pairing_code_mismatch" => Self::PairingCodeMismatch,
            "user_cancelled" => Self::UserCancelled,
            "concurrent_attempt" => Self::ConcurrentAttempt,
            other => Self::Unrecognised(other.to_owned()),
        }
    }
}

/// Why a client says goodbye to a server.
///
/// The value is the `reason` field of `client/goodbye`, which the client sends before a graceful
/// close so the server knows whether to reconnect. Only the reasons this revision can reach are
/// named: a reason that needs a state this client cannot detect - `unauthorized`,
/// `pairing_required`, `concurrent_attempt` - is left out until that state exists. The enum is
/// non-exhaustive: a later revision may add one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GoodbyeReason {
    /// The client is switching to another server.
    ///
    /// `messaging.md` requires this reason from a client that leaves one server for another; with
    /// one connection this revision only lets a host express it through
    /// [`ClientHandle::close`].
    AnotherServer,
    /// The client is restarting and will reconnect.
    Restart,
    /// The client is shutting down or otherwise going offline.
    Shutdown,
    /// The operator asked to disconnect from this server.
    UserRequest,
    /// The client processed `server/unpair`.
    Unpaired,
}

impl GoodbyeReason {
    /// Returns the reason as `client/goodbye` spells it.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::AnotherServer => "another_server",
            Self::Restart => "restart",
            Self::Shutdown => "shutdown",
            Self::UserRequest => "user_request",
            Self::Unpaired => "unpaired",
        }
    }
}

/// Something that happened on the connection.
///
/// Events are ordered as the connection produced them. A connection attempt ends with at most one
/// [`Event::Disconnected`], and the crate then reconnects on its own, so a later event belongs to
/// the next attempt. The enum is non-exhaustive: a later revision adds events without breaking a
/// host's match.
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Event {
    /// The Noise handshake finished and the session is ready for playback.
    ///
    /// Reported once per successful attempt. `encrypted` is always `true`: this client has no path
    /// that speaks to a server without Noise, so the field documents the property rather than
    /// hedging it.
    Connected {
        /// The server's static public key as 43 base64url characters.
        server_id: String,
        /// Whether the connection is encrypted; always `true`.
        encrypted: bool,
    },
    /// The attempt ended.
    ///
    /// Another attempt follows unless the audio sink failed or the host dropped the connection.
    /// `reason` is human-readable and never contains key material. `retry_in` is the delay
    /// before the next attempt, one step of the reconnect policy: it is two seconds after an
    /// attempt that never reached [`Event::Connected`], doubles with every further failed attempt
    /// up to [`ClientConfig::max_backoff`], and drops to one second again after a session that
    /// connected. It is [`Duration::ZERO`] when no attempt follows, because the audio sink failed.
    Disconnected {
        /// Why the attempt ended.
        reason: String,
        /// How long to wait before the next attempt; zero when none follows.
        retry_in: Duration,
    },
    /// A stream was announced with the given format.
    ///
    /// The sink has been opened for this format before the event is emitted.
    StreamStart {
        /// Format of the new stream.
        format: AudioFormat,
    },
    /// A player stream ended.
    ///
    /// Reported before the sink is flushed, so a host can mute or stop its output first. The
    /// client flushes the sink itself as well, after this event. A seek does not end the stream;
    /// it is reported as [`Event::StreamClear`], and chunks after it still belong to the open
    /// stream.
    StreamEnd {
        /// Why the stream ended.
        reason: StreamEndReason,
    },
    /// The server asked to drop buffered audio without ending the stream.
    ///
    /// Reported before the sink is flushed, so a host can restart its output or drop the matching
    /// part of its own queue first. The announced format stays open, and chunks that follow still
    /// belong to it.
    StreamClear {
        /// Server timestamp the audio should resume from, when the server named one.
        from_timestamp: Option<i64>,
    },
    /// Now-playing metadata changed.
    ///
    /// Absent text fields are reported as empty strings, so a host can clear its display; absent
    /// progress fields are `None`.
    Track {
        /// Track title.
        title: String,
        /// Track artist.
        artist: String,
        /// Album title.
        album: String,
        /// Album artist.
        album_artist: String,
        /// URL of the artwork image, empty when the server sent none.
        artwork_url: String,
        /// Position within the track in milliseconds, when the server reports one.
        progress_ms: Option<u64>,
        /// Track length in milliseconds, when the server reports one.
        duration_ms: Option<u64>,
        /// Playback speed per mille: `1000` is normal speed, `500` half speed and `0` paused.
        ///
        /// Reported as [`NORMAL_PLAYBACK_SPEED`] when the server sends no speed, because a track
        /// without the field plays at normal speed.
        playback_speed: u32,
    },
    /// The server's view of playback state changed.
    State(PlaybackState),
    /// The server asked this player to change its volume or mute state.
    ///
    /// The session applies the [`VolumePolicy`] from its configuration and reports the applied
    /// level back to the server in `client/state`. The event carries both numbers, so a host can
    /// see what the server asked for and what the policy allowed without repeating the policy.
    /// `muted` is present for a mute command and absent for a volume command; a mute leaves the
    /// level unchanged, so both numbers then name the level already in force.
    Volume {
        /// Level the server asked for, as it arrived on the wire.
        ///
        /// A non-conformant server can send a value above 100; it is reported unchanged so a host
        /// can log it, while `applied` always stays inside the protocol's range.
        requested: u8,
        /// Level that was applied, always in the range 0-100.
        applied: u8,
        /// Mute state that was requested, absent for a volume command.
        muted: Option<bool>,
    },
    /// A pairing completed and the record is stored.
    ///
    /// Reported after `server/pair-finalize`, once [`Storage`](crate::storage::Storage) accepted
    /// the record. The record authenticates later connections to that server, and the server
    /// normally rotates the live connection to it with an in-band re-handshake: the session swaps
    /// its keys without reopening the stream, so playback continues and the next connection is
    /// keyed by the same long-term PSK.
    Paired {
        /// Static public key of the server the record belongs to, as 43 base64url characters.
        server_id: String,
    },
    /// The server dropped its pairing record for this client.
    ///
    /// Reported when a paired session receives `server/unpair`. The record is gone from
    /// [`Storage`](crate::storage::Storage) by the time the event arrives, and the client says
    /// goodbye with reason `unpaired` and closes the connection: the next connection to that
    /// server lands in the Sentinel fallback unless the host pairs again.
    ///
    /// A record the host passed through
    /// [`connect_with_psks`](crate::connect_with_psks) is not in a store the client may write, so
    /// the client reports it and ends the connection without removing it: the host owns that
    /// record and has to drop it.
    Unpaired {
        /// Static public key of the server that dropped the record, as 43 base64url characters.
        server_id: String,
    },
    /// A server needs a pairing this client cannot complete with what it holds.
    ///
    /// Reported once per connection, never once per message: the situation is decided by the
    /// Noise handshake, so the event fires after [`Event::Connected`] and before the session acts
    /// on its first message, and never again while that connection lasts. A plain guest handshake -
    /// an unpaired connection whose server announced the Sentinel key - is not a mismatch and
    /// reports nothing.
    ///
    /// Nothing is removed or replaced on either side, and the session continues as an ordinary
    /// unpaired one. A server that holds a record for this client will keep the connection in
    /// pairing or idle until the host offers pairing again.
    PairingRequired {
        /// Static public key of the server that announced the unknown credential.
        server_id: String,
        /// What the server expects and the client cannot provide.
        reason: PairingRequiredReason,
    },
    /// A pairing attempt ended without a record.
    ///
    /// Nothing was stored: the host has to offer pairing again, or act on the reason. The
    /// connection itself stays open - a pairing abort is not a connection failure - so the session
    /// keeps serving playback in the roles it had.
    PairingFailed {
        /// Why the attempt ended.
        reason: PairingFailure,
    },
    /// A dynamic pairing code is ready for the operator.
    ///
    /// The code belongs to one attempt and is bound to the Noise handshake and to the two nonces
    /// of that attempt. A client with a display or a speaker shows or speaks it; the operator then
    /// types or scans it into the server. The code does not change across the rounds of an attempt,
    /// so a host that already shows it can ignore a repeat with the same `code`, while `round`
    /// tells it that another round is running.
    ///
    /// The value is deliberate: it is the method's whole point, and the host is expected to render
    /// it. It is not key material, but it does authorise one pairing, so a host should not keep it
    /// longer than the attempt.
    #[cfg(feature = "pairing-code")]
    PairingCode {
        /// The code to show: six digits in the `digits` format, a version-1 token in `qr_code`.
        code: String,
        /// Emission format the server activated.
        format: CodeFormat,
        /// Round this emission belongs to, counting from one.
        round: u32,
    },
}

impl fmt::Debug for Event {
    /// Formats an event, redacting the dynamic pairing code it may carry.
    ///
    /// A host renders that code deliberately, through the event itself; a `Debug` format is what a
    /// panic, an assertion or a logging facade writes, and the code authorises one pairing, so it
    /// must not appear there. Every other field is what a log line needs.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Connected {
                server_id,
                encrypted,
            } => formatter
                .debug_struct("Connected")
                .field("server_id", server_id)
                .field("encrypted", encrypted)
                .finish(),
            Self::Disconnected { reason, retry_in } => formatter
                .debug_struct("Disconnected")
                .field("reason", reason)
                .field("retry_in", retry_in)
                .finish(),
            Self::StreamStart { format } => formatter
                .debug_struct("StreamStart")
                .field("format", format)
                .finish(),
            Self::StreamEnd { reason } => formatter
                .debug_struct("StreamEnd")
                .field("reason", reason)
                .finish(),
            Self::StreamClear { from_timestamp } => formatter
                .debug_struct("StreamClear")
                .field("from_timestamp", from_timestamp)
                .finish(),
            Self::Track {
                title,
                artist,
                album,
                album_artist,
                artwork_url,
                progress_ms,
                duration_ms,
                playback_speed,
            } => formatter
                .debug_struct("Track")
                .field("title", title)
                .field("artist", artist)
                .field("album", album)
                .field("album_artist", album_artist)
                .field("artwork_url", artwork_url)
                .field("progress_ms", progress_ms)
                .field("duration_ms", duration_ms)
                .field("playback_speed", playback_speed)
                .finish(),
            Self::State(state) => formatter.debug_tuple("State").field(state).finish(),
            Self::Volume {
                requested,
                applied,
                muted,
            } => formatter
                .debug_struct("Volume")
                .field("requested", requested)
                .field("applied", applied)
                .field("muted", muted)
                .finish(),
            Self::Paired { server_id } => formatter
                .debug_struct("Paired")
                .field("server_id", server_id)
                .finish(),
            Self::Unpaired { server_id } => formatter
                .debug_struct("Unpaired")
                .field("server_id", server_id)
                .finish(),
            Self::PairingRequired { server_id, reason } => formatter
                .debug_struct("PairingRequired")
                .field("server_id", server_id)
                .field("reason", reason)
                .finish(),
            Self::PairingFailed { reason } => formatter
                .debug_struct("PairingFailed")
                .field("reason", reason)
                .finish(),
            #[cfg(feature = "pairing-code")]
            Self::PairingCode { format, round, .. } => formatter
                .debug_struct("PairingCode")
                .field("code", &"<redacted>")
                .field("format", format)
                .field("round", round)
                .finish(),
        }
    }
}

/// Playback state as the server describes it.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PlaybackState {
    /// Commands the server accepts from clients in the same group.
    pub supported_commands: Vec<String>,
    /// Group volume in the range 0-100, when the server reports one.
    pub volume: Option<u8>,
    /// Group mute state, when the server reports one.
    pub muted: Option<bool>,
}

/// A live connection.
///
/// The connection reconnects on its own after a drop, so the handle and the event stream outlive
/// an individual session. Dropping the handle or the event stream ends it; [`ClientHandle::finished`]
/// waits without ending it.
#[derive(Debug)]
pub struct ClientHandle {
    task: Option<JoinHandle<Result<()>>>,
    control: ControlHandle,
    shutdown: Option<oneshot::Sender<GoodbyeReason>>,
}

impl ClientHandle {
    /// Returns a handle for the playback controls of this connection.
    ///
    /// The handle is a cheap clone; every copy controls the same connection.
    #[must_use]
    pub fn control(&self) -> ControlHandle {
        self.control.clone()
    }

    /// Stops the connection after telling the server why.
    ///
    /// The reason is sent as `client/goodbye` on the live connection before it closes; a client
    /// that is reconnecting has no connection to say it on. The goodbye is best effort: a write
    /// that fails neither turns the close into an error nor schedules another attempt. Dropping
    /// the handle does the same with [`GoodbyeReason::Shutdown`].
    pub fn close(&mut self, reason: GoodbyeReason) {
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(reason);
        }
    }

    /// Waits for the connection to end and returns its outcome.
    ///
    /// The connection ends when the handle or the event stream is dropped, when
    /// [`ClientHandle::close`] is called, or when the audio sink fails; a dropped session is
    /// reconnected, not returned. Awaiting this keeps the handle alive, so dropping the event
    /// stream is the usual way to stop.
    pub async fn finished(mut self) -> Result<()> {
        let task = self.task.take().expect("the handle owns its task");
        task.await.context("the connection task panicked")?
    }
}

impl Drop for ClientHandle {
    fn drop(&mut self) {
        // `finished` takes the task, so a handle that is only waiting on the connection does not
        // stop it; a handle that is dropped asks the connection to end and says why.
        if self.task.is_some() {
            if let Some(shutdown) = self.shutdown.take() {
                let _ = shutdown.send(GoodbyeReason::Shutdown);
            }
        }
    }
}

/// Playback controls for a live connection.
///
/// A call returns once the connection has handled it: a transport command has been written to the
/// server, and a level or mute change has been recorded and included in the next state report. It
/// is an error when the connection has ended, when no session is established at that moment, and
/// when the server did not activate the role the command belongs to: transport commands need
/// `controller@v1`, volume and mute state need `player@v1`. A transport command the server did not
/// list in its controller state is refused as well, so a host can offer exactly the buttons the
/// server accepts.
#[derive(Debug, Clone)]
pub struct ControlHandle {
    commands: mpsc::UnboundedSender<ControlRequest>,
    volume: Arc<AtomicU8>,
    pairing_token: Option<PairingToken>,
}

/// The version-0 pairing token, held as the credential it is.
///
/// [`Debug`] prints `<redacted>`, so a log line that formats a [`ControlHandle`] never carries the
/// token, which would let anyone holding that line pair a server with this client.
#[derive(Clone)]
struct PairingToken(String);

impl fmt::Debug for PairingToken {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("<redacted>")
    }
}

impl ControlHandle {
    /// Resumes playback.
    pub async fn play(&self) -> Result<()> {
        self.transport("play").await
    }

    /// Pauses playback at the current position.
    pub async fn pause(&self) -> Result<()> {
        self.transport("pause").await
    }

    /// Skips to the next track.
    pub async fn next(&self) -> Result<()> {
        self.transport("next").await
    }

    /// Skips to the previous track.
    pub async fn previous(&self) -> Result<()> {
        self.transport("previous").await
    }

    /// Sets the output level, limited by [`ClientConfig::volume_policy`] against the level in
    /// force, and returns the level that was applied.
    ///
    /// The level belongs to the connection: the session reports the applied value to the server
    /// with its next state report, which is the initial report when the first clock measurement is
    /// still pending, and every reconnect keeps it. A new connection starts from
    /// [`ClientConfig::volume`], so a host passes the level it restored there and uses this call
    /// for changes after that. [`ControlHandle::volume`] answers what is in force without a call.
    pub async fn set_volume(&self, level: u8) -> Result<u8> {
        self.send(ControlAction::Volume(level)).await
    }

    /// Reports the level the device is actually at, without applying the volume policy.
    ///
    /// [`ControlHandle::set_volume`] treats its argument as a request, so the policy may lower it
    /// and above the threshold it limits one step. A host that knows the level its output is
    /// really at - because the device applied a level at its own control, or because another
    /// process raised it - reports that level with this call instead: it becomes the level in
    /// force, [`ControlHandle::volume`] returns it, and the next `client/state` carries it
    /// unchanged, so the server's view matches the output.
    ///
    /// The protocol's range still applies. A level above 100 is refused with an error before
    /// anything is recorded or sent; the policy is not consulted and does not clamp the value.
    ///
    /// # Errors
    /// Returns an error for a level above 100, for a connection that has ended, and when the
    /// server did not activate `player@v1`.
    pub async fn report_volume(&self, level: u8) -> Result<()> {
        check_reported_level(level)?;
        self.send(ControlAction::ReportVolume(level))
            .await
            .map(|_level| ())
    }

    /// Sets the mute state without changing the level.
    ///
    /// The reported level therefore stays what it was, and the server sees the mute in the
    /// `muted` field alone.
    pub async fn set_muted(&self, muted: bool) -> Result<()> {
        self.send(ControlAction::Mute(muted)).await.map(|_level| ())
    }

    /// Returns the volume level in force.
    ///
    /// The value follows every change the connection made or received, including a mute, which
    /// leaves it untouched. It is available without a call, so a host can render its slider while
    /// the connection is reconnecting.
    #[must_use]
    pub fn volume(&self) -> u8 {
        self.volume.load(Ordering::Relaxed)
    }

    /// The version-0 pairing token for this client, or `None` when no
    /// [`Storage`](crate::storage::Storage) is configured.
    ///
    /// The token is `SP:0` plus the base32 body of the identity's public key and the pairing PSK
    /// that was drawn or restored when the client started, so it is fixed for the client's
    /// lifetime and survives a reconnect. A token is a credential: anyone holding it can pair a
    /// server with this client, so a host renders it only while the operator needs it and never
    /// writes it into a log.
    #[must_use]
    pub fn pairing_token(&self) -> Option<String> {
        self.pairing_token.as_ref().map(|token| token.0.clone())
    }

    /// Opens a pairing window on the live connection and starts a waiting static-code attempt.
    ///
    /// The pairing window is the client's half of the static code flow's operator gesture: the host
    /// decides what the gesture is - a button press, a pinhole, a power-cycle pattern - and calls
    /// this when it happened. The window belongs to the connection that is live now, so it is
    /// opened there and closes when that connection ends, when the configured lifetime expires,
    /// when a pairing completes, or after five failed key confirmations.
    ///
    /// # Errors
    /// Returns an error when no connection is established at this moment. A call that arrives while
    /// the client is reconnecting is refused rather than remembered for the next connection, which
    /// the spec requires: a window admits attempts only on the connection that carries its gesture.
    #[cfg(feature = "pairing-code")]
    pub async fn open_pairing_window(&self) -> Result<()> {
        self.send(ControlAction::OpenPairingWindow)
            .await
            .map(|_level| ())
    }

    /// Releases the dynamic flow's round limit: the deliberate operator action the spec requires.
    ///
    /// After twenty rounds without a verified `server_kc`, the client aborts the attempt and holds
    /// further dynamic attempts back, answering their activations with `client/pair-pending`. The
    /// counter is not partitioned by server and only this call - or a verified `server_kc` - resets
    /// it. A host wires it to whatever gesture means "let this device try pairing again".
    ///
    /// Unlike a pairing window, the release needs no connection: the count belongs to the process,
    /// and the next connection starts from a clean one. The call still answers the current level,
    /// so it observes the same contract as the other control requests.
    #[cfg(feature = "pairing-code")]
    pub async fn release_holdback(&self) -> Result<()> {
        self.send(ControlAction::ReleaseHoldback)
            .await
            .map(|_level| ())
    }

    /// Sends one controller command.
    async fn transport(&self, command: &'static str) -> Result<()> {
        self.send(ControlAction::Transport(command))
            .await
            .map(|_level| ())
    }

    /// Queues one request and waits for the level in force after it.
    async fn send(&self, action: ControlAction) -> Result<u8> {
        let (reply, response) = oneshot::channel();
        self.commands
            .send(ControlRequest { action, reply })
            .map_err(|_| anyhow!("the connection has ended"))?;
        response
            .await
            .map_err(|_| anyhow!("the connection ended before it answered"))?
    }
}

/// What a [`ControlHandle`] asks the session to do.
#[derive(Debug)]
pub(crate) enum ControlAction {
    /// A controller command, by wire name.
    Transport(&'static str),
    /// Set the output level, subject to the volume limit.
    Volume(u8),
    /// Record the level the output is actually at, without the volume limit.
    ReportVolume(u8),
    /// Set the mute state, keeping the level.
    Mute(bool),
    /// Open a pairing window for this connection and start a waiting static-code attempt.
    #[cfg(feature = "pairing-code")]
    OpenPairingWindow,
    /// Reset the dynamic flow's round counter and start the attempt it held back.
    #[cfg(feature = "pairing-code")]
    ReleaseHoldback,
}

/// One request from a [`ControlHandle`], with the channel its answer travels back on.
///
/// The answer is the volume level in force after the request; a transport command and a mute
/// leave it unchanged.
pub(crate) struct ControlRequest {
    /// Operation to perform.
    pub(crate) action: ControlAction,
    /// Answer for the waiting call.
    pub(crate) reply: oneshot::Sender<Result<u8>>,
}

/// The receiving end of a session's [`Event`] stream.
///
/// [`EventStream::next`] returns `None` once the connection has ended. An [`Event::Disconnected`]
/// is not necessarily the last event: the client reconnects and reports the next attempt's events
/// on the same stream, and only a fatal audio-sink failure or a dropped handle ends the sequence.
/// Dropping the stream ends the connection: the next time the client would emit an event, it stops
/// instead.
#[derive(Debug)]
pub struct EventStream {
    events: mpsc::UnboundedReceiver<Event>,
}

impl EventStream {
    /// Waits for the next event.
    pub async fn next(&mut self) -> Option<Event> {
        self.events.recv().await
    }
}

/// Namespaced entry point, for callers that prefer a type to the free function.
#[derive(Debug, Clone, Copy, Default)]
pub struct Client;

impl Client {
    /// Connects to `config.server` and returns the handle and event stream.
    ///
    /// See [`connect`] for the details.
    pub async fn connect(
        config: ClientConfig,
        sink: Box<dyn Sink>,
    ) -> Result<(ClientHandle, EventStream)> {
        connect(config, sink).await
    }

    /// Connects with pairing keys the host stores for known servers.
    ///
    /// See [`connect_with_psks`] for the details.
    pub async fn connect_with_psks(
        config: ClientConfig,
        sink: Box<dyn Sink>,
        psks: Vec<StoredPsk>,
    ) -> Result<(ClientHandle, EventStream)> {
        connect_with_psks(config, sink, psks).await
    }
}

/// Connects to a server and returns its handle and event stream.
///
/// The configuration is validated before this returns; the connection itself runs in a spawned
/// task and is reported through the event stream, with [`Event::Connected`] as the first event of a
/// successful attempt.
///
/// The task owns the sink and every attempt, so it reconnects after a drop with the same identity
/// and sink and with the level, mute state and roles the previous session had. Each drop is
/// reported as [`Event::Disconnected`] together with the delay before the next attempt. The
/// connection ends when the sink fails, which the host has to repair, or when the handle or the
/// event stream is dropped.
///
/// # Errors
/// Returns an error for an invalid [`ClientConfig`] and for a server address this build cannot use
/// (including a TLS URL). An unreachable server is not an error: the connection reports it as
/// [`Event::Disconnected`] and keeps retrying.
///
/// # Panics
/// Panics when called outside a Tokio runtime, because the connection is spawned.
pub async fn connect(
    config: ClientConfig,
    sink: Box<dyn Sink>,
) -> Result<(ClientHandle, EventStream)> {
    connect_with_psks(config, sink, Vec::new()).await
}

/// Connects with pairing keys the host stores for known servers.
///
/// The server announces a `psk_id` and `psk_category` in Noise message 1. A candidate matches when
/// its category and derived id both match the announcement; its `server_id` must then equal the one
/// in `server/init`. On a lookup miss the initial handshake continues with the published Sentinel
/// key, which is why [`connect`] is the same call with no candidates.
///
/// The keys are used for the handshake only; they are not stored or logged. A reconnect uses them
/// again, so a pairing that worked keeps working. A [`Storage`](crate::storage::Storage) configured
/// on `config` joins them: its records are read for every attempt, and on the same `server_id` the
/// stored record wins over `explicit`, because the store is the authority for the servers it knows.
/// The pairing PSK the store holds is always a candidate as well, whether or not a pairing is
/// running.
///
/// # Errors
/// Same as [`connect`]. A record that belongs to another server fails the handshake of that
/// attempt and is reported as [`Event::Disconnected`]. A store that cannot be read or cannot hold
/// the pairing PSK is reported from this call before the first attempt. A configuration with a
/// store makes the client draw and persist its pairing PSK on a device's first run.
pub async fn connect_with_psks(
    config: ClientConfig,
    sink: Box<dyn Sink>,
    psks: Vec<StoredPsk>,
) -> Result<(ClientHandle, EventStream)> {
    config.validate()?;
    let endpoint = endpoint(&config.server)?;
    // The pairing PSK is settled before the first attempt, so a store that cannot hold it is
    // reported to the caller instead of a connection that silently cannot pair. A restored key is
    // used as it is; a missing one is drawn and stored once, here.
    let pairing = match config.storage.as_deref() {
        Some(storage) => Some(load_or_draw_pairing_psk(storage)?),
        None => None,
    };
    // The token is fixed once the key is settled: the identity and the pairing PSK do not change
    // while the client runs, so it is built here and carried by every control handle.
    let pairing_token = pairing
        .as_ref()
        .map(|psk| PairingToken(encode_v0(&config.identity.public, psk.as_bytes())));

    let (events, receiver) = mpsc::unbounded_channel();
    let (commands, control) = mpsc::unbounded_channel();
    let (shutdown, shutdown_rx) = oneshot::channel();
    let reported_volume = Arc::new(AtomicU8::new(config.volume));
    // The dynamic flow's round counter belongs to the process, not to a connection or a server:
    // only a verified `server_kc` or a deliberate operator action resets it.
    #[cfg(feature = "pairing-code")]
    let failed_rounds = Arc::new(AtomicU32::new(0));

    let carried = Carried {
        sink,
        control: Some(control),
        volume: config.volume,
        muted: config.muted,
        active_roles: Vec::new(),
        #[cfg(feature = "pairing-code")]
        failed_rounds: Arc::clone(&failed_rounds),
    };
    let connection = Connection {
        endpoint,
        config,
        psks,
        pairing,
        events: events.clone(),
        reported_volume: Arc::clone(&reported_volume),
        #[cfg(feature = "pairing-code")]
        failed_rounds,
    };

    let task = tokio::spawn(serve_until_dropped(connection, carried, shutdown_rx));

    let handle = ClientHandle {
        task: Some(task),
        control: ControlHandle {
            commands,
            volume: reported_volume,
            pairing_token,
        },
        shutdown: Some(shutdown),
    };
    Ok((handle, EventStream { events: receiver }))
}

/// Opens one WebSocket, treating an address that does not answer as an error.
async fn open(endpoint: &str) -> Result<Transport> {
    tokio::time::timeout(CONNECT_TIMEOUT, Transport::connect(endpoint))
        .await
        .with_context(|| format!("timed out connecting to {endpoint}"))?
}

/// The parts of a connection that do not change across attempts.
struct Connection {
    endpoint: String,
    config: ClientConfig,
    psks: Vec<StoredPsk>,
    /// This client's pairing PSK, present exactly when a store is configured.
    pairing: Option<PairingPsk>,
    events: mpsc::UnboundedSender<Event>,
    /// Level shared with every [`ControlHandle`].
    reported_volume: Arc<AtomicU8>,
    /// Failed dynamic rounds since the last verified `server_kc`, shared with every session this
    /// connection runs and with [`ControlHandle::release_holdback`].
    #[cfg(feature = "pairing-code")]
    failed_rounds: Arc<AtomicU32>,
}

impl Connection {
    /// Delivers one event, ignoring a stream the host dropped.
    fn emit(&self, event: Event) {
        let _ = self.events.send(event);
    }
}

/// One wake-up while no session is established.
enum Wakeup {
    /// A control handle sent a request.
    Control(Option<ControlRequest>),
    /// The delay elapsed.
    Deadline,
    /// The host dropped the event stream.
    Closed,
}

/// Answers a request that arrived while no session was established.
///
/// A pairing window belongs to a connection and is refused; releasing the round hold-back belongs
/// to the process and is honoured, so an operator gesture made during a reconnect is not lost.
#[cfg(feature = "pairing-code")]
fn refuse(connection: &Connection, carried: &Carried, request: ControlRequest) {
    if matches!(request.action, ControlAction::ReleaseHoldback) {
        connection.failed_rounds.store(0, Ordering::SeqCst);
        let _ = request.reply.send(Ok(carried.volume));
        return;
    }
    let _ = request
        .reply
        .send(Err(anyhow!("no connection is established")));
}

/// Answers a request that arrived while no session was established.
#[cfg(not(feature = "pairing-code"))]
fn refuse(request: ControlRequest) {
    let _ = request
        .reply
        .send(Err(anyhow!("no connection is established")));
}

/// Waits before the next attempt, refusing control requests meanwhile.
///
/// Returns `false` when the host dropped the event stream and the connection should end.
async fn wait_before_retry(
    connection: &Connection,
    carried: &mut Carried,
    delay: Duration,
    shutdown: &mut oneshot::Receiver<GoodbyeReason>,
) -> bool {
    let deadline = tokio::time::Instant::now() + delay;
    loop {
        if connection.events.is_closed() {
            return false;
        }
        let control = &mut carried.control;
        let wakeup = tokio::select! {
            biased;
            request = async {
                match control {
                    Some(receiver) => receiver.recv().await,
                    None => std::future::pending().await,
                }
            } => Wakeup::Control(request),
            // No connection is open, so there is nothing to say goodbye on; the loop just ends.
            reason = &mut *shutdown => {
                let _ = reason;
                return false;
            }
            () = tokio::time::sleep_until(deadline) => Wakeup::Deadline,
            () = connection.events.closed() => Wakeup::Closed,
        };
        match wakeup {
            Wakeup::Control(Some(request)) => {
                #[cfg(feature = "pairing-code")]
                refuse(connection, carried, request);
                #[cfg(not(feature = "pairing-code"))]
                refuse(request);
            }
            Wakeup::Control(None) => *control = None,
            Wakeup::Deadline => return true,
            Wakeup::Closed => return false,
        }
    }
}

/// Runs connection attempts until the host drops the connection or the sink fails.
///
/// Every attempt, including the first, is opened here. A failed one is followed by the next after
/// the delay [`next_backoff`] computes, with the sink, the level, the mute state and the roles
/// carried over. A [`ClientHandle::close`] or a dropped handle stops the loop after the live
/// session has had its chance to say goodbye.
async fn serve_until_dropped(
    connection: Connection,
    mut carried: Carried,
    mut shutdown: oneshot::Receiver<GoodbyeReason>,
) -> Result<()> {
    // The first attempt is not preceded by a wait, but the policy starts at the initial delay, so a
    // failure before any session connected reports two seconds: one growth step from that value.
    let mut delay = INITIAL_BACKOFF;

    loop {
        // A close that arrives while the attempt is still connecting has no session to say
        // goodbye on, so the loop ends as soon as it fires instead of waiting out the connect
        // timeout and reporting a failure the host did not cause.
        let transport = tokio::select! {
            result = open(&connection.endpoint) => result,
            reason = &mut shutdown => {
                let _ = reason;
                return Ok(());
            }
        };
        let transport = match transport {
            Ok(transport) => transport,
            Err(error) => {
                delay = next_backoff(delay, false, connection.config.max_backoff);
                connection.emit(Event::Disconnected {
                    reason: format!("cannot connect to {}: {error:#}", connection.endpoint),
                    retry_in: delay,
                });
                if !wait_before_retry(&connection, &mut carried, delay, &mut shutdown).await {
                    return Ok(());
                }
                continue;
            }
        };

        let attempt = session::attempt(
            transport,
            &connection.config,
            &connection.events,
            &connection.psks,
            connection.pairing,
            &connection.reported_volume,
            carried,
            &mut shutdown,
        )
        .await;
        carried = attempt.carried;

        // Nothing buffered for the session that ended may become audible later; the flush runs
        // before the host hears about the drop so the output stops even if the next attempt takes
        // a while.
        let flush_error = carried.sink.flush().err().map(|error| {
            error.context("the audio sink rejected a flush after the connection ended")
        });

        // A deliberate close is not a drop: nothing is reported and no attempt follows. The
        // goodbye was best effort, so neither its send nor the flush can turn it into an error.
        if matches!(&attempt.ending, session::Ending::Shutdown) {
            return Ok(());
        }

        // A broken output cannot be repaired by reconnecting, and neither can a flush that fails
        // after the session; both end the connection instead of scheduling another attempt.
        let fatal = attempt.sink_failed || flush_error.is_some();
        delay = if fatal {
            // No attempt follows, so there is no delay to recommend.
            Duration::ZERO
        } else {
            next_backoff(delay, attempt.connected, connection.config.max_backoff)
        };
        connection.emit(Event::Disconnected {
            reason: attempt.ending.reason(),
            retry_in: delay,
        });

        if attempt.sink_failed {
            // The host has to fix the sink; a retry would only fail the same way.
            return match attempt.ending {
                session::Ending::Failed(error) => Err(error),
                session::Ending::Closed | session::Ending::Unpaired | session::Ending::Shutdown => {
                    Err(anyhow!("the audio sink failed"))
                }
            };
        }
        if let Some(error) = flush_error {
            return Err(error);
        }

        if !wait_before_retry(&connection, &mut carried, delay, &mut shutdown).await {
            return Ok(());
        }
    }
}

/// Splits a URL tail into the part before a query or fragment and the marker onward.
fn split_marker(value: &str) -> (&str, &str) {
    match value.find(['?', '#']) {
        Some(index) => value.split_at(index),
        None => (value, ""),
    }
}

/// Builds the WebSocket URL for a configured server address.
///
/// A bare `host:port` uses the default `/sendspin` path, and so does a `ws://` URL without a path.
/// A URL with an explicit path is used unchanged. TLS is rejected: this build carries no TLS
/// stack, and pretending otherwise would end in a confusing connection error.
fn endpoint(server: &str) -> Result<String> {
    let server = server.trim();
    if server.is_empty() {
        bail!("the server address is empty");
    }
    if server.starts_with("wss://") {
        bail!("TLS is not supported by this build; use a ws:// address or host:port");
    }
    if let Some(rest) = server.strip_prefix("ws://") {
        if rest.is_empty() {
            bail!("the WebSocket address {server:?} names no host");
        }
        // A query or fragment belongs after the path, so the default path has to be inserted
        // before it rather than appended to the whole URL.
        let (before_marker, after_marker) = split_marker(rest);
        let Some((authority, path)) = before_marker.split_once('/') else {
            if before_marker.is_empty() {
                bail!("the WebSocket address {server:?} names no host");
            }
            return Ok(format!("ws://{before_marker}/sendspin{after_marker}"));
        };
        if authority.is_empty() {
            bail!("the WebSocket address {server:?} names no host");
        }
        if path.is_empty() {
            return Ok(format!("ws://{authority}/sendspin{after_marker}"));
        }
        return Ok(format!("ws://{before_marker}{after_marker}"));
    }
    if server.contains("://") {
        bail!("unsupported URL scheme in {server:?}; use ws:// or host:port");
    }
    let (before_marker, after_marker) = split_marker(server);
    if before_marker.is_empty() {
        bail!("the server address is empty");
    }
    Ok(format!("ws://{before_marker}/sendspin{after_marker}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A pairing code is a credential for one attempt: `Debug` names it but never prints it.
    #[cfg(feature = "pairing-code")]
    #[test]
    fn the_pairing_code_event_formats_without_its_code() {
        for (code, format) in [
            ("482913".to_owned(), CodeFormat::Digits),
            (
                "SP:1QJOHGMGBXP55Y6KMJD3KOYR9F6OWYD6QQPIKEFY".to_owned(),
                CodeFormat::QrCode,
            ),
        ] {
            let event = Event::PairingCode {
                code: code.clone(),
                format,
                round: 3,
            };
            let text = format!("{event:?}");
            assert!(text.contains("code: \"<redacted>\""), "got: {text}");
            assert!(
                !text.contains(&code),
                "the code must never reach a log line: {text}"
            );
            assert!(
                text.contains(&format!("format: {format:?}")),
                "the format is not secret: {text}"
            );
            assert!(text.contains("round: 3"), "the round is not secret: {text}");
        }
    }

    /// The record-lifecycle events name the server, which is not secret, and nothing else.
    #[test]
    fn the_record_lifecycle_events_format_their_server_id() {
        let server_id = "B1y4w7z0c3f6i9l2o5r8u1x4a7d0g3j6m9p2s5v8y1b".to_owned();

        let unpaired = format!(
            "{:?}",
            Event::Unpaired {
                server_id: server_id.clone(),
            }
        );
        assert!(
            unpaired.contains(&format!("Unpaired {{ server_id: \"{server_id}\" }}")),
            "got: {unpaired}"
        );

        let required = format!(
            "{:?}",
            Event::PairingRequired {
                server_id: server_id.clone(),
                reason: PairingRequiredReason::CredentialMismatch,
            }
        );
        assert!(required.contains(&server_id), "got: {required}");
        assert!(
            required.contains("CredentialMismatch"),
            "the reason is what the host acts on: {required}"
        );
        assert!(
            !required.contains("<redacted>"),
            "neither event carries a secret, so neither redacts a field: {required}"
        );
    }

    /// Every other event keeps the fields a log line needs.
    #[test]
    fn an_event_still_formats_its_readable_fields() {
        let text = format!(
            "{:?}",
            Event::Disconnected {
                reason: "the server closed the connection".to_owned(),
                retry_in: Duration::from_secs(2),
            }
        );
        assert!(
            text.contains("Disconnected { reason: \"the server closed the connection\""),
            "got: {text}"
        );
        assert!(text.contains("retry_in: 2s"), "got: {text}");
    }

    #[test]
    fn every_abort_reason_round_trips_through_the_wire_spelling() {
        for (wire, reason) in [
            ("attempt_timeout", PairingFailure::AttemptTimeout),
            ("method_not_supported", PairingFailure::MethodNotSupported),
            ("pairing_code_mismatch", PairingFailure::PairingCodeMismatch),
            ("user_cancelled", PairingFailure::UserCancelled),
            ("concurrent_attempt", PairingFailure::ConcurrentAttempt),
        ] {
            assert_eq!(reason.as_str(), wire);
            assert_eq!(PairingFailure::from_wire(wire), reason);
        }

        // A reason from a later revision is carried, not rejected.
        assert_eq!(
            PairingFailure::from_wire("future_reason"),
            PairingFailure::Unrecognised("future_reason".to_owned())
        );
        assert_eq!(
            PairingFailure::Unrecognised("future_reason".to_owned()).as_str(),
            "future_reason"
        );
    }

    #[test]
    fn a_bare_address_gets_the_default_websocket_endpoint() {
        assert_eq!(
            endpoint("192.0.2.10:8927").unwrap(),
            "ws://192.0.2.10:8927/sendspin"
        );
        assert_eq!(
            endpoint(" 192.0.2.10:8927 ").unwrap(),
            "ws://192.0.2.10:8927/sendspin"
        );
        assert_eq!(
            endpoint("ws://example.test:1").unwrap(),
            "ws://example.test:1/sendspin"
        );
        assert_eq!(
            endpoint("ws://example.test:1/").unwrap(),
            "ws://example.test:1/sendspin"
        );
        assert_eq!(
            endpoint("ws://example.test:1/player").unwrap(),
            "ws://example.test:1/player"
        );
    }

    #[test]
    fn a_query_or_fragment_stays_after_the_default_path() {
        assert_eq!(
            endpoint("ws://example.test:1?token=1").unwrap(),
            "ws://example.test:1/sendspin?token=1"
        );
        assert_eq!(
            endpoint("ws://example.test:1#frag").unwrap(),
            "ws://example.test:1/sendspin#frag"
        );
        assert_eq!(
            endpoint("ws://example.test:1/?token=1").unwrap(),
            "ws://example.test:1/sendspin?token=1"
        );
        assert_eq!(
            endpoint("ws://example.test:1/player?token=1").unwrap(),
            "ws://example.test:1/player?token=1"
        );
        assert_eq!(
            endpoint("example.test:1?token=1").unwrap(),
            "ws://example.test:1/sendspin?token=1"
        );
    }

    #[test]
    fn an_address_this_build_cannot_use_is_rejected() {
        for server in [
            "",
            "   ",
            "wss://example.test",
            "https://example.test",
            "ws://",
            "ws://?token=1",
            "ws://#fragment",
            "ws:///player",
            "?token=1",
            "#fragment",
        ] {
            assert!(
                endpoint(server).is_err(),
                "{server:?} must not be turned into a cleartext endpoint"
            );
        }
    }

    use crate::test_support::NullSink;

    /// Builds the parts of a connection that do not change across attempts.
    fn connection_at(endpoint: &str, events: mpsc::UnboundedSender<Event>) -> Connection {
        Connection {
            endpoint: endpoint.to_owned(),
            config: ClientConfig {
                server: endpoint.to_owned(),
                player_name: "Test player".to_owned(),
                device_info: None,
                identity: crate::Identity::generate(),
                storage: None,
                audio_mode: crate::AudioMode::Passthrough,
                volume: 42,
                muted: false,
                volume_policy: VolumePolicy::default(),
                max_backoff: Duration::from_secs(30),
                pairing_timeout: DEFAULT_PAIRING_TIMEOUT,
                #[cfg(feature = "pairing-code")]
                static_pairing_code: None,
                #[cfg(feature = "pairing-code")]
                pairing_window: crate::DEFAULT_PAIRING_WINDOW,
                #[cfg(feature = "pairing-code")]
                pairing_pending_text: None,
                #[cfg(feature = "pairing-code")]
                code_formats: Vec::new(),
                #[cfg(feature = "pairing-code")]
                code_out_channels: Vec::new(),
            },
            psks: Vec::new(),
            pairing: None,
            events,
            reported_volume: Arc::new(AtomicU8::new(42)),
            #[cfg(feature = "pairing-code")]
            failed_rounds: Arc::new(AtomicU32::new(0)),
        }
    }

    fn empty_carried(control: Option<mpsc::UnboundedReceiver<ControlRequest>>) -> Carried {
        Carried {
            sink: Box::new(NullSink),
            control,
            volume: 42,
            muted: false,
            active_roles: Vec::new(),
            #[cfg(feature = "pairing-code")]
            failed_rounds: Arc::new(AtomicU32::new(0)),
        }
    }

    /// A shutdown receiver the test never fires; its sender is leaked so the receiver stays open.
    fn idle_shutdown() -> oneshot::Receiver<GoodbyeReason> {
        let (sender, receiver) = oneshot::channel();
        Box::leak(Box::new(sender));
        receiver
    }

    #[tokio::test]
    async fn a_retry_wait_without_a_control_handle_still_honours_the_delay() {
        let (events, _receiver) = mpsc::unbounded_channel();
        let connection = connection_at("127.0.0.1:1", events);
        let (commands, control) = mpsc::unbounded_channel();
        drop(commands);
        let mut carried = empty_carried(Some(control));
        let mut shutdown = idle_shutdown();

        let started = std::time::Instant::now();
        let retry = wait_before_retry(
            &connection,
            &mut carried,
            Duration::from_millis(50),
            &mut shutdown,
        )
        .await;

        assert!(retry, "the delay elapsed, so the next attempt must follow");
        assert!(
            started.elapsed() >= Duration::from_millis(50),
            "the delay must be waited out, not skipped"
        );
        assert!(
            carried.control.is_none(),
            "a control channel whose senders are gone is dropped"
        );
    }

    #[tokio::test]
    async fn dropping_the_event_stream_during_a_retry_ends_the_connection() {
        let (events, receiver) = mpsc::unbounded_channel();
        let connection = connection_at("127.0.0.1:1", events);
        let mut carried = empty_carried(None);
        let mut shutdown = idle_shutdown();

        let retry = wait_before_retry(
            &connection,
            &mut carried,
            Duration::from_secs(60),
            &mut shutdown,
        );
        tokio::pin!(retry);
        let mut finished = false;
        tokio::select! {
            _ = &mut retry => finished = true,
            () = tokio::time::sleep(Duration::from_millis(50)) => {}
        }
        assert!(!finished, "the wait must not end while the stream is open");

        drop(receiver);
        assert!(
            !retry.await,
            "a dropped event stream must stop the retry loop"
        );
    }

    #[tokio::test]
    async fn a_failed_attempt_with_no_event_stream_ends_the_connection() {
        // Bind and release a loopback port, so the open fails immediately rather than timing out.
        let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).expect("a loopback port");
        let address = listener.local_addr().expect("the bound address");
        drop(listener);

        let (events, receiver) = mpsc::unbounded_channel();
        drop(receiver);
        let connection = connection_at(&address.to_string(), events);

        let outcome = serve_until_dropped(connection, empty_carried(None), idle_shutdown()).await;
        assert!(
            outcome.is_ok(),
            "a host that dropped the stream ends the connection cleanly"
        );
    }

    /// A close that arrives while the socket is still opening ends the loop at once, without an
    /// `Event::Disconnected` for a failure the host did not cause.
    #[tokio::test]
    async fn a_close_during_the_connect_ends_without_a_disconnected_event() {
        // A loopback listener that never accepts: the TCP connection is queued by the kernel and
        // the WebSocket handshake answers nothing, so the open stays in flight.
        let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("a loopback port");
        let address = listener.local_addr().expect("the bound address");
        let url = format!("ws://{address}/sendspin");

        let (events, mut receiver) = mpsc::unbounded_channel();
        let connection = connection_at(&url, events);
        let (shutdown, shutdown_rx) = oneshot::channel();
        let task = tokio::spawn(serve_until_dropped(
            connection,
            empty_carried(None),
            shutdown_rx,
        ));

        tokio::time::sleep(Duration::from_millis(100)).await;
        shutdown
            .send(GoodbyeReason::UserRequest)
            .expect("the connection loop is still waiting");

        let outcome = tokio::time::timeout(Duration::from_secs(2), task)
            .await
            .expect("the close must not wait out the connect timeout")
            .expect("the connection task panicked");
        assert!(
            outcome.is_ok(),
            "a close during the connect is clean: {outcome:?}"
        );
        if let Ok(event) = receiver.try_recv() {
            panic!(
                "no failure event may be reported for a close the host asked for, got {event:?}"
            );
        }
    }

    #[tokio::test]
    async fn the_namespace_entry_points_wrap_the_free_functions() {
        // A bad configuration is rejected by both before any socket is opened, which pins the
        // forwarding: a wrapper that inverted the result would not return this error.
        let mut config = connection_at("127.0.0.1:1", mpsc::unbounded_channel().0).config;
        config.player_name.clear();
        let error = Client::connect(config, Box::new(NullSink))
            .await
            .expect_err("an invalid configuration must be rejected");
        assert!(format!("{error:#}").contains("player_name"));

        let mut config = connection_at("127.0.0.1:1", mpsc::unbounded_channel().0).config;
        config.server = "wss://example.test".to_owned();
        let error = Client::connect_with_psks(config, Box::new(NullSink), Vec::new())
            .await
            .expect_err("a TLS address must be rejected before connecting");
        assert!(format!("{error:#}").contains("TLS"));
    }
}