| // Copyright 2018 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 |
| |
| import ( |
| "sync" |
| |
| "syscall/zx" |
| ) |
| |
| // messageBytesPool is a pool of buffers that can fit the data part of any |
| // FIDL message. |
| var messageBytesPool = sync.Pool{ |
| New: func() interface{} { |
| return make([]byte, zx.ChannelMaxMessageBytes) |
| }, |
| } |
| |
| // messageHandlesPool is a pool of buffers that can fit the handles part of |
| // any FIDL message. |
| var messageHandlesPool = sync.Pool{ |
| New: func() interface{} { |
| return make([]zx.Handle, zx.ChannelMaxMessageHandles) |
| }, |
| } |
| |
| // MarshalHeaderThenMessage marshals a transactional message |
| // (see https://fuchsia.googlesource.com/fuchsia/+/master/docs/development/languages/fidl/reference/wire-format/README.md#messages-for-transactions). |
| // It marshals a `MessageHeader`, optionally followed by a `Message` (body). |
| func MarshalHeaderThenMessage(header *MessageHeader, body Message, data []byte, handles []zx.Handle) (int, int, error) { |
| hnb, _, err := Marshal(header, data, nil) |
| if err != nil { |
| return 0, 0, err |
| } |
| if body == nil { |
| return hnb, 0, nil |
| } |
| nb, nh, err := Marshal(body, data[hnb:], handles) |
| if err != nil { |
| return 0, 0, err |
| } |
| return hnb + nb, nh, nil |
| } |
| |
| // UnmarshalHeaderThenMessage unmarshals a transactional message |
| // (see https://fuchsia.googlesource.com/fuchsia/+/master/docs/development/languages/fidl/reference/wire-format/README.md#messages-for-transactions). |
| // It unmarshals a `MessageHeader`, optionally followed by a `Message` (body). |
| func UnmarshalHeaderThenMessage(data []byte, handles []zx.Handle, header *MessageHeader, body Message) error { |
| hnb, _, err := Unmarshal(data, nil, header) |
| if err != nil { |
| return err |
| } |
| if body == nil { |
| return nil |
| } |
| if _, _, err := UnmarshalWithContext(header.NewCtx(), data[hnb:], handles, body); err != nil { |
| return err |
| } |
| return nil |
| } |