blob: 4487170b3e6d6aab00a4550463cc179722616949 [file] [log] [blame]
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fidl
const FidlWireFormatMagicNumberInitial = 0x01
const FidlV2WireFormatFlagMask = 0x02
// MarshalerContext stores flags to control marshaling/unmarshaling.
//
// This is currently empty. We keep it around to ease the implementation of
// context-dependent behavior for future migrations.
type MarshalerContext struct{}
// NewCtx returns the default context.
// During migrations, this controls the default write path.
func NewCtx() MarshalerContext {
return MarshalerContext{}
}
// MessageHeader represents a transactional message header.
type MessageHeader struct {
_ struct{} `fidl:"s" fidl_size_v2:"16" fidl_alignment_v2:"8"`
Txid uint32 `fidl:"0" fidl_offset_v2:"0" fidl_bounds:""`
Flags [3]uint8 `fidl:"4" fidl_offset_v2:"4" fidl_bounds:""`
Magic uint8 `fidl:"7" fidl_offset_v2:"7" fidl_bounds:""`
Ordinal uint64 `fidl:"8" fidl_offset_v2:"8" fidl_bounds:""`
}
func (msg *MessageHeader) ValidateWireFormat() error {
if msg.Magic != FidlWireFormatMagicNumberInitial {
return ErrUnknownMagic
}
if msg.Flags[0]&FidlV2WireFormatFlagMask == 0 {
return ErrUnsupportedWireFormatVersion
}
return nil
}
// NewCtx creates a new MarshalerContext for unmarshaling based on msg.Flags.
// During migrations, this controls dynamic behavior in the read path.
func (msg *MessageHeader) NewCtx() MarshalerContext {
return MarshalerContext{}
}
var mMessageHeader = MustCreateMarshaler(MessageHeader{})
var MessageHeaderSize = 16
func (msg *MessageHeader) Marshaler() Marshaler {
return mMessageHeader
}
// NewHeader create a new MessageHeader for marshaling with the correct flags
// set based on the MarshalerContext. It is the caller's responsibilty to set
// the transaction ID and method ordinal
func (ctx MarshalerContext) NewHeader() MessageHeader {
return MessageHeader{
Flags: [3]uint8{FidlV2WireFormatFlagMask, 0, 0},
Magic: FidlWireFormatMagicNumberInitial,
}
}