aboutsummaryrefslogtreecommitdiff
path: root/mojo/edk/system/ports/message.h
blob: 95fa04676ce0e670025a74036e6f24fed4683ad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_EDK_SYSTEM_PORTS_MESSAGE_H_
#define MOJO_EDK_SYSTEM_PORTS_MESSAGE_H_

#include <stddef.h>

#include <memory>

#include "mojo/edk/system/ports/name.h"

namespace mojo {
namespace edk {
namespace ports {

// A message consists of a header (array of bytes), payload (array of bytes)
// and an array of ports. The header is used by the Node implementation.
//
// This class is designed to be subclassed, and the subclass is responsible for
// providing the underlying storage. The header size will be aligned, and it
// should be followed in memory by the array of ports and finally the payload.
//
// NOTE: This class does not manage the lifetime of the ports it references.
class Message {
 public:
  virtual ~Message() {}

  // Inspect the message at |bytes| and return the size of each section. Returns
  // |false| if the message is malformed and |true| otherwise.
  static bool Parse(const void* bytes,
                    size_t num_bytes,
                    size_t* num_header_bytes,
                    size_t* num_payload_bytes,
                    size_t* num_ports_bytes);

  void* mutable_header_bytes() { return start_; }
  const void* header_bytes() const { return start_; }
  size_t num_header_bytes() const { return num_header_bytes_; }

  void* mutable_payload_bytes() {
    return start_ + num_header_bytes_ + num_ports_bytes_;
  }
  const void* payload_bytes() const {
    return const_cast<Message*>(this)->mutable_payload_bytes();
  }
  size_t num_payload_bytes() const { return num_payload_bytes_; }

  PortName* mutable_ports() {
    return reinterpret_cast<PortName*>(start_ + num_header_bytes_);
  }
  const PortName* ports() const {
    return const_cast<Message*>(this)->mutable_ports();
  }
  size_t num_ports_bytes() const { return num_ports_bytes_; }
  size_t num_ports() const { return num_ports_bytes_ / sizeof(PortName); }

 protected:
  // Constructs a new Message base for a user message.
  //
  // Note: You MUST call InitializeUserMessageHeader() before this Message is
  // ready for transmission.
  Message(size_t num_payload_bytes, size_t num_ports);

  // Constructs a new Message base for an internal message. Do NOT call
  // InitializeUserMessageHeader() when using this constructor.
  Message(size_t num_header_bytes,
          size_t num_payload_bytes,
          size_t num_ports_bytes);

  Message(const Message& other) = delete;
  void operator=(const Message& other) = delete;

  // Initializes the header in a newly allocated message buffer to carry a
  // user message.
  void InitializeUserMessageHeader(void* start);

  // Note: storage is [header][ports][payload].
  char* start_ = nullptr;
  size_t num_ports_ = 0;
  size_t num_header_bytes_ = 0;
  size_t num_ports_bytes_ = 0;
  size_t num_payload_bytes_ = 0;
};

using ScopedMessage = std::unique_ptr<Message>;

}  // namespace ports
}  // namespace edk
}  // namespace mojo

#endif  // MOJO_EDK_SYSTEM_PORTS_MESSAGE_H_