Bidirectional msgpack wire protocol with unpack support
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
||||
|
||||
var HashKey = [8]byte{}
|
||||
|
||||
type RebootingBootsel struct{}
|
||||
type ResponseBOOTSEL struct{}
|
||||
type RequestBOOTSEL struct{}
|
||||
|
||||
type Envelope struct {
|
||||
Checksum uint32
|
||||
@@ -18,7 +19,23 @@ type Envelope struct {
|
||||
|
||||
func init() {
|
||||
msgpack.RegisterExt(0, (*Envelope)(nil))
|
||||
msgpack.RegisterExt(1, (*RebootingBootsel)(nil))
|
||||
msgpack.RegisterExt(1, (*ResponseBOOTSEL)(nil))
|
||||
msgpack.RegisterExt(2, (*RequestBOOTSEL)(nil))
|
||||
}
|
||||
|
||||
func EncodeMessage(msg any) ([]byte, error) {
|
||||
payload, err := msgpack.Marshal(msg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode inner: %w", err)
|
||||
}
|
||||
|
||||
checksum := halfsiphash.Sum32(payload, HashKey)
|
||||
env := Envelope{Checksum: checksum, Payload: payload}
|
||||
data, err := msgpack.Marshal(&env)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode envelope: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func DecodeMessage(data []byte) (any, error) {
|
||||
|
||||
Reference in New Issue
Block a user