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
use async_std::future;
use cyfs_base::*;
use futures::future::{AbortHandle, AbortRegistration, Abortable};
use rand::Rng;
use std::fmt;
use std::{
    hash::{Hash, Hasher},
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc,
    },
    time::{Duration, SystemTime}
};
use cyfs_debug::Mutex;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Sequence(u32);

impl Sequence {
    pub fn value(&self) -> u32 {
        self.0
    }
}

impl std::fmt::Debug for Sequence {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.value())
    }
}

impl From<u32> for Sequence {
    fn from(v: u32) -> Self {
        Sequence(v)
    }
}

impl Hash for Sequence {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u32(self.0)
    }
}

impl RawFixedBytes for Sequence {
    fn raw_bytes() -> Option<usize> {
        u32::raw_bytes()
    }
}

impl RawEncode for Sequence {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> Result<usize, BuckyError> {
        Ok(<u32 as RawFixedBytes>::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> Result<&'a mut [u8], BuckyError> {
        self.0.raw_encode(buf, purpose)
    }
}

impl<'de> RawDecode<'de> for Sequence {
    fn raw_decode(buf: &'de [u8]) -> Result<(Self, &'de [u8]), BuckyError> {
        u32::raw_decode(buf).map(|(n, buf)| (Self(n), buf))
    }
}

pub struct SequenceGenerator {
    next_seq: Arc<Mutex<u32>>,
}

impl SequenceGenerator {
    pub fn new() -> Self {
        SequenceGenerator {
            next_seq: Arc::new(Mutex::new(1)),
        }
    }

    pub fn generate(&self) -> Sequence {
        let mut next_seq = self.next_seq.lock().unwrap();
        *next_seq += 1;
        Sequence(*next_seq - 1)
    }
}

#[derive(Clone, Copy, Ord, PartialEq, Eq, Debug)]
pub struct TempSeq(u32);

impl TempSeq {
    pub fn value(&self) -> u32 {
        self.0
    }

    fn now(_now: Timestamp) -> u32 {
        let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
        let since_2021 = Duration::from_secs((40 * 365 + 9) * 24 * 3600).as_secs() as u32;
        // TODO: 用10年?
        (now - since_2021) * 10
    }

    // fn time_bits() -> usize {
    //     20
    // }
}

impl PartialOrd for TempSeq {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.0 == 0 || other.0 == 0 {
            self.0.partial_cmp(&other.0)
        } else if (std::cmp::max(self.0, other.0) - std::cmp::min(self.0, other.0)) > (u32::MAX / 2)
        {
            Some(if self.0 > other.0 {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            })
        } else {
            self.0.partial_cmp(&other.0)
        }
    }
}

impl Default for TempSeq {
    fn default() -> Self {
        Self(0)
    }
}

impl From<u32> for TempSeq {
    fn from(v: u32) -> Self {
        Self(v)
    }
}

impl Hash for TempSeq {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u32(self.0)
    }
}

impl RawFixedBytes for TempSeq {
    fn raw_bytes() -> Option<usize> {
        u32::raw_bytes()
    }
}

impl RawEncode for TempSeq {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> Result<usize, BuckyError> {
        Ok(<u32 as RawFixedBytes>::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> Result<&'a mut [u8], BuckyError> {
        self.0.raw_encode(buf, purpose)
    }
}

impl<'de> RawDecode<'de> for TempSeq {
    fn raw_decode(buf: &'de [u8]) -> Result<(Self, &'de [u8]), BuckyError> {
        u32::raw_decode(buf).map(|(n, buf)| (Self(n), buf))
    }
}

pub struct TempSeqGenerator {
    cur: AtomicU32,
}

impl TempSeqGenerator {
    pub fn new() -> Self {
        let now = TempSeq::now(bucky_time_now());
        Self {
            cur: AtomicU32::new(now),
        }
    }

    pub fn generate(&self) -> TempSeq {
        let v = self.cur.fetch_add(1, Ordering::SeqCst);
        if v == 0 {
            TempSeq(self.cur.fetch_add(1, Ordering::SeqCst))
        } else {
            TempSeq(v)
        }
    }
}

pub type Timestamp = u64;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct IncreaseId(u32);

impl std::fmt::Display for IncreaseId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Default for IncreaseId {
    fn default() -> Self {
        Self::invalid()
    }
}

impl IncreaseId {
    pub fn invalid() -> Self {
        Self(0)
    }

    pub fn is_valid(&self) -> bool {
        *self != Self::invalid()
    }
}

impl RawEncode for IncreaseId {
    fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> Result<usize, BuckyError> {
        Ok(<u32 as RawFixedBytes>::raw_bytes().unwrap())
    }

    fn raw_encode<'a>(
        &self,
        buf: &'a mut [u8],
        purpose: &Option<RawEncodePurpose>,
    ) -> Result<&'a mut [u8], BuckyError> {
        self.0.raw_encode(buf, purpose)
    }
}

impl<'de> RawDecode<'de> for IncreaseId {
    fn raw_decode(buf: &'de [u8]) -> Result<(Self, &'de [u8]), BuckyError> {
        u32::raw_decode(buf).map(|(n, buf)| (Self(n), buf))
    }
}

pub struct IncreaseIdGenerator {
    cur: AtomicU32,
}

impl IncreaseIdGenerator {
    pub fn new() -> Self {
        let mut rng = rand::thread_rng();
        Self {
            cur: AtomicU32::new(rng.gen_range(1, 0x7fffffff)),
        }
    }

    pub fn generate(&self) -> IncreaseId {
        IncreaseId(self.cur.fetch_add(1, Ordering::SeqCst) + 1)
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct EndpointPair(Endpoint, Endpoint);

impl std::fmt::Display for EndpointPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{{},{}}}", self.0, self.1)
    }
}

impl From<(Endpoint, Endpoint)> for EndpointPair {
    fn from(ep_pair: (Endpoint, Endpoint)) -> Self {
        assert!(ep_pair.0.is_same_ip_version(&ep_pair.1));
        assert!(ep_pair.0.protocol() == ep_pair.1.protocol());
        Self(ep_pair.0, ep_pair.1)
    }
}

impl EndpointPair {
    pub fn local(&self) -> &Endpoint {
        &self.0
    }

    pub fn remote(&self) -> &Endpoint {
        &self.1
    }

    pub fn protocol(&self) -> Protocol {
        self.0.protocol()
    }

    pub fn is_ipv4(&self) -> bool {
        self.0.addr().is_ipv4()
    }

    pub fn is_ipv6(&self) -> bool {
        self.0.addr().is_ipv6()
    }

    pub fn is_tcp(&self) -> bool {
        self.0.is_tcp() && self.0.addr().port() == 0
    }

    pub fn is_udp(&self) -> bool {
        self.0.is_udp()
    }

    pub fn is_reverse_tcp(&self) -> bool {
        self.0.is_tcp() && self.0.addr().port() != 0
    }
}

pub struct StateWaiter {
    wakers: Vec<AbortHandle>,
}

impl StateWaiter {
    pub fn new() -> Self {
        Self { wakers: vec![] }
    }

    pub fn transfer(&mut self) -> Self {
        let mut waiter = Self::new();
        self.transfer_into(&mut waiter);
        waiter
    }

    pub fn transfer_into(&mut self, waiter: &mut Self) {
        waiter.wakers.append(&mut self.wakers);
    }

    pub fn new_waiter(&mut self) -> AbortRegistration {
        let (waker, waiter) = AbortHandle::new_pair();
        self.wakers.push(waker);
        waiter
    }

    pub async fn wait<T, S: FnOnce() -> T>(waiter: AbortRegistration, state: S) -> T {
        let _ = Abortable::new(future::pending::<()>(), waiter).await;
        state()
    }

    pub fn wake(self) {
        for waker in self.wakers {
            waker.abort();
        }
    }

    pub fn len(&self) -> usize {
        self.wakers.len()
    }
}