Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_CONTEXT_H_ |
| 2 | #define AOS_EVENTS_CONTEXT_H_ |
| 3 | |
| 4 | #include "aos/flatbuffers.h" |
| 5 | #include "aos/time/time.h" |
| 6 | #include "aos/uuid.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | |
| 10 | // Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context |
| 11 | // about the current message. |
| 12 | struct Context { |
| 13 | // Time that the message was sent on this node, or the timer was triggered. |
| 14 | monotonic_clock::time_point monotonic_event_time; |
| 15 | // Realtime the message was sent on this node. This is set to min_time for |
| 16 | // Timers and PhasedLoops. |
| 17 | realtime_clock::time_point realtime_event_time; |
| 18 | |
| 19 | // The rest are only valid for Watchers and Fetchers. |
| 20 | |
| 21 | // For a single-node configuration, these two are identical to *_event_time. |
| 22 | // In a multinode configuration, these are the times that the message was |
| 23 | // sent on the original node. |
| 24 | monotonic_clock::time_point monotonic_remote_time; |
| 25 | realtime_clock::time_point realtime_remote_time; |
| 26 | |
| 27 | // Index in the queue. |
| 28 | uint32_t queue_index; |
| 29 | // Index into the remote queue. Useful to determine if data was lost. In a |
| 30 | // single-node configuration, this will match queue_index. |
| 31 | uint32_t remote_queue_index; |
| 32 | |
| 33 | // Size of the data sent. |
| 34 | size_t size; |
| 35 | // Pointer to the data. |
| 36 | const void *data; |
| 37 | |
| 38 | // Index of the message buffer. This will be in [0, NumberBuffers) on |
| 39 | // read_method=PIN channels, and -1 for other channels. |
| 40 | // |
| 41 | // This only tells you about the underlying storage for this message, not |
| 42 | // anything about its position in the queue. This is only useful for advanced |
| 43 | // zero-copy use cases, on read_method=PIN channels. |
| 44 | // |
| 45 | // This will uniquely identify a message on this channel at a point in time. |
| 46 | // For senders, this point in time is while the sender has the message. With |
| 47 | // read_method==PIN, this point in time includes while the caller has access |
| 48 | // to this context. For other read_methods, this point in time may be before |
| 49 | // the caller has access to this context, which makes this pretty useless. |
| 50 | int buffer_index; |
| 51 | |
| 52 | // UUID of the remote node which sent this message, or this node in the case |
| 53 | // of events which are local to this node. |
| 54 | UUID source_boot_uuid = UUID::Zero(); |
| 55 | |
| 56 | // Efficiently copies the flatbuffer into a FlatbufferVector, allocating |
| 57 | // memory in the process. It is vital that T matches the type of the |
| 58 | // underlying flatbuffer. |
| 59 | template <typename T> |
| 60 | FlatbufferVector<T> CopyFlatBuffer() const { |
| 61 | ResizeableBuffer buffer; |
| 62 | buffer.resize(size); |
| 63 | memcpy(buffer.data(), data, size); |
| 64 | return FlatbufferVector<T>(std::move(buffer)); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | } // namespace aos |
| 69 | |
| 70 | #endif // AOS_EVENTS_CONTEXT_H_ |