mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
- fill EventPreLoad/EventLoaded struct members instead of leaving them as uninitialized garbage; post PreLoad only after the agent environment (JavaVM/JNIEnv/JVMTI) is set so listeners can query Agent state - store (EventId, Handle) pairs in IService and add unlistenAll() to remove registered listeners on shutdown, preventing dangling callbacks into destroyed services; BytecodeService and JarLoaderService now call it from onShutdown
57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <vector>
|
|
#include <utility>
|
|
#include <type_traits>
|
|
#include <JuiceAgent/Logger.hpp>
|
|
#include <eventbus.hpp>
|
|
|
|
namespace JuiceAgent {
|
|
namespace Services {
|
|
|
|
class IService {
|
|
public:
|
|
virtual ~IService() = default;
|
|
|
|
virtual const char* name() const = 0;
|
|
|
|
virtual bool onInitialize() { return true; }
|
|
|
|
virtual void onShutdown() {}
|
|
|
|
protected:
|
|
template <typename ServiceT, typename EventT>
|
|
void listen(EventId id, void (ServiceT::*handler)(const EventT&)) {
|
|
static_assert(std::is_base_of<IService, ServiceT>::value,
|
|
"ServiceT must derive from IService");
|
|
auto* self = static_cast<ServiceT*>(this);
|
|
handles_.push_back({
|
|
id,
|
|
EventBus::getInstance().appendListener(
|
|
id,
|
|
[self, handler](const void* data) {
|
|
if (data) {
|
|
(self->*handler)(*static_cast<const EventT*>(data));
|
|
}
|
|
}
|
|
)
|
|
});
|
|
}
|
|
|
|
// Remove all listeners registered by this service. Call it in onShutdown()
|
|
// so no callback outlives the service instance.
|
|
void unlistenAll() {
|
|
auto& bus = EventBus::getInstance();
|
|
for (auto& [id, handle] : handles_) {
|
|
bus.removeListener(id, handle);
|
|
}
|
|
handles_.clear();
|
|
}
|
|
|
|
private:
|
|
std::vector<std::pair<EventId, EventBus::Dispatcher::Handle>> handles_;
|
|
};
|
|
|
|
}
|
|
}
|