package node import ( "errors" "unsafe" ) var errMalformedPacket = errors.New("malformed packet") const ( packetTypeInvalid = iota // Used to maintain connection. packetTypePing packetTypePong ) type routingPacket struct { Type byte // One of the packetType* constants. TraceID uint64 // For matching requests and responses. } func newRoutingPacket(reqType byte, traceID uint64) routingPacket { return routingPacket{ Type: reqType, TraceID: traceID, } } func (p routingPacket) Marshal(buf []byte) []byte { buf = buf[:32] // Reserve 32 bytes just in case we need to add anything. buf[0] = p.Type *(*uint64)(unsafe.Pointer(&buf[1])) = uint64(p.TraceID) return buf } func (p *routingPacket) Parse(buf []byte) error { if len(buf) != 32 { return errMalformedPacket } p.Type = buf[0] p.TraceID = *(*uint64)(unsafe.Pointer(&buf[1])) return nil }