refactor-for-testability (#3)
Co-authored-by: jdl <jdl@desktop> Co-authored-by: jdl <jdl@crumpington.com> Reviewed-on: #3
This commit is contained in:
46
peer/header.go
Normal file
46
peer/header.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package peer
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
headerSize = 12
|
||||
controlStreamID = 2
|
||||
controlHeaderSize = 24
|
||||
dataStreamID = 1
|
||||
dataHeaderSize = 12
|
||||
)
|
||||
|
||||
type header struct {
|
||||
Version byte
|
||||
StreamID byte
|
||||
SourceIP byte
|
||||
DestIP byte
|
||||
Counter uint64 // Init with time.Now().Unix << 30 to ensure monotonic.
|
||||
}
|
||||
|
||||
func parseHeader(b []byte) (h header) {
|
||||
h.Version = b[0]
|
||||
h.StreamID = b[1]
|
||||
h.SourceIP = b[2]
|
||||
h.DestIP = b[3]
|
||||
h.Counter = *(*uint64)(unsafe.Pointer(&b[4]))
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *header) Parse(b []byte) {
|
||||
h.Version = b[0]
|
||||
h.StreamID = b[1]
|
||||
h.SourceIP = b[2]
|
||||
h.DestIP = b[3]
|
||||
h.Counter = *(*uint64)(unsafe.Pointer(&b[4]))
|
||||
}
|
||||
|
||||
func (h *header) Marshal(buf []byte) {
|
||||
buf[0] = h.Version
|
||||
buf[1] = h.StreamID
|
||||
buf[2] = h.SourceIP
|
||||
buf[3] = h.DestIP
|
||||
*(*uint64)(unsafe.Pointer(&buf[4])) = h.Counter
|
||||
}
|
||||
Reference in New Issue
Block a user