| // 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. |
| |
| // +build fuchsia |
| |
| package fidl |
| |
| const FidlWireFormatMagicNumberInitial = 1 |
| |
| type MarshalerContext struct { |
| DecodeUnionsFromXUnionBytes bool |
| EncodeUnionsAsXUnionBytes bool |
| } |
| |
| // MessageHeader represents a transactional message header. |
| type MessageHeader struct { |
| _ struct{} `fidl:"s,16,8" fidl_size_v1:"16" fidl_alignment_v1:"8"` |
| Txid uint32 `fidl:"0" fidl_offset_v1:"0"` |
| Flags [3]uint8 `fidl:"4" fidl_offset_v1:"4"` |
| Magic uint8 `fidl:"7" fidl_offset_v1:"7"` |
| Ordinal uint64 `fidl:"8" fidl_offset_v1:"8"` |
| } |
| |
| func (msg *MessageHeader) IsSupportedVersion() bool { |
| return msg.Magic == FidlWireFormatMagicNumberInitial |
| } |
| |
| func (msg *MessageHeader) HasUnionFromXunionBytes() bool { |
| return msg.Flags[0]&0x01 != 0 |
| } |
| |
| func (msg *MessageHeader) SetUnionFromXunionBytes() { |
| msg.Flags[0] |= 0x01 |
| } |
| |
| func (msg *MessageHeader) UnsetUnionFromXunionBytes() { |
| msg.Flags[0] &= ^(uint8(0x01)) |
| } |
| |
| // NewCtx creates a new MarshalerContext for unmarshaling based on the flags in |
| // the MessageHeader |
| func (msg *MessageHeader) NewCtx() MarshalerContext { |
| ctx := newCtx() |
| ctx.DecodeUnionsFromXUnionBytes = msg.HasUnionFromXunionBytes() |
| return ctx |
| } |
| |
| 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 { |
| header := MessageHeader{ |
| Flags: [3]uint8{0, 0, 0}, |
| Magic: FidlWireFormatMagicNumberInitial, |
| } |
| if ctx.EncodeUnionsAsXUnionBytes { |
| header.SetUnionFromXunionBytes() |
| } else { |
| header.UnsetUnionFromXunionBytes() |
| } |
| return header |
| } |