Home | History | Annotate | Download | only in ports
      1 // Copyright 2016 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_EDK_SYSTEM_PORTS_EVENT_H_
      6 #define MOJO_EDK_SYSTEM_PORTS_EVENT_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include "mojo/edk/system/ports/message.h"
     11 #include "mojo/edk/system/ports/name.h"
     12 
     13 namespace mojo {
     14 namespace edk {
     15 namespace ports {
     16 
     17 #pragma pack(push, 1)
     18 
     19 // TODO: Add static assertions of alignment.
     20 
     21 struct PortDescriptor {
     22   PortDescriptor();
     23 
     24   NodeName peer_node_name;
     25   PortName peer_port_name;
     26   NodeName referring_node_name;
     27   PortName referring_port_name;
     28   uint64_t next_sequence_num_to_send;
     29   uint64_t next_sequence_num_to_receive;
     30   uint64_t last_sequence_num_to_receive;
     31   bool peer_closed;
     32   char padding[7];
     33 };
     34 
     35 enum struct EventType : uint32_t {
     36   kUser,
     37   kPortAccepted,
     38   kObserveProxy,
     39   kObserveProxyAck,
     40   kObserveClosure,
     41   kMergePort,
     42 };
     43 
     44 struct EventHeader {
     45   EventType type;
     46   uint32_t padding;
     47   PortName port_name;
     48 };
     49 
     50 struct UserEventData {
     51   uint64_t sequence_num;
     52   uint32_t num_ports;
     53   uint32_t padding;
     54 };
     55 
     56 struct ObserveProxyEventData {
     57   NodeName proxy_node_name;
     58   PortName proxy_port_name;
     59   NodeName proxy_to_node_name;
     60   PortName proxy_to_port_name;
     61 };
     62 
     63 struct ObserveProxyAckEventData {
     64   uint64_t last_sequence_num;
     65 };
     66 
     67 struct ObserveClosureEventData {
     68   uint64_t last_sequence_num;
     69 };
     70 
     71 struct MergePortEventData {
     72   PortName new_port_name;
     73   PortDescriptor new_port_descriptor;
     74 };
     75 
     76 #pragma pack(pop)
     77 
     78 inline const EventHeader* GetEventHeader(const Message& message) {
     79   return static_cast<const EventHeader*>(message.header_bytes());
     80 }
     81 
     82 inline EventHeader* GetMutableEventHeader(Message* message) {
     83   return static_cast<EventHeader*>(message->mutable_header_bytes());
     84 }
     85 
     86 template <typename EventData>
     87 inline const EventData* GetEventData(const Message& message) {
     88   return reinterpret_cast<const EventData*>(
     89       reinterpret_cast<const char*>(GetEventHeader(message) + 1));
     90 }
     91 
     92 template <typename EventData>
     93 inline EventData* GetMutableEventData(Message* message) {
     94   return reinterpret_cast<EventData*>(
     95       reinterpret_cast<char*>(GetMutableEventHeader(message) + 1));
     96 }
     97 
     98 inline const PortDescriptor* GetPortDescriptors(const UserEventData* event) {
     99   return reinterpret_cast<const PortDescriptor*>(
    100       reinterpret_cast<const char*>(event + 1));
    101 }
    102 
    103 inline PortDescriptor* GetMutablePortDescriptors(UserEventData* event) {
    104   return reinterpret_cast<PortDescriptor*>(reinterpret_cast<char*>(event + 1));
    105 }
    106 
    107 }  // namespace ports
    108 }  // namespace edk
    109 }  // namespace mojo
    110 
    111 #endif  // MOJO_EDK_SYSTEM_PORTS_EVENT_H_
    112