code_scanner/common/event_monitor.h

113 lines
3.4 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 "packet.h"
2022-12-05 08:03:17 +00:00
#include <thread>
#include <memory>
typedef std::shared_ptr<std::thread> THREAD_PTR;
2022-12-28 02:40:59 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// event definition ...
enum scanner_event
{
SCANNER_EVENT_NONE = 0,
// 1 - IPC
SCANNER_EVENT_IPC_DATA_RECEIVED = 1,
SCANNER_EVENT_IPC_DATA_SENT,
// 2 - image-collector
SCANNER_EVENT_COLLECTOR_WORKING = 100, // on_event(, nullptr, 0), the first message after start success
// normal image, double feeding image, jammed image, stapled imge, size-check error image ...
2022-12-28 02:54:59 +00:00
SCANNER_EVENT_COLLECTOR_IMG_DATA, // on_event(, (LPIMGD)data, bytes of data), image data transfer, buffer can be re-used when return
2022-12-28 02:40:59 +00:00
SCANNER_EVENT_COLLECTOR_PAPER_ON, // on_event(, (bool*)paper_on, (bool)local_display)
SCANNER_EVENT_COLLECTOR_COVER_OPENNED, // on_event(, (bool*)openned, (bool)local_display)
SCANNER_EVENT_COLLECTOR_SLEEPPING, // on_event(, (bool*)sleepping, (bool)local_display)
SCANNER_EVENT_COLLECTOR_ERROR, // on_event(, (char*)err-msg, (bool)local_display), used when unknown fatal error occurs!
SCANNER_EVENT_COLLECTOR_STOPPED, // on_event(, (char*)err-msg, (size_t)err-code), the last message after start success
// 3 - image-process
SCANNER_EVENT_IMAGE_PROC_FINAL_BUF = 200, // on_event(, (void**)buf, (size_t)len), get buffer for store the final image
2022-12-31 03:38:27 +00:00
SCANNER_EVENT_IMAGE_PROC_OK, // on_event(, )
2022-12-28 02:40:59 +00:00
SCANNER_EVENT_IMAGE_PROC_ERR,
// 4 - resource
SCANNER_EVENT_RESOURCE_LOW_MEM = 300, // on_event(, (bool*)low_mem, (bool)local_display)
SCANNER_EVENT_RESOURCE_LOW_DISK, // on_event(, (bool*)low_disk, (bool)local_display)
SCANNER_EVENT_RESOURCE_HIGH_CPU, // on_event(, (bool*)high_cpu, (bool)local_display)
2022-12-28 02:40:59 +00:00
};
2022-12-28 02:54:59 +00:00
typedef struct _img_data
2022-12-28 02:40:59 +00:00
{
LPPACKIMAGE info;
uint8_t* data;
2022-12-28 02:54:59 +00:00
}IMGD, *LPIMGD;
2022-12-28 02:40:59 +00:00
2022-12-05 08:03:17 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:
2022-12-28 02:40:59 +00:00
virtual int32_t on_event(int32_t ev, void* data, size_t data_len) = 0;
virtual int32_t get_fd(void) = 0;
2022-12-05 08:03:17 +00:00
enum
{
2022-12-28 02:40:59 +00:00
EVENT_READ = 0,
EVENT_WRITE,
2022-12-05 08:03:17 +00:00
};
};
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);
};