2022-05-03 03:56:07 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
// Objects for Inter-Process-Communication
|
|
|
|
|
//
|
|
|
|
|
// created on 2022-03-01
|
|
|
|
|
//
|
|
|
|
|
|
2022-07-18 08:56:03 +00:00
|
|
|
|
#if defined(WIN32) || defined(_WIN64)
|
2022-05-03 03:56:07 +00:00
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
|
|
|
|
#define sem_t HANDLE
|
|
|
|
|
#define USB_TIMEOUT_INFINITE -1
|
|
|
|
|
#else
|
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
|
#include <sys/shm.h>
|
2022-06-01 03:04:10 +00:00
|
|
|
|
#include <sys/mman.h>
|
2022-05-03 03:56:07 +00:00
|
|
|
|
#define USB_TIMEOUT_INFINITE 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2022-06-01 03:04:10 +00:00
|
|
|
|
#include <vector>
|
2022-05-03 03:56:07 +00:00
|
|
|
|
#include <mutex>
|
2022-06-01 03:04:10 +00:00
|
|
|
|
#include <memory>
|
2022-05-03 03:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-20 07:21:17 +00:00
|
|
|
|
#include <hginclude/utils.h>
|
|
|
|
|
|
2022-06-28 09:16:03 +00:00
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// memory management ...
|
|
|
|
|
void* allocate_memory(size_t bytes, const char* log_msg = "");
|
|
|
|
|
void free_memory(void* ptr);
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2022-05-03 03:56:07 +00:00
|
|
|
|
class platform_event
|
|
|
|
|
{
|
|
|
|
|
sem_t sem_;
|
|
|
|
|
volatile bool waiting_;
|
|
|
|
|
std::string dbg_info_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
platform_event();
|
|
|
|
|
~platform_event();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool try_wait(void);
|
|
|
|
|
bool wait(unsigned timeout = USB_TIMEOUT_INFINITE/*ms*/); // USB_TIMEOUT_INFINITE is waiting unfinite, true when watied and false for wait timeout
|
|
|
|
|
void notify(void);
|
|
|
|
|
bool is_waiting(void);
|
|
|
|
|
|
|
|
|
|
void set_debug_info(const char* info);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
class do_when_born_and_dead : public refer
|
|
|
|
|
{
|
|
|
|
|
T* obj_;
|
|
|
|
|
void(T::* dead_)(void*);
|
|
|
|
|
void* param_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
do_when_born_and_dead(T* obj, void(T::* born)(void*), void(T::* dead)(void*), void* param)
|
|
|
|
|
: obj_(obj), dead_(dead), param_(param)
|
|
|
|
|
{
|
|
|
|
|
if(born)
|
|
|
|
|
(obj_->*born)(param_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
~do_when_born_and_dead()
|
|
|
|
|
{
|
|
|
|
|
(obj_->*dead_)(param_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// mutex object
|
|
|
|
|
class shared_memory : public refer
|
|
|
|
|
{
|
|
|
|
|
unsigned long long key_;
|
|
|
|
|
void* obj_;
|
|
|
|
|
bool first_;
|
|
|
|
|
size_t bytes_;
|
|
|
|
|
size_t len_;
|
|
|
|
|
|
|
|
|
|
void init(void);
|
|
|
|
|
void clear(void);
|
|
|
|
|
char* get_buf(void);
|
|
|
|
|
void release_buf(void* buf);
|
|
|
|
|
|
2022-07-18 08:56:03 +00:00
|
|
|
|
#if !defined(WIN32) && !defined(_WIN64)
|
2022-05-16 09:26:36 +00:00
|
|
|
|
static std::string get_proc_name_by_pid(pid_t pid);
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-05-03 03:56:07 +00:00
|
|
|
|
public:
|
|
|
|
|
shared_memory(unsigned long long key, size_t size = 1024);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
~shared_memory();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool is_ok(void);
|
|
|
|
|
bool is_first(void);
|
|
|
|
|
std::string read(void);
|
|
|
|
|
int write(const char* data, size_t len);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// buffer
|
2022-07-18 08:56:03 +00:00
|
|
|
|
#if defined(WIN32) || defined(_WIN64)
|
2022-06-06 04:03:24 +00:00
|
|
|
|
#define HANDLE_NAME HANDLE
|
|
|
|
|
#define INVALID_HANDLE_NAME NULL
|
2022-06-01 03:04:10 +00:00
|
|
|
|
#else
|
2022-06-06 04:03:24 +00:00
|
|
|
|
#define HANDLE_NAME int
|
|
|
|
|
#define INVALID_HANDLE_NAME -1
|
2022-06-01 03:04:10 +00:00
|
|
|
|
#endif
|
2022-06-06 04:03:24 +00:00
|
|
|
|
class tiny_file_map
|
|
|
|
|
{
|
|
|
|
|
unsigned int size_;
|
|
|
|
|
unsigned int page_size_;
|
|
|
|
|
HANDLE_NAME map_;
|
2022-06-01 03:04:10 +00:00
|
|
|
|
unsigned char *buf_;
|
|
|
|
|
std::string file_;
|
|
|
|
|
bool keep_f_;
|
2022-06-06 04:03:24 +00:00
|
|
|
|
unsigned int map_off_;
|
|
|
|
|
unsigned int map_bytes_;
|
2022-06-01 03:04:10 +00:00
|
|
|
|
|
2022-06-06 04:03:24 +00:00
|
|
|
|
int map_to_mem(unsigned int off = 0);
|
|
|
|
|
|
2022-06-01 03:04:10 +00:00
|
|
|
|
public:
|
|
|
|
|
tiny_file_map();
|
|
|
|
|
~tiny_file_map();
|
|
|
|
|
|
2022-06-06 04:03:24 +00:00
|
|
|
|
static HANDLE_NAME open_file_for_mapping(const char* file, unsigned* bytes, bool create);
|
|
|
|
|
static void close_handle_name(HANDLE_NAME h);
|
|
|
|
|
static void* sys_map_api(HANDLE_NAME h, int access, unsigned int off, unsigned size, int* err);
|
|
|
|
|
static void sys_unmap_api(void* buf, size_t size = 0);
|
|
|
|
|
|
2022-06-01 03:04:10 +00:00
|
|
|
|
public:
|
2022-06-06 04:03:24 +00:00
|
|
|
|
int open(const char* file, bool existing = true, unsigned int size = 0);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
void close(void);
|
|
|
|
|
void keep_file(bool keep);
|
2022-06-06 04:03:24 +00:00
|
|
|
|
unsigned char* mapping_buffer(unsigned int off, unsigned int* bytes);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
std::string file(void);
|
|
|
|
|
unsigned int size(void);
|
|
|
|
|
|
|
|
|
|
// mapping if unmapped; or unmapping if mapped
|
|
|
|
|
bool swap(void);
|
|
|
|
|
};
|
|
|
|
|
class tiny_buffer
|
|
|
|
|
{
|
2022-07-23 05:42:34 +00:00
|
|
|
|
unsigned int size_;
|
|
|
|
|
unsigned char *buf_;
|
|
|
|
|
tiny_file_map fmap_;
|
|
|
|
|
int img_statu_;
|
2022-06-01 03:04:10 +00:00
|
|
|
|
|
|
|
|
|
void init(const char* tmp_path, const char* name_leading, const char* ext, unsigned int uniq_id);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
tiny_buffer(unsigned int size, const char* tmp_path, const char* name_leading, const char* ext, unsigned int uniq_id);
|
|
|
|
|
tiny_buffer(const char* src_file);
|
|
|
|
|
~tiny_buffer();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
unsigned int size(void);
|
2022-06-06 04:03:24 +00:00
|
|
|
|
unsigned char* data(unsigned int off, unsigned int* bytes/*[in] - need bytes, [out] - real bytes*/);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
void keep_file(bool keep);
|
|
|
|
|
std::string file(void);
|
|
|
|
|
|
|
|
|
|
// mapping if unmapped; or unmapping if mapped
|
|
|
|
|
bool swap(void);
|
2022-07-19 09:12:06 +00:00
|
|
|
|
int to_file(const char* file);
|
2022-07-23 05:42:34 +00:00
|
|
|
|
|
|
|
|
|
void set_image_statu(int statu);
|
|
|
|
|
int get_image_statu(void);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct _img_header
|
|
|
|
|
{
|
|
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
int bits;
|
|
|
|
|
int channels;
|
|
|
|
|
int line_bytes;
|
|
|
|
|
unsigned bytes;
|
2023-01-28 07:19:10 +00:00
|
|
|
|
uint32_t src_id;
|
2022-06-01 03:04:10 +00:00
|
|
|
|
}IMH;
|
|
|
|
|
typedef struct _img
|
|
|
|
|
{
|
|
|
|
|
IMH header;
|
|
|
|
|
unsigned offset;
|
|
|
|
|
std::shared_ptr<tiny_buffer> data;
|
|
|
|
|
}IMGDT;
|
|
|
|
|
class final_img_queue
|
|
|
|
|
{
|
|
|
|
|
mutable std::mutex lock_;
|
|
|
|
|
std::vector<IMGDT> queue_;
|
2022-11-26 02:00:40 +00:00
|
|
|
|
long long mem_usage_;
|
2022-06-01 03:04:10 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
final_img_queue();
|
|
|
|
|
~final_img_queue();
|
|
|
|
|
|
|
|
|
|
public:
|
2022-11-26 02:00:40 +00:00
|
|
|
|
unsigned long long mem_usage(void);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
size_t size(void);
|
|
|
|
|
void clear(void);
|
|
|
|
|
bool put(int w, int h, int bpp, int channels, int line_bytes, void* data, unsigned bytes
|
2023-01-28 07:19:10 +00:00
|
|
|
|
, const char* tmp_path, const char* name_leading, const char* ext, int ind, uint32_t id);
|
2022-06-01 03:04:10 +00:00
|
|
|
|
bool front(IMH* header);
|
|
|
|
|
void fetch_front(void* buf, int* len, bool* over);
|
2022-05-03 03:56:07 +00:00
|
|
|
|
};
|