载入中...
搜索中...
未找到
PlatformEvent.cpp
浏览该文件的文档.
3
4#include "common/Assert.h"
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8namespace eve::platform_event {
9
10Message::Message(const std::string& name, const std::vector<Variant>& vargs) : name(name), args(vargs) {}
12
14
15void PlatformEvent::expose(ssq::Table& table) {
16 auto cls = table.addClass(name, PlatformEvent::create, false);
17 expose(cls);
18}
19
20void PlatformEvent::expose(ssq::Class& cls) {
21 cls.addFunc("getName", &PlatformEvent::getName);
22 cls.addFunc("pump", &PlatformEvent::pump);
23 cls.addFunc("poll", &PlatformEvent::pollName);
24 cls.addFunc("pollData", &PlatformEvent::pollData);
25 cls.addFunc("getLastData", &PlatformEvent::getLastData);
26 cls.addFunc("pushData", &PlatformEvent::pushData);
27}
28
30
32 return pollObservers_.subscribe(std::move(callback));
33}
34
35void PlatformEvent::push(Message* msg) { push(std::unique_ptr<Message>(msg)); }
36
37void PlatformEvent::push(std::unique_ptr<Message> msg) {
38 EV_PARAM_CHECK(msg.get() != nullptr, "event message must not be null");
39 if (!msg) return;
40 {
41 std::lock_guard<std::mutex> lock(queueMu_);
42 queue.push(std::move(msg));
43 }
45}
46
47void PlatformEvent::pushData(std::string eventName, std::string data) {
48 std::vector<Variant> args;
49 if (!data.empty()) args.push_back(Variant::makeString(std::move(data)));
50 push(std::make_unique<Message>(std::move(eventName), args));
51}
52
53Message* PlatformEvent::poll() { return pollOwned().release(); }
54
55std::unique_ptr<Message> PlatformEvent::pollOwned() {
56 std::unique_ptr<Message> msg;
57 {
58 std::lock_guard<std::mutex> lock(queueMu_);
59 if (queue.empty()) return nullptr;
60 msg = std::move(queue.front());
61 queue.pop();
62 }
63 // Report consumption outside the lock so observers may re-enter push/poll.
64 // The queue mutation is already committed; callback exceptions are
65 // contained and counted instead of making a successful poll look failed.
66 if (msg) {
67 static_cast<void>(pollObservers_.notifyChecked([this]() noexcept { ++pollObserverFailures_; }, *msg));
68 }
69 return msg;
70}
71
73 auto msg = pollOwned();
74 lastData_.clear();
75 if (!msg) return {};
76 for (const auto& a : msg->args) {
77 if (a.type == Variant::Type::String) {
78 lastData_ = a.s;
79 break;
80 }
81 }
82 std::string n = msg->name;
83 return n;
84}
85
86std::string PlatformEvent::getLastData() const { return lastData_; }
87
89 auto msg = pollOwned();
90 lastData_.clear();
91 if (!msg) return {};
92 std::string data;
93 for (const auto& a : msg->args) {
94 if (a.type == Variant::Type::String) {
95 data = a.s;
96 break;
97 }
98 }
99 lastData_ = data;
100 return data;
101}
102
104 std::lock_guard<std::mutex> lock(queueMu_);
105 while (!queue.empty()) queue.pop();
106}
107
108} // namespace eve::platform_event
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
HSQOBJECT cls
Definition ECS.cpp:21
glm::vec3 n
Definition Grass.cpp:63
std::string name
MeleePoint3 a
Definition MeleeHit.cpp:40
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
ViewPreparation callback
virtual std::string getName() const =0
Returns the name.
Move-only RAII token that owns one observer registration.
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
Message(const std::string &name, const std::vector< Variant > &vargs={})
Creates an event message.
EVENGINE_API_FOUNDATION public API.
std::unique_ptr< Message > pollOwned()
Pops the oldest message into an RAII handle, or returns null.
std::string pollName()
Script helper: pops one message and returns its name, or "" if none.
eve::Subscription subscribePoll(std::function< void(const Message &)> callback)
Subscribe to every message consumed by poll/pollOwned.
virtual void clear()
Drops all queued messages and frees them.
virtual ~PlatformEvent()
Platform event.
void pushData(std::string name, std::string data="")
Script helper: pushes an event with an optional string payload.
Message * poll()
Pops the oldest message, or nullptr if the queue is empty. @ownership Ownership transfers to the call...
virtual void pump()=0
Platform hook: pumps the native event queue into this module.
std::queue< std::unique_ptr< Message > > queue
std::string pollData()
Pops one message and returns its first string arg (or ""). Name is discarded — use pollName when you ...
eve::Observer< Message > pollObservers_
std::string getLastData() const
First string arg of the message most recently returned by pollName/pollData.
void push(Message *msg)
Queues a message. Thread-safe: workers may push completions for the main loop to poll.
virtual void wakeWaiters()
Wake a platform-specific blocking wait after a message is queued.
Event public API.
Definition Event.h:15
static Variant makeString(std::string v)
Constructs a string variant (takes ownership of the value).