36 lines
689 B
Go
36 lines
689 B
Go
package node
|
|
|
|
import "unsafe"
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
const (
|
|
headerSize = 12
|
|
controlStreamID = 2
|
|
controlHeaderSize = 24
|
|
dataStreamID = 1
|
|
dataHeaderSize = 12
|
|
)
|
|
|
|
type header struct {
|
|
StreamID byte
|
|
Counter uint64 // Init with time.Now().Unix << 30 to ensure monotonic.
|
|
SourceIP byte
|
|
DestIP byte
|
|
}
|
|
|
|
func (h *header) Parse(b []byte) {
|
|
h.StreamID = b[0]
|
|
h.Counter = *(*uint64)(unsafe.Pointer(&b[1]))
|
|
h.SourceIP = b[9]
|
|
h.DestIP = b[10]
|
|
}
|
|
|
|
func (h *header) Marshal(buf []byte) {
|
|
buf[0] = h.StreamID
|
|
*(*uint64)(unsafe.Pointer(&buf[1])) = h.Counter
|
|
buf[9] = h.SourceIP
|
|
buf[10] = h.DestIP
|
|
buf[11] = 0
|
|
}
|