mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Feat: EventBus
This commit is contained in:
1 parent
a64bbb8bb8
commit
49d2499f73
7 files changed
+176
-43
No files matched your search
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "event_type.hpp"
|
||||
|
||||
namespace JuiceAgent::Bytecodes {
|
||||
void register_bytecodes();
|
||||
|
||||
void capture_bytecodes(const EventClassFileLoadHook& e);
|
||||
void patch_bytecodes(const EventClassFileLoadHook& e);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <jni_impl.hpp>
|
||||
|
||||
// ClassFileLoadHook
|
||||
struct EventClassFileLoadHook {
|
||||
jvmtiEnv* jvmti_env;
|
||||
JNIEnv* jni_env;
|
||||
jclass class_being_redefined;
|
||||
jobject loader;
|
||||
const char* name;
|
||||
jobject protection_domain;
|
||||
jint class_data_len;
|
||||
const unsigned char* classbytes;
|
||||
jint* new_class_data_len;
|
||||
unsigned char** new_classbytes;
|
||||
};
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <eventpp/eventdispatcher.h>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <typeindex>
|
||||
|
||||
class EventBus {
|
||||
public:
|
||||
template<typename Event>
|
||||
using Listener = std::function<void(const Event&)>;
|
||||
|
||||
template<typename Event>
|
||||
void subscribe(Listener<Event> listener, int priority = 0) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto& dispatcher = getDispatcher<Event>();
|
||||
dispatcher.appendListener(priority, std::move(listener));
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
void clear() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
dispatchers_.erase(std::type_index(typeid(Event)));
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
void post(const Event& event) {
|
||||
std::shared_ptr<void> dispatcherPtr;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto it = dispatchers_.find(std::type_index(typeid(Event)));
|
||||
if(it == dispatchers_.end()) return;
|
||||
dispatcherPtr = it->second;
|
||||
}
|
||||
|
||||
auto dispatcher = std::static_pointer_cast<
|
||||
eventpp::EventDispatcher<int, void(const Event&)>
|
||||
>(dispatcherPtr);
|
||||
|
||||
dispatcher->dispatch(0, event);
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> dispatchers_;
|
||||
|
||||
template<typename Event>
|
||||
eventpp::EventDispatcher<int, void(const Event&)>& getDispatcher() {
|
||||
auto typeIdx = std::type_index(typeid(Event));
|
||||
auto it = dispatchers_.find(typeIdx);
|
||||
|
||||
if(it == dispatchers_.end()) {
|
||||
auto dispatcher = std::make_shared<
|
||||
eventpp::EventDispatcher<int, void(const Event&)>
|
||||
>();
|
||||
|
||||
dispatchers_[typeIdx] = dispatcher;
|
||||
return *dispatcher;
|
||||
}
|
||||
|
||||
return *std::static_pointer_cast<
|
||||
eventpp::EventDispatcher<int, void(const Event&)>
|
||||
>(it->second);
|
||||
}
|
||||
};
|
||||
@@ -3,12 +3,14 @@
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <eventbus.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
class Agent {
|
||||
private:
|
||||
JavaVM* jvm;
|
||||
jvmtiEnv* jvmti;
|
||||
EventBus eventbus;
|
||||
|
||||
private:
|
||||
Agent() = default;
|
||||
@@ -27,6 +29,7 @@ namespace JuiceAgent {
|
||||
// getters
|
||||
JavaVM* get_jvm() const { return jvm; }
|
||||
jvmtiEnv* get_jvmti() const { return jvmti; }
|
||||
EventBus& get_eventbus() { return eventbus; }
|
||||
|
||||
// setters
|
||||
void set_jvm(JavaVM* jvm) { this->jvm = jvm; }
|
||||
|
||||
Reference in new issue
Block a user