generated from soypat/go-module-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvaluesize.go
66 lines (51 loc) · 1.77 KB
/
valuesize.go
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
/*
package seqs implements TCP control flow.
# Transmission Control Block
The Transmission Control Block (TCB) is the core data structure of TCP.
It stores core state of the TCP connection such as the send and receive
sequence number spaces, the current state of the connection, and the
pending control segment flags.
# Values and Sizes
All arithmetic dealing with sequence numbers must be performed modulo 2**32
which brings with it subtleties to computer modulo arithmetic.
*/
package seqs
import "time"
// Value represents the value of a sequence number.
type Value uint32
// Size represents the size (length) of a sequence number window.
type Size uint32
// LessThan checks if v is before w (modulo 32) i.e., v < w.
func LessThan(v, w Value) bool {
return int32(v-w) < 0
}
// LessThanEq returns true if v==w or v is before (modulo 32) i.e., v < w.
func LessThanEq(v, w Value) bool {
return v == w || LessThan(v, w)
}
// InRange checks if v is in the range [a,b) (modulo 32), i.e., a <= v < b.
func InRange(v, a, b Value) bool {
return v-a < b-a
}
// InWindow checks if v is in the window that starts at 'first' and spans 'size'
// sequence numbers (modulo 32).
func InWindow(v, first Value, size Size) bool {
return InRange(v, first, Add(first, size))
}
// Add calculates the sequence number following the [v, v+s) window.
func Add(v Value, s Size) Value {
return v + Value(s)
}
// Size calculates the size of the window defined by [v, w).
func Sizeof(v, w Value) Size {
return Size(w - v)
}
// UpdateForward updates v such that it becomes v + s.
func (v *Value) UpdateForward(s Size) {
*v += Value(s)
}
// DefaultNewISS returns a new initial send sequence number.
// It's implementation is suggested by RFC9293.
func DefaultNewISS(t time.Time) Value {
return Value(t.UnixMicro() / 4)
}