blob: 916d17b475b9450e296d2f8e855b4bd6c48dda0b [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
var allowV2WireFormatDecoding bool
// Enable V2 wire format decoding for this process.
func AllowV2WireFormatDecoding() {
allowV2WireFormatDecoding = true
}
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 {
UseV2WireFormat bool
}
func (ctx MarshalerContext) isV2WireFormatDecodingEnabled() bool {
return allowV2WireFormatDecoding && ctx.UseV2WireFormat
}
// newCtx returns the default context.
// During migrations, this controls the default write path.
func newCtx() MarshalerContext {
return MarshalerContext{UseV2WireFormat: false}
}
// MessageHeader represents a transactional message header.
type MessageHeader struct {
_ struct{} `fidl:"s" fidl_size_v1:"16" fidl_alignment_v1:"8" fidl_size_v2:"16" fidl_alignment_v2:"8"`
Txid uint32 `fidl:"0" fidl_offset_v1:"0" fidl_offset_v2:"0" fidl_bounds:""`
Flags [3]uint8 `fidl:"4" fidl_offset_v1:"4" fidl_offset_v2:"4" fidl_bounds:""`
Magic uint8 `fidl:"7" fidl_offset_v1:"7" fidl_offset_v2:"7" fidl_bounds:""`
Ordinal uint64 `fidl:"8" fidl_offset_v1:"8" fidl_offset_v2:"8" fidl_bounds:""`
}
func (msg *MessageHeader) IsSupportedVersion() bool {
return msg.Magic == FidlWireFormatMagicNumberInitial
}
// 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{
UseV2WireFormat: (msg.Flags[0] & FidlV2WireFormatFlagMask) != 0,
}
}
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 {
var flagByte0 uint8
if ctx.UseV2WireFormat {
flagByte0 = 1
}
return MessageHeader{
Flags: [3]uint8{flagByte0, 0, 0},
Magic: FidlWireFormatMagicNumberInitial,
}
}