code_scanner/common/event_monitor.h

71 lines
1.7 KiB
C
Raw Normal View History

2022-12-05 08:03:17 +00:00
#pragma once
// event monitor
//
// created on 2022-11-29
//
#include "referer.h"
#include <thread>
#include <memory>
typedef std::shared_ptr<std::thread> THREAD_PTR;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// object event_handler
//
// derived from 'event_handler' if your class will handle some events of drived by events
//
class event_handler : public refer
{
protected:
event_handler();
virtual ~event_handler();
public:
enum
{
EVENT_NONE = 0,
EVENT_READ, // indicates that the 'fd' is readable, you should perform 'read' if data was null
EVENT_WRITE, // indicates that the 'fd' is writeable, data in 'data' has sent 'data_len' bytes, in async-write only
};
virtual int32_t on_event(int32_t ev, void* data, size_t data_len) = 0;
virtual int32_t get_fd(void) = 0;
};
class parent_holder : public refer
{
protected:
parent_holder();
virtual ~parent_holder();
public:
virtual int32_t stop(void) = 0; // stop work and release parent ptr
};
// event_monitor to manage an event-driven model, this will trigger EVENT_READ/EVENT_WRITE events to 'handler'
//
class event_monitor : public refer
{
protected:
std::string desc_;
protected:
event_monitor(const char* desc);
virtual ~event_monitor();
public:
virtual int32_t start(int32_t threads = 1) = 0;
virtual int32_t stop(void) = 0;
virtual int32_t add_fd(event_handler* handler) = 0;
virtual int32_t remove_fd(event_handler* handler) = 0;
enum
{
EV_TYPE_POLL = 1,
EV_TYPE_EPOLL,
};
static event_monitor* create(const char* desc, int32_t type = EV_TYPE_EPOLL);
};