no message

master
zcy 2021-12-15 16:19:59 +08:00
parent 9c707cd6bd
commit 4374d4f2aa
16 changed files with 285 additions and 216 deletions

View File

@ -54,8 +54,6 @@ message( "copy net \n")
file(GLOB NET ${PROJECT_SOURCE_DIR}/general/src/net/*.h)
file(COPY ${NET} DESTINATION ${LIBRARY_OUTPUT_PATH}/inc/net/
FILE_PERMISSIONS OWNER_READ OWNER_WRITE GROUP_WRITE GROUP_READ WORLD_READ)
message( "copy threadsafe library \n")
file(GLOB THREADSAFECODE ${PROJECT_SOURCE_DIR}/general/src/threadsafe/*.hpp)
message("threadsafe " ${THREADSAFECODE})

View File

@ -18,6 +18,7 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# using Intel C++
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
add_compile_options(/D_CRT_SECURE_NO_WARNINGS_CRT_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS)
endif()
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/obj)
@ -27,6 +28,7 @@ INCLUDE_DIRECTORIES (./)
INCLUDE_DIRECTORIES (inc)
INCLUDE_DIRECTORIES (encrypt)
INCLUDE_DIRECTORIES (pattern)
include_directories(third/include)
aux_source_directory(src DIRSRCS)
aux_source_directory(src/pattern PaternSrc)

View File

@ -12,33 +12,30 @@
#include <iostream>
using namespace std;
typedef enum Mode{
Mode_Daily, // 每天保存一次日志
MODE_Monthly, // 每个月保存一次日志
MODE_Weekly // 每周保存一次日志
}ESaveMode;
namespace Loger{
class Loger {
private:
string mCurrentPath;
FILE *mFile; // 日志文件
ESaveMode mMode; // 工作模式
string mCurrentDate; // 当天
int error;
typedef enum Mode{
Mode_Daily, // 每天保存一次日志
MODE_Monthly, // 每个月保存一次日志
MODE_Weekly // 每周保存一次日志
}ESaveMode;
typedef class _C_Loger {
private:
string mCurrentPath;
FILE *mFile; // 日志文件
ESaveMode mMode; // 工作模式
string mCurrentDate; // 当天
int error;
public:
_C_Loger(FILE *p);
_C_Loger(string path);
#define DEBUG_FILE_POSITION __FILE__,__LINE__
int Debug(string,string,int);
int Log();
int LogFile();
void operator+(const string&);
void operator<<(const string&);
}Loger;
}
public:
Loger(FILE *p);
Loger(string path);
#define DEBUG_FILE_POSITION __FILE__,__LINE__
int Debug(string,string,int);
int Log();
int LogFile();
void operator+(const string&);
void operator<<(const string&);
}Loger;
#endif //CPP11FEATURETEST_LOGER_H

View File

@ -1,94 +0,0 @@
#include <vector>
#include <stdint.h>
using namespace std;
template<typename T>
class RingBuffer{
public:
enum Error{
Error = -10,
ERROR_FULL = -11,
ERROR_NO_ENOUGH_WRITE = -12,
};
RingBuffer(uint16_t len) {
mBuffer = new T[len];
mHead = mTail = mBuffer;
if (mBuffer == nullptr){
this->mLen = 0;
}
mLen = len;
};
uint16_t ReadableCnt(){
if (mHead == mTail) return 0;
if (mHead < mTail)
return mTail - mHead;
return mLen - (mHead - mTail);
}
uint16_t WriteableCnt(){
return mLen - this->ReadableCnt();
}
int16_t Read(T *data, uint16_t count)
{
//assert(rb != NULL);
//assert(data != NULL);
if (mHead< mTail) // 头部小于尾部,正常情况下
{
int copy_sz = min(count, this->ReadableCnt());
memcpy(data, mHead, copy_sz * sizeof(T));
mHead += copy_sz;
return copy_sz;
}
else // 头部大于尾部
{
if (count < mLen -(mHead - mBuffer)) // (rb->rb_head - rb->rb_buff) 是从头部到数组物理结束的长度如果小于说明不需要读取数组头部的只需要copy一次
{
int copy_sz = count*sizeof(T);
memcpy(data, mHead, copy_sz*sizeof(T));
mHead += copy_sz;
return copy_sz;
}
else
{
//反之需要copy两次因为数组本身不是有环的
int copy_sz = mLen - (mHead - mBuffer);
memcpy(data, mHead, copy_sz * sizeof(T));
mHead = mBuffer;
copy_sz += Read( (T*)data + copy_sz*sizeof(T), count - copy_sz);
return copy_sz;
}
}
}
int16_t Write(const T *in,uint16_t len){
if (len >= WriteableCnt())
return ERROR_NO_ENOUGH_WRITE;
if (mHead <= mTail) // 头部小于尾部
{
int tail_avail_sz = mLen - (mTail - this->mBuffer);
if (len <= tail_avail_sz) // 是否需要循环到0开始写
{
memcpy(mTail, in, len*sizeof(T));
mTail += len;
if (mTail == mBuffer + mLen) // 刚好相等的情况
mTail = mBuffer;
return len;
}
else
{
memcpy(mTail, in, tail_avail_sz*sizeof(T));
mTail = mBuffer;
return tail_avail_sz + Write( (T*)in + tail_avail_sz* sizeof(T), (len - tail_avail_sz));
}
}
else
{
memcpy(mTail, in, len*sizeof(T)); // 头部大于尾部
mTail += len;
return len;
}
}
private:
T *mBuffer;
T* mHead ;
T* mTail;
uint16_t mLen;
};

View File

@ -18,6 +18,7 @@ void itoa(int i,char* string)
*string='\0';
}
#endif
int PrintDumpObjvoid (void *dst,int rowNum,int num,bool ifAsii){
char out [2048] = {0};
int row = num / rowNum;

Binary file not shown.

View File

@ -258,31 +258,20 @@ static BOOL KillProcessByName(const TCHAR *lpszProcessName)
while (flag != 0)
{
printf("kill process find %s\r\n", processInfo.szExeFile);
if (strcmp(processInfo.szExeFile, lpszProcessName) == 0)
{
// Terminate the process.
pid = processInfo.th32ProcessID;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid);
printf("kill process pid %d\r\n", pid);
if (TerminateProcess(hProcess, 0) != TRUE)
{ // Failed to terminate it.
retval = FALSE;
break;
}
}
flag = Process32Next(hSnapshot, &processInfo);
} // while (flag != 0)
@ -552,84 +541,52 @@ int DaemonizeMonitor::StartMonitor()
for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++)
{
bool found = false;
if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes))
{
Proc_count = Retn_bytes / sizeof(DWORD);
SetProcessPrivilege("SeDebugPrivilege", 1);
for (i = 0; i < Proc_count; i++)
{
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]);
if (hProcess != NULL)
{
EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2);
GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName));
// printf("PID=%d Path=%s\n", Proc_pid[i], szModName);
if (itr->second == Proc_pid[i])
{
found = true;
break;
}
}
CloseHandle(hProcess);
}
SetProcessPrivilege("SeDebugPrivilege", 0);
if (!found)
{
// 没找到该应用实例就重启应
auto it = m_path_process.begin();
while (it != m_path_process.end())
{
if (*it == itr->first)
{
it = m_path_process.erase(it);
}
else
it++;
}
for (auto it = m_path_process.begin(); it != m_path_process.end(); it++)
{
std::cout << *it << std::endl;
}
this->AddNewProcess(itr->first);
}
}
}
}
return 0;
#endif
#ifdef linux
@ -673,53 +630,36 @@ int DaemonizeMonitor::StartMonitor()
}
}
}
#endif
}
int DaemonizeMonitor::StopMonitor(string)
{
#ifdef _WIN32
return 0;
#endif
}
int DaemonizeMonitor::StopProcess(string path)
{
if (path == "")
{
return -1;
}
#ifdef _WIN32
if (m_running_pid.find(path) != m_running_pid.end())
{
DWORD pid = m_running_pid.at(path);
m_running_pid.erase(path);
if (KillProcess(pid))
return 0;
else
{
return -1;
}
}
return 0;
#endif
#ifdef linux
#endif
}

View File

@ -0,0 +1,129 @@
#include "threadpool.h"
unsigned int CoreCount()
{
unsigned count = 1; // 至少一个
#if defined (LINUX)
count = sysconf(_SC_NPROCESSORS_CONF);
#elif defined (WINDOWS)
SYSTEM_INFO si;
GetSystemInfo(&si);
count = si.dwNumberOfProcessors;
#endif
return count;
}
namespace general{
CThreadPool::CThreadPool(int num) {
this->mThreadCnt = num;
mStarted = false;
}
int CThreadPool::AddTask(Task *t) {
if ( nullptr == t){
return -1;
}
if(!mStoping){
{
std::unique_lock<std::mutex> lck(this->mMutex);
this->mTasks.push(t);
}
mQueueAvaliableCondition.notify_one();
return 0;
}else{
return -2;
}
}
void CThreadPool::Start() {
mStarted = true;
mThreads.reserve(this->mThreadCnt);
for (int i = 0; i < mThreadCnt; i++)
{
mThreads.emplace_back(new std::thread(&ThreadPool::Process, this, i));
}
}
Task *CThreadPool::PopTask() {
std::unique_lock<std::mutex> lk(this->mMutex);
while (mTasks.empty() && mStarted)
{
mQueueAvaliableCondition.wait(lk);
}
if(!mStarted){
return nullptr;
}
Task *ret = this->mTasks.front();
this->mTasks.pop();
return ret;
}
void CThreadPool::Process(int id) {
while (mStarted)
{
Task *task = PopTask();
if (nullptr == task)
{
continue;
}
else
{
task->Run();
task->OnDone();
}
}
}
void CThreadPool::Stop() {
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
mQueueAvaliableCondition.notify_all();
}
for (auto &th : mThreads)
{
th->join();
}
mStarted = false;
}
void CThreadPool::StopAll() {
{
while(this->mTasks.size() > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mStarted = false;
mQueueAvaliableCondition.notify_all();
}
for (auto &th : mThreads)
{
th->join();
}
mStarted = false;
}
bool CThreadPool::Started(){
return this->mStarted;
}
CThreadPool::~CThreadPool(){
if (this->mStarted)
{
std::cout << "desruction" << std::endl;
this->mStarted = false;
{
std::lock_guard<std::mutex> lk(mMutex);
mStarted = false;
mQueueAvaliableCondition.notify_all();
}
for (size_t i = 0; i != this->mThreads.size(); ++i)
{
if (mThreads[i]->joinable())
{
mThreads[i]->join();
}
}
}
}
}

View File

@ -0,0 +1,96 @@
/*
* @Author: your name
* @Date: 2021-10-09 10:03:45
* @LastEditTime: 2021-11-25 15:26:02
* @LastEditors: Please set LastEditors
* @Description: koroFileHeader : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \cpp11\threadpool.h
*/
#pragma once
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <queue>
#include <condition_variable>
#include <iostream>
#include <functional>
#include <utility>
#include "threadpool.h"
extern "C"{
#if !defined (_WIN32) && !defined (_WIN64)
#define LINUX
#include <sysconf.h>
#else
#define WINDOWS
#include <windows.h>
#endif
unsigned int CoreCount();
}
namespace general{
class CThreadPool;
enum PRIORITY {
MIN = 1, NORMAL = 25, MAX = 50
};
class Task{
public:
Task() {
}
void SetPriority(int priority) {
if (priority>(PRIORITY::MAX))
{
priority = (PRIORITY::MAX);
}
else if (priority>(PRIORITY::MAX))
{
priority = (PRIORITY::MIN);
}
}
virtual void Run() = 0;
virtual void OnDone() = 0;
protected:
int priority_;
};
class Worker{
public:
// The required constructor for this example.
explicit Worker(int& evenCount)
: m_evenCount(evenCount) { }
void operator()(CThreadPool *pool) const {
}
private:
// Default assignment operator to silence warning C4512.
Worker& operator=(const Worker&);
int& m_evenCount;
};
typedef class CThreadPool {
public:
CThreadPool(int num);
~CThreadPool();
int AddTask(Task *);
void Start();
void Stop();
bool Started();
void StopAll();
private:
CThreadPool();
void Process(int id);
uint32_t mThreadCnt;
bool mStarted;
bool mStoping;
Task *PopTask();
std::vector<std::thread*> mThreads;
std::queue<Task *> mTasks;
std::mutex mMutex;
std::condition_variable mQueueAvaliableCondition;
}ThreadPool;
}

View File

@ -3,6 +3,7 @@
//
#include "loger.h"
using namespace Loger;
string getTimeDate()
{
time_t timep;
@ -21,7 +22,7 @@ string getTime()
return string(tmp);
}
int _C_Loger::Debug(string dat,string function,int line){
int Loger::Debug(string dat,string function,int line){
// 还没有过天
if(getTimeDate() == this->mCurrentDate){
string tmp = getTime();
@ -51,7 +52,7 @@ int _C_Loger::Debug(string dat,string function,int line){
return 0;
}
void _C_Loger::operator<<(const string& wb){
void Loger::operator<<(const string& wb){
// 还没有过天
if(getTimeDate() == this->mCurrentDate){
string tmp = getTime();
@ -67,7 +68,6 @@ void _C_Loger::operator<<(const string& wb){
if (this->mFile == nullptr){
this->error = true;
}
string tmp = getTime();
tmp += ":";
tmp += wb;
@ -90,12 +90,13 @@ bool file_existed(string path) {
return true;
}
}
_C_Loger::_C_Loger(FILE *p){
Loger::Loger(FILE *p){
this->mFile = p;
this->mCurrentDate = getTime();
}
_C_Loger::_C_Loger(string path) {
Loger::Loger(string path) {
this->mCurrentDate = getTimeDate();
this->mCurrentPath = path + this->mCurrentDate;
this->mFile = fopen(this->mCurrentPath.c_str(),"a+");

View File

@ -175,8 +175,6 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
}
template<typename T>
int RingBuffer<T>::Take(T *data,uint64_t len){
if(data == nullptr)

View File

@ -118,14 +118,13 @@ public:
return ret;
}
}
uint32_t Length()
{
return m_data.size();
}
private:
std::mutex m_mux;
std::mutex m_mux;
std::condition_variable m_cv;
std::list<T> m_data;
};

View File

@ -60,7 +60,7 @@ public:
/*
* ,false
* */
bool try_pop(value_type& value){
bool try_pop(value_type& value) {
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return false;
@ -71,14 +71,14 @@ public:
/*
*
* */
auto empty() const->decltype(data_queue.empty()) {
auto empty() const->decltype(data_queue.empty()) {s
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
/*
*
* */
auto size() const->decltype(data_queue.size()){
auto size() const->decltype(data_queue.size()) {
std::lock_guard<std::mutex> lk(mut);
return data_queue.size();
}

View File

@ -11,16 +11,30 @@
#include <stdint.h>
template<typename TYP>
class ThreadSafeVector{
class ThreadSafeVector {
public:
typedef typename std::vector<TYP>::iterator iterator;
typedef typename std::vector<TYP>::const_iterator const_iterator;
Val& operator [](uin32_t index)
{
ThreadSafeVector(size_type n){
this->m_data = new vector<TYP>(n);
}
const Val& operator [](uin32_t index)
{
std::unique_lock<std::mutex> lck(m_mutex);
return *m_data[index];
}
const Val& at(uin32_t index)
{
std::unique_lock<std::mutex> lck(m_mutex);
return m_data->at(index);
}
iterator Insert(TYP dat,iterator pos){
std::unique_lock<std::mutex> lck(m_mutex);
*m_data.insert(pos,dat);
}
private:
std::vector<TYP> m_data;
std::vector<TYP> *m_data;
std::mutex m_mutex;
}
}

View File

@ -1,20 +1,7 @@
/*
* @Author: your name
* @Date: 2020-10-11 17:37:54
* @LastEditTime: 2021-07-24 00:46:47
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \generallib\general\src\utils.cpp
*/
//
// Created by 29019 on 2019/5/2.
//
#if __cplusplus >= 201103L
#pragma message("编译器支持c++11 ")
#endif // __cplusplus >= 201103L
#include "utils.h"
#ifdef linux
int itoa(int n ,char * const s,int radix){
@ -73,3 +60,5 @@ inline ENV_COMPILER CurrentEnvCompiler()
#endif
return UNKNOWN;
}

View File

@ -28,7 +28,6 @@ void async_cb(uv_async_t* handle)
printf("async_cb called!\n");
printf("thread id:%lu.\n", id);
//uv_close((uv_handle_t*)&async, close_cb); //如果async没有关闭消息队列是会阻塞的
}
void async_cb2(uv_async_t* handle)
@ -36,7 +35,7 @@ void async_cb2(uv_async_t* handle)
int id = GetCurrentThreadId();
printf("async_cb2 called!\n");
printf("thread id:%lu.\n", id);
//uv_close((uv_handle_t*)&async, close_cb); //如果async没有关闭消息队列是会阻塞的
}