载入中...
搜索中...
未找到
GameEvent.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6#include "common/Identity.h"
7#include "common/Module.h"
8#include "common/Result.h"
9#include "common/Revision.h"
11#include "common/Snapshot.h"
12#include "common/Time.h"
13#include "common/UuidV7.h"
14
15#include <cstdint>
16#include <deque>
17#include <functional>
18#include <memory>
19#include <optional>
20#include <ostream>
21#include <string>
22#include <string_view>
23#include <utility>
24#include <variant>
25#include <vector>
26
27namespace eve::game_event {
28
33
43
51public:
53 enum class Kind : uint8_t { None, Id, Legacy };
54
56 CorrelationId() = default;
57
59 [[nodiscard]] static CorrelationId fromEventId(EventId value) {
61 result.kind_ = value.isNil() ? Kind::None : Kind::Id;
62 result.id_ = value;
63 return result;
64 }
65
67 [[nodiscard]] static CorrelationId fromLegacy(std::string value) {
69 result.kind_ = value.empty() ? Kind::None : Kind::Legacy;
70 result.legacy_ = std::move(value);
71 return result;
72 }
73
75 [[nodiscard]] Kind kind() const noexcept { return kind_; }
76
78 [[nodiscard]] bool isCanonical() const noexcept { return kind_ != Kind::Legacy; }
79
81 [[nodiscard]] std::string format() const { return kind_ == Kind::Id ? id_.format() : legacy_; }
82
83 friend bool operator==(const CorrelationId&, const CorrelationId&) noexcept = default;
84
85private:
86 Kind kind_ = Kind::None;
87 EventId id_;
88 std::string legacy_;
89};
90
98public:
100 enum class Kind : uint8_t { None, Event, Command, Legacy };
101
103 CausationRef() = default;
104
106 [[nodiscard]] static CausationRef fromEventId(EventId value) {
108 if (!value.isNil()) {
109 result.kind_ = Kind::Event;
110 result.value_ = std::move(value);
111 }
112 return result;
113 }
114
118 if (!value.isNil()) {
119 result.kind_ = Kind::Command;
120 result.value_ = std::move(value);
121 }
122 return result;
123 }
124
126 [[nodiscard]] static CausationRef fromLegacy(std::string value) {
128 result.kind_ = value.empty() ? Kind::None : Kind::Legacy;
129 result.value_ = std::move(value);
130 return result;
131 }
132
134 [[nodiscard]] Kind kind() const noexcept { return kind_; }
135
137 [[nodiscard]] bool isCanonical() const noexcept { return kind_ != Kind::Legacy; }
138
140 [[nodiscard]] std::string format() const {
141 if (kind_ == Kind::Event) return std::get<EventId>(value_).format();
142 if (kind_ == Kind::Command) return std::get<CommandId>(value_).format();
143 if (kind_ == Kind::Legacy) return std::get<std::string>(value_);
144 return {};
145 }
146
147 friend bool operator==(const CausationRef&, const CausationRef&) noexcept = default;
148
149private:
150 Kind kind_ = Kind::None;
151 std::variant<std::monostate, EventId, CommandId, std::string> value_;
152};
153
155inline std::ostream& operator<<(std::ostream& stream, CorrelationId::Kind kind) {
156 switch (kind) {
157 case CorrelationId::Kind::None: return stream << "none";
158 case CorrelationId::Kind::Id: return stream << "id";
159 case CorrelationId::Kind::Legacy: return stream << "legacy";
160 }
161 return stream << "unknown";
162}
163
165inline std::ostream& operator<<(std::ostream& stream, CausationRef::Kind kind) {
166 switch (kind) {
167 case CausationRef::Kind::None: return stream << "none";
168 case CausationRef::Kind::Event: return stream << "event";
169 case CausationRef::Kind::Command: return stream << "command";
170 case CausationRef::Kind::Legacy: return stream << "legacy";
171 }
172 return stream << "unknown";
173}
174
210
215template <class Payload>
221
222class GameEventLog;
223
226public:
228 int read(int maxCount);
230 int batchCount() const;
238 const GameEvent* batchAt(int index) const;
240 [[nodiscard]] EventSequence position() const;
242 void seek(EventSequence sequence);
244 void seek(uint64_t sequence);
245
246private:
247 friend class GameEventLog;
249
250 GameEventLog* stream_ = nullptr;
251 EventSequence nextSequence_ = EventSequence(1);
252 std::vector<const GameEvent*> batch_;
253};
254
263public:
264 // The stream owns move-only members (std::vector<std::unique_ptr<EventConsumer>>),
265 // so it is not copyable; dllexport must not instantiate the implicitly declared
266 // copy operations, whose bodies cannot compile. Deleting the copies is a no-op and
267 // the defaulted moves keep the previous implicit behaviour.
268 GameEventLog(const GameEventLog&) = delete;
274
276 GameEventLog() = default;
277
280
286 explicit GameEventLog(eve::UuidEntropySource entropy, eve::UuidClock clock = {});
287
296 [[nodiscard]] eve::Result<EventSequence> append(GameEvent envelope);
297
306 template <class Payload>
309 GameEvent envelope, const Payload& payload,
310 const std::function<eve::Result<std::string>(const Payload&)>& encode) {
311 if (!encode) {
314 eve::DiagnosticCode::InvalidArgument, "event payload encoder must not be empty"));
315 }
316 auto encoded = encode(payload);
317 if (!encoded) return eve::Result<EventSequence>::failure(encoded.status());
318 envelope.payload = std::move(encoded).takeValue();
320 return append(std::move(envelope));
321 }
322
332 [[nodiscard]] const GameEvent* find(EventSequence sequence) const;
340 [[nodiscard]] const GameEvent* find(uint64_t sequence) const;
342 int queryType(const std::string& type);
344 int querySource(const std::string& source);
346 int querySubject(const std::string& subject);
348 int querySequence(EventSequence firstSequence);
350 int querySequence(uint64_t firstSequence);
352 const GameEvent* queryAt(int index) const;
354 int size() const;
356 const GameEvent* at(int index) const;
358 [[nodiscard]] EventConsumer* newConsumer(EventSequence firstSequence = EventSequence(1));
360 [[nodiscard]] EventConsumer* newConsumer(uint64_t firstSequence);
362 void clearBefore(EventSequence firstSequence);
364 void clearBefore(uint64_t firstSequence);
366 void clear();
368 void reset();
370 std::string snapshotJson() const;
377 [[nodiscard]] eve::Result<void> restore(std::string_view json);
378
380 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
387 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
388 const eve::SnapshotHashProvider& hashProvider);
390 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
392 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
393 const eve::SnapshotHashProvider& hashProvider);
394
395private:
396 friend class EventConsumer;
397 [[nodiscard]] EventSequence oldestSequence() const;
398
399 std::optional<eve::UuidV7Generator> eventIdGenerator_;
400 eve::PersistentId instanceId_;
403 EventSequence nextSequence_ = EventSequence(1);
404 std::deque<GameEvent> events_;
405 std::vector<const GameEvent*> query_;
406 std::vector<std::unique_ptr<EventConsumer>> consumers_;
407};
408
410class GameEventModule : public Module {
411public:
414 GameEventModule() = default;
416 ~GameEventModule() override = default;
417
427 [[nodiscard("event stream ownership must be retained or explicitly handled")]] static GameEventLog* newLog();
428
429private:
430 std::vector<std::unique_ptr<GameEventLog>> streams_;
431};
432
433} // namespace eve::game_event
Value::Object payload
int subject
Definition AnimSmr.cpp:163
int instanceId
Stream-local event ordering value.
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
Strong identity values shared by persistence, content and logical-name APIs.
std::function< Numbers(b3JointId)> read
std::array< float, 3 > position
Poco::JSON::Object::Ptr result
Snapshot snapshot
std::unique_ptr< gpgpu::Sequence > sequence
Definition OnnxGpgpu.cpp:43
Object::Ptr value
Move-only, checked operation results for the common layer.
Monotonic state revision for concurrency and change checks.
Persistent data-contract version identity.
std::string_view source
std::string kind
Versioned, integrity-checked persistence envelopes.
Common time values and an injectable deterministic simulation clock.
float size
Definition TreeMesh.cpp:158
std::size_t at
uint32_t index
UUIDv7 generation kept separate from lightweight identity values.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Human-readable, scoped identifier in the form namespace:name.
Definition Identity.h:414
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Id128 public API.
Definition Identity.h:111
std::string format() const
Formats the ID in lower-case canonical UUID text.
Definition Identity.h:172
static constexpr Id128 nil() noexcept
Returns the all-zero nil value.
Definition Identity.h:141
static constexpr StrongUint64 zero() noexcept
Returns the zero value for this strong type.
Tagged direct causation reference to an event or command.
Definition GameEvent.h:97
static CausationRef fromEventId(EventId value)
Creates a causation reference to an event.
Definition GameEvent.h:106
CausationRef()=default
Constructs an absent causation reference.
bool isCanonical() const noexcept
Returns whether this reference uses the canonical ID form.
Definition GameEvent.h:137
std::string format() const
Returns the stable UUID or compatibility text projection.
Definition GameEvent.h:140
friend bool operator==(const CausationRef &, const CausationRef &) noexcept=default
static CausationRef fromLegacy(std::string value)
Creates a compatibility-only arbitrary string causation.
Definition GameEvent.h:126
Kind kind() const noexcept
Returns the tagged causation kind.
Definition GameEvent.h:134
static CausationRef fromCommandId(CommandId value)
Creates a causation reference to a command.
Definition GameEvent.h:116
Strong business-workflow correlation identity.
Definition GameEvent.h:50
Kind kind() const noexcept
Returns the tagged correlation kind.
Definition GameEvent.h:75
static CorrelationId fromLegacy(std::string value)
Creates a compatibility-only arbitrary string correlation.
Definition GameEvent.h:67
bool isCanonical() const noexcept
Returns whether this reference uses the canonical ID form.
Definition GameEvent.h:78
std::string format() const
Returns the stable ID or compatibility text projection.
Definition GameEvent.h:81
CorrelationId()=default
Constructs an absent correlation reference.
friend bool operator==(const CorrelationId &, const CorrelationId &) noexcept=default
static CorrelationId fromEventId(EventId value)
Creates a canonical correlation ID from an EventId.
Definition GameEvent.h:59
Independent sequence cursor used to consume an GameEventLog in batches.
Definition GameEvent.h:225
Deterministic in-memory stream of topic-neutral event envelopes.
Definition GameEvent.h:262
GameEventLog & operator=(GameEventLog &&)=default
Operator =.
GameEventLog()=default
Creates a stream without an implicit entropy source.
GameEventLog(eve::PersistentId instanceId)
Creates a stream with a persistent snapshot identity.
Definition GameEvent.h:279
GameEventLog(GameEventLog &&)=default
Game event log.
eve::Result< EventSequence > appendTyped(GameEvent envelope, const Payload &payload, const std::function< eve::Result< std::string >(const Payload &)> &encode)
Encodes a strongly typed domain payload and appends its envelope.
Definition GameEvent.h:308
GameEventLog(const GameEventLog &)=delete
GameEventLog & operator=(const GameEventLog &)=delete
Script module factory for generic GameEventLog objects.
Definition GameEvent.h:410
~GameEventModule() override=default
Releases GameEventModule resources.
static GameEventLog * newLog()
Allocates a module-owned event stream. @ownership Borrowed from the manager-owned GameEvent module; t...
GameEventModule()=default
Constructs a GameEventModule.
std::ostream & operator<<(std::ostream &stream, CorrelationId::Kind kind)
Writes a correlation representation tag for diagnostics.
Definition GameEvent.h:155
std::function< std::chrono::system_clock::time_point()> UuidClock
Clock callback used by the injectable UUIDv7 generator.
Definition UuidV7.h:29
StrongUuid< detail::CommandIdTag > CommandId
Stable command identity for causal references.
Definition Identity.h:287
detail::StrongUint64< detail::SchemaVersionTag > SchemaVersion
Persistent data-format version; not a runtime replacement generation.
std::function< bool(std::span< std::uint8_t > bytes)> UuidEntropySource
Entropy callback used by UUIDv7 generation.
Definition UuidV7.h:26
detail::StrongUint64< detail::SimulationTickTag > SimulationTick
Deterministic simulation time step; it is not wall-clock time.
Definition Time.h:31
StrongUuid< detail::EventIdTag > EventId
Stable event identity for event envelopes and causal references.
Definition Identity.h:284
detail::StrongUint64< detail::EventSequenceTag > EventSequence
Stream-local event ordering value; it is not a global event identity.
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
Envelope metadata shared by event domains.
Definition GameEvent.h:184
std::string source
Stable source reference, owned by this envelope.
Definition GameEvent.h:192
std::string type
Compatibility event name and diagnostic topic.
Definition GameEvent.h:190
EventSequence sequence
Sequence assigned by the owning stream; it is never global identity.
Definition GameEvent.h:188
EventId eventId
Stable event identity; nil is allowed only for legacy transient events.
Definition GameEvent.h:186
std::string payload
Canonical serialized payload at the stream persistence boundary.
Definition GameEvent.h:208
SchemaVersion schemaVersion
Version of the payload schema, not a stream or runtime generation.
Definition GameEvent.h:202
SchemaId schemaId
Logical schema identifier for the typed payload codec.
Definition GameEvent.h:200
uint32_t flags
Domain-defined bit flags; their meanings belong to the schema.
Definition GameEvent.h:206
SimulationTick tick
Deterministic simulation time at which the event was emitted.
Definition GameEvent.h:204
std::string subject
Stable subject reference, owned by this envelope.
Definition GameEvent.h:194
CorrelationId correlation
Business workflow correlation reference.
Definition GameEvent.h:198
CausationRef causation
Direct causation reference, normally an event or command ID.
Definition GameEvent.h:196
Strongly typed event value used before serialization into a stream.
Definition GameEvent.h:217