init
This commit is contained in:
241
Gateway/common/utils/fdop.cc
Normal file
241
Gateway/common/utils/fdop.cc
Normal file
@@ -0,0 +1,241 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "os_def.h"
|
||||
#include "_ast.h"
|
||||
#include "share_util.h"
|
||||
#include "fdop.h"
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
using namespace FDOP;
|
||||
|
||||
bool FDOP::FileExists(const char* sFilePath)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
DWORD dwAttr = GetFileAttributes(sFilePath);
|
||||
|
||||
if (dwAttr == (DWORD) - 1)
|
||||
return false;
|
||||
else return true;
|
||||
|
||||
#else
|
||||
|
||||
if (!access(sFilePath, F_OK))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool FDOP::DeepCreateDirectory(const char* sDirPath)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
char sPath[4096];
|
||||
LPTSTR sPathPtr = sPath;
|
||||
size_t dwNameLen, dwBufLen = ArrayCount(sPath) - 1;
|
||||
DWORD dwAttr;
|
||||
|
||||
while (true)
|
||||
{
|
||||
dwNameLen = ExtractTopDirectoryName(sDirPath, &sDirPath, sPathPtr, dwBufLen);
|
||||
|
||||
//如果目录名称长度超过目录缓冲区长度则放弃
|
||||
if (dwNameLen >= dwBufLen)
|
||||
return false;
|
||||
|
||||
//如果目录名称长度为0则表示所有目录均已创建完成
|
||||
if (dwNameLen == 0)
|
||||
return true;
|
||||
|
||||
sPathPtr += dwNameLen;
|
||||
|
||||
//如果目录名称不是驱动器名称则检查和创建目录
|
||||
if (sPathPtr[-1] != ':')
|
||||
{
|
||||
//如果目录不存在则创建此目录
|
||||
dwAttr = GetFileAttributes(sPath);
|
||||
|
||||
if ((dwAttr == (DWORD) - 1 && GetLastError() == ERROR_FILE_NOT_FOUND))
|
||||
{
|
||||
if (!CreateDirectory(sPath, NULL))
|
||||
return false;
|
||||
}
|
||||
//如果文件存在且文件不是目录则返回false
|
||||
else if (!(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
sPathPtr[0] = '\\';
|
||||
sPathPtr++;
|
||||
|
||||
if (dwBufLen > dwNameLen)
|
||||
dwBufLen -= dwNameLen + 1;
|
||||
else dwBufLen = 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
#else
|
||||
char dirName[256];
|
||||
_STRNCPY_A(dirName, sDirPath);
|
||||
int len = strlen(dirName);
|
||||
|
||||
if (dirName[len - 1] != '/')
|
||||
_STRNCAT_A(dirName, "/");
|
||||
|
||||
len = strlen(dirName);
|
||||
|
||||
for (int i = 1; i < len; i++)
|
||||
{
|
||||
if (dirName[i] == '/')
|
||||
{
|
||||
dirName[i] = 0;
|
||||
if (access(dirName, F_OK) != 0)
|
||||
{
|
||||
if (mkdir(dirName, 0755) == -1)
|
||||
{
|
||||
//OutputMsg(rmError, "mkdir error, %s", dirName);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
dirName[i] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
size_t FDOP::ExtractFileDirectory(const char* sFilePath, char* sDirBuf, size_t dwBufLen)
|
||||
{
|
||||
const char* sDirEnd = sFilePath + strlen(sFilePath) - 1;
|
||||
|
||||
while (sDirEnd >= sFilePath && *sDirEnd != '/' && *sDirEnd != '\\')
|
||||
{
|
||||
sDirEnd--;
|
||||
}
|
||||
|
||||
if (sDirEnd > sFilePath)
|
||||
{
|
||||
size_t dwNameLen = sDirEnd - sFilePath;
|
||||
|
||||
if (dwBufLen > 0)
|
||||
{
|
||||
if (dwBufLen > dwNameLen)
|
||||
dwBufLen = dwNameLen;
|
||||
else dwBufLen--;
|
||||
|
||||
memcpy(sDirBuf, sFilePath, sizeof(*sDirBuf) * dwBufLen);
|
||||
sDirBuf[dwBufLen] = 0;
|
||||
}
|
||||
|
||||
return dwNameLen;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t FDOP::ExtractTopDirectoryName(const char* sDirPath, const char* *ppChildDirPath, char* sDirName, size_t dwBufLen)
|
||||
{
|
||||
const char* sNameEnd;
|
||||
|
||||
//跳过目录名称前连续的'/'或'\'
|
||||
while (*sDirPath && (*sDirPath == '/' || *sDirPath == '\\'))
|
||||
{
|
||||
sDirPath++;
|
||||
}
|
||||
|
||||
sNameEnd = sDirPath;
|
||||
|
||||
//定位目录名称起始的位置
|
||||
while (*sNameEnd)
|
||||
{
|
||||
if (*sNameEnd == '/' || *sNameEnd == '\\')
|
||||
break;
|
||||
|
||||
sNameEnd++;
|
||||
}
|
||||
|
||||
//拷贝目录名称
|
||||
if (sNameEnd > sDirPath)
|
||||
{
|
||||
size_t dwNameLen = sNameEnd - sDirPath;
|
||||
|
||||
if (dwBufLen > 0)
|
||||
{
|
||||
if (dwBufLen > dwNameLen)
|
||||
dwBufLen = dwNameLen;
|
||||
else dwBufLen--;
|
||||
|
||||
memcpy(sDirName, sDirPath, sizeof(*sDirPath) * dwBufLen);
|
||||
sDirName[dwBufLen] = 0;
|
||||
|
||||
if (ppChildDirPath)
|
||||
*ppChildDirPath = sNameEnd;
|
||||
}
|
||||
|
||||
return dwNameLen;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t FDOP::ExtractFileName(const char* sFilePath, char* sNameBuf, size_t dwBufLen)
|
||||
{
|
||||
const char* sNameStart;
|
||||
const char* sNameEnd = sFilePath + strlen(sFilePath) - 1;
|
||||
|
||||
//跳过目录名称后连续的'/'或'\'
|
||||
while (sNameEnd >= sFilePath && (*sNameEnd == '/' || *sNameEnd == '\\'))
|
||||
{
|
||||
sNameEnd--;
|
||||
}
|
||||
|
||||
sNameStart = sNameEnd;
|
||||
sNameEnd++;
|
||||
|
||||
//定位目录名称起始的位置
|
||||
while (sNameStart >= sFilePath)
|
||||
{
|
||||
if (*sNameStart == '/' || *sNameStart == '\\')
|
||||
break;
|
||||
|
||||
sNameStart--;
|
||||
}
|
||||
|
||||
sNameStart++;
|
||||
|
||||
//拷贝目录名称
|
||||
if (sNameStart < sNameEnd)
|
||||
{
|
||||
size_t dwNameLen = sNameEnd - sNameStart;
|
||||
|
||||
if (dwBufLen > 0)
|
||||
{
|
||||
if (dwBufLen > dwNameLen)
|
||||
dwBufLen = dwNameLen;
|
||||
else dwBufLen--;
|
||||
|
||||
memcpy(sNameBuf, sNameStart, sizeof(*sNameStart) * dwBufLen);
|
||||
sNameBuf[dwBufLen] = 0;
|
||||
}
|
||||
|
||||
return dwNameLen;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
66
Gateway/common/utils/fdop.h
Normal file
66
Gateway/common/utils/fdop.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef _FDOP_H_
|
||||
#define _FDOP_H_
|
||||
|
||||
/************************************************************************
|
||||
*
|
||||
* 常用文件与目录操作函数库
|
||||
*
|
||||
************************************************************************/
|
||||
|
||||
namespace FDOP
|
||||
{
|
||||
/* 判断文件或目录是否存在
|
||||
* 在操作系统中,目录也是一个文件,如果要判断一个目录是否存在则应当使用DirectoryExists,
|
||||
* 要判断一个文件是否存在且是一个归档文件则应当使用IsArchive。
|
||||
* @如果文件或目录存在则返回true否则返回false
|
||||
* %文件路径字符长度不得超过MAX_PATH
|
||||
*/
|
||||
bool FileExists(const char* sFilePath);
|
||||
|
||||
/* 逐层创建目录
|
||||
* 如果创建目录C:\a\b\c\d,最终目录的父目录不存在则逐级创建父目录并创建最终目录
|
||||
* @如果目录完全创建成功则函数返回true,否则返回false。
|
||||
* %如果在创建某个父目录成功后并创建子目录失败,则函数返回false且已经创建的父目录不会被删除。
|
||||
* %目录路径的总体字符长度,函数要求必须在MAX_PATH个字符长度以内
|
||||
*/
|
||||
bool DeepCreateDirectory(const char* sDirPath);
|
||||
|
||||
|
||||
/* 获取文件所在目录路径
|
||||
* c:\abc\efg\123.txt --> c:\abc\efg\
|
||||
* c:\abc\efg\ --> c:\abc\
|
||||
* 参数sDirBuf用于存储目录字符串
|
||||
* 参数dwBufLen为sDirName参数的缓冲区字符(非字节)长度,其中含需要保留的终止字符,
|
||||
如果dwBufLen值为0则函数不会将目录路径拷贝到sDirBuf中;
|
||||
如果dwBufLen值非0则函数会将目录路径拷贝到sDirBuf中并会在sDirBuf中写入终止符;
|
||||
如果缓冲区不够则只拷贝dwBufLen-1个字符并会在sDirBuf中写入终止符。
|
||||
@函数返回拷贝目录路径所需的字符长度(含终止符)
|
||||
*/
|
||||
size_t ExtractFileDirectory(const char* sFilePath, char* sDirBuf, size_t dwBufLen);
|
||||
|
||||
/* 获取顶层目录名称
|
||||
* (abc\efg\ --> abc)
|
||||
* 参数ppChildDirPath用于存顶层目录之后的目录路径,参数可以为空
|
||||
* 参数sDirName用于存储目录字符串
|
||||
* 参数dwBufLen为sDirName参数的缓冲区字符(非字节)长度,其中含需要保留的终止字符,
|
||||
如果dwBufLen值为0则函数不会将目录名拷贝到sDirName中;
|
||||
如果dwBufLen值非0则函数会将目录名拷贝到sDirName中并会在sDirName中写入终止符;
|
||||
如果缓冲区不够则只拷贝dwBufLen-1个字符并会在sDirName中写入终止符。
|
||||
@函数返回拷贝目录名所需的字符长度(含终止符)
|
||||
*/
|
||||
size_t ExtractTopDirectoryName(const char* sDirPath, const char* *ppChildDirPath, char* sDirName, size_t dwBufLen);
|
||||
|
||||
/* 获取文件或目录名称
|
||||
* c:\abc\123.txt --> 123.txt
|
||||
* c:\abc\efg\ --> efg
|
||||
* 参数sDirBuf用于存储文件名称字符串
|
||||
* 参数dwBufLen为sNameBuf参数的缓冲区字符(非字节)长度,其中含需要保留的终止字符,
|
||||
如果dwBufLen值为0则函数不会将文件名拷贝到sNameBuf中;
|
||||
如果dwBufLen值非0则函数会将文件名拷贝到sNameBuf中并会在sNameBuf中写入终止符;
|
||||
如果缓冲区不够则只拷贝dwBufLen-1个字符并会在sNameBuf中写入终止符。
|
||||
@函数返回拷贝文件名所需的字符长度(含终止符)
|
||||
*/
|
||||
size_t ExtractFileName(const char* sFilePath, char* sNameBuf, size_t dwBufLen);
|
||||
}
|
||||
|
||||
#endif
|
||||
36
Gateway/common/utils/luareader.h
Normal file
36
Gateway/common/utils/luareader.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "luabase/base_lua_config.h"
|
||||
#include "string_ex.h"
|
||||
|
||||
/*
|
||||
ly:使该类可以对外提供读取lua的工具型接口,读取lua配置的地方只需要在源文件中引入
|
||||
本文件,而不再是需要在声明中就要引入并继承BaseLuaConfig类
|
||||
*/
|
||||
class LuaReader :public BaseLuaConfig
|
||||
{
|
||||
typedef BaseLuaConfig Base;
|
||||
public:
|
||||
void showError(const char* sError) { OutputMsg(rmError, sError); }
|
||||
void showTableNotExists(const char* sTableName) { return Base::showTableNotExists(sTableName); }
|
||||
void showFieldDoesNotContainValue(const char* sFieldName, const char* sValueType) { return Base::showFieldDoesNotContainValue(sFieldName, sValueType); }
|
||||
|
||||
bool openGlobalTable(const char* sTableName) { return Base::openGlobalTable(sTableName); }
|
||||
bool openFieldTable(const char* sTableName) { return Base::openFieldTable(sTableName); }
|
||||
bool enumTableFirst(){ return Base::enumTableFirst(); }
|
||||
bool enumTableNext() { return Base::enumTableNext(); }
|
||||
void endTableEnum() { return Base::endTableEnum(); }
|
||||
bool getFieldIndexTable(const int IndexStartFromOne) { return Base::getFieldIndexTable(IndexStartFromOne); }
|
||||
bool fieldTableExists(const char* sTableName) { return Base::feildTableExists(sTableName); }
|
||||
void closeTable() { return Base::closeTable(); }
|
||||
const char* getTablePath() { String r; return Base::getTablePath(r); }
|
||||
const char* getKeyName() { String r; return Base::getKeyName(r); }
|
||||
inline int getValueType(){ return Base::getValueType(); }
|
||||
bool getFieldBoolean(const char* sFieldName, const bool* pDefValue = nullptr, int* pIsValid = nullptr) { return Base::getFieldBoolean(sFieldName, pDefValue, pIsValid); }
|
||||
double getFieldNumber(const char* sFieldName, const double* pDefValue = nullptr, int* pIsValid = nullptr) { return Base::getFieldNumber(sFieldName, pDefValue, pIsValid); }
|
||||
int64_t getFieldInt64(const char* sFieldName, const int64_t* pDefValue = nullptr, int* pIsValid = nullptr) { return Base::getFieldInt64(sFieldName, pDefValue, pIsValid); }
|
||||
int getFieldInt(const char* sFieldName, const int* pDefValue = nullptr, int* pIsValid = nullptr) { return Base::getFieldInt(sFieldName, pDefValue, pIsValid); }
|
||||
const char* getFieldString(const char* sFieldName, const char* pDefValue = nullptr, int* pIsValid = nullptr) { return Base::getFieldString(sFieldName, pDefValue, pIsValid); }
|
||||
int getFieldStringBuffer(const char* sFieldName, char* sBuffer, size_t dwBufLen) { return Base::getFieldStringBuffer(sFieldName, sBuffer, dwBufLen); }
|
||||
int getTableLen() { return (int)lua_objlen(lua_, -1); }
|
||||
};
|
||||
40
Gateway/common/utils/singleton.h
Normal file
40
Gateway/common/utils/singleton.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
template< typename T >
|
||||
struct Singleton
|
||||
{
|
||||
typedef T value_type;
|
||||
typedef Singleton self_type;
|
||||
|
||||
private:// not to be implemented
|
||||
Singleton(const self_type&){}
|
||||
self_type& operator = (const self_type&){}
|
||||
Singleton(){}
|
||||
|
||||
friend T;
|
||||
private:// this struct promises thread safty
|
||||
struct creator
|
||||
{
|
||||
creator()
|
||||
{ // ensure that instance() called before main() starts
|
||||
self_type::instance();
|
||||
}
|
||||
inline void null_action() const
|
||||
{
|
||||
//empty
|
||||
}
|
||||
};
|
||||
|
||||
static creator creator_;
|
||||
|
||||
public:
|
||||
static value_type& instance()
|
||||
{ // the object to be returned
|
||||
static value_type instance_;
|
||||
// force the initialization of creator_
|
||||
creator_.null_action();
|
||||
return instance_;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
typename Singleton<T>::creator Singleton<T>::creator_;
|
||||
250
Gateway/common/utils/watch_thread.cc
Normal file
250
Gateway/common/utils/watch_thread.cc
Normal file
@@ -0,0 +1,250 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "os_def.h"
|
||||
#include "_ast.h"
|
||||
|
||||
#include "x_tick.h"
|
||||
#include "x_lock.h"
|
||||
#include "container/queue_list.h"
|
||||
#include "share_util.h"
|
||||
#include "os_def.h"
|
||||
#include "appitnmsg.h"
|
||||
#include "server_def.h"
|
||||
#include "watch_thread.h"
|
||||
|
||||
|
||||
WatchThread::WatchThread()
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
handle_ = NULL;
|
||||
#endif
|
||||
stoped_ = true;
|
||||
exit_ = true;
|
||||
|
||||
msg_list_.setLock(&msg_lock_);
|
||||
|
||||
Startup();
|
||||
}
|
||||
|
||||
WatchThread::~WatchThread()
|
||||
{
|
||||
Stop();
|
||||
|
||||
msg_list_.flush();
|
||||
msg_list_.clear();
|
||||
}
|
||||
|
||||
void WatchThread::SingleRun()
|
||||
{
|
||||
ProcessSysMsg();
|
||||
|
||||
//check
|
||||
time_t now_t = _getTickCount();
|
||||
for (int i = 0; i < list_.count(); ++i)
|
||||
{
|
||||
WatchData& data = list_[i];
|
||||
|
||||
if (now_t - data.last_t_ >= data.timeout_)
|
||||
{
|
||||
printf("watch thread: %d block!time:%lld current = %d\n",
|
||||
data.id_, (long long int)(now_t - data.last_t_), list_.count());
|
||||
|
||||
if (data.func_)
|
||||
data.func_(data.params_);
|
||||
data.last_t_ = now_t; // 避免不断的输出
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WatchThread::PostMsg(UINT uMsg, size_t para1, size_t para2, BlockFunc func, void* params)
|
||||
{
|
||||
InterMsg msg;
|
||||
msg.uMsg = uMsg;
|
||||
msg.uParam1 = para1;
|
||||
msg.uParam2 = para2;
|
||||
msg.func = func;
|
||||
msg.params = params;
|
||||
msg_list_.append(msg);
|
||||
}
|
||||
|
||||
void WatchThread::OnRecvSysMsg(UINT msg, size_t p1, size_t p2, BlockFunc func, void* params)
|
||||
{
|
||||
switch (msg)
|
||||
{
|
||||
case wmAddWatch:
|
||||
{
|
||||
int id = (int)p1;
|
||||
if (Find(id))
|
||||
{
|
||||
OutputMsg(rmError, "this id exist:%d", id);
|
||||
}
|
||||
WatchData data;
|
||||
data.id_ = id;
|
||||
data.last_t_ = _getTickCount();
|
||||
data.timeout_ = (int)p2;
|
||||
data.func_ = func;
|
||||
data.params_ = params;
|
||||
list_.add(data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case wmReport:
|
||||
{
|
||||
WatchData* data = Find((int)p1);
|
||||
if (data)
|
||||
{
|
||||
data->last_t_ = _getTickCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d is not find. report error!!!!\n", (int)p1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case wmRemove:
|
||||
{
|
||||
int id = (int)p1;
|
||||
for (int i = 0; i < list_.count(); ++i)
|
||||
{
|
||||
WatchData& data = list_[i];
|
||||
|
||||
if (data.id_ == id)
|
||||
{
|
||||
int count = list_.count();
|
||||
//查错
|
||||
//list_.remove(i);
|
||||
for (int j = i + 1, t = i; j < list_.count(); j++, t++)
|
||||
{
|
||||
WatchData& srcData = list_[j];
|
||||
WatchData& destData = list_[t];
|
||||
memcpy(&destData, &srcData, sizeof(WatchData));
|
||||
}
|
||||
list_.trunc(count-1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void WatchThread::ProcessSysMsg()
|
||||
{
|
||||
msg_list_.flush();
|
||||
int count = msg_list_.count();
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
InterMsg& msg = msg_list_[i];
|
||||
OnRecvSysMsg(msg.uMsg, msg.uParam1, msg.uParam2, msg.func, msg.params);
|
||||
}
|
||||
|
||||
msg_list_.clear();
|
||||
}
|
||||
|
||||
void WatchThread::OnWorkThreadStart()
|
||||
{
|
||||
OutputMsg(rmError, "watch thread start");
|
||||
}
|
||||
|
||||
void WatchThread::OnWorkThreadStop()
|
||||
{
|
||||
OutputMsg(rmError, "watch thread stop");
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
void STDCALL WatchThread::Work_R(WatchThread* ptr)
|
||||
#else
|
||||
void* WatchThread::Work_R(void* ptr)
|
||||
#endif
|
||||
{
|
||||
WatchThread* watch = (WatchThread*)ptr;
|
||||
|
||||
watch->OnWorkThreadStart();
|
||||
|
||||
while (!watch->stoped_)
|
||||
{
|
||||
watch->SingleRun();
|
||||
|
||||
Sleep(1);
|
||||
}
|
||||
|
||||
watch->OnWorkThreadStop();
|
||||
|
||||
watch->exit_ = true;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
ExitThread(0);//设置线程退出返回值
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool WatchThread::Startup()
|
||||
{
|
||||
bool ret = true;
|
||||
int err = 0;
|
||||
|
||||
stoped_ = false;
|
||||
#ifdef _MSC_VER
|
||||
handle_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Work_R, this, 0, &thread_id_);
|
||||
|
||||
if (!handle_)
|
||||
{
|
||||
err = GetLastError();
|
||||
ret = false;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
if (pthread_create(&thread_id_, NULL, Work_R, this))
|
||||
{
|
||||
err = errno;
|
||||
ret = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
OutputError(err, ("创建监控线程失败"));
|
||||
return false;
|
||||
}
|
||||
|
||||
exit_ = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void WatchThread::Stop()
|
||||
{
|
||||
stoped_ = true;
|
||||
|
||||
while (!exit_)
|
||||
{
|
||||
Sleep(10);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
CloseThread(handle_);
|
||||
#endif
|
||||
}
|
||||
|
||||
WatchThread::WatchData* WatchThread::Find(int id)
|
||||
{
|
||||
for (int i = 0; i < list_.count(); ++i)
|
||||
{
|
||||
WatchData& data = list_[i];
|
||||
|
||||
if (data.id_ == id)
|
||||
{
|
||||
return &data;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
102
Gateway/common/utils/watch_thread.h
Normal file
102
Gateway/common/utils/watch_thread.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef _WATCH_THREAD_H_
|
||||
#define _WATCH_THREAD_H_
|
||||
|
||||
using namespace tick64;
|
||||
using namespace container;
|
||||
using namespace lock;
|
||||
|
||||
typedef void (*BlockFunc)(void*);
|
||||
|
||||
class WatchThread
|
||||
{
|
||||
// 可以考虑加个超时处理,外部传个函数指针进来
|
||||
struct WatchData
|
||||
{
|
||||
int id_;
|
||||
int64_t last_t_;
|
||||
int timeout_;
|
||||
BlockFunc func_;
|
||||
void* params_;
|
||||
WatchData() : id_(0), last_t_(0), timeout_(0), func_(NULL), params_(NULL)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
enum WatchMsg
|
||||
{
|
||||
wmAddWatch,
|
||||
wmReport,
|
||||
wmRemove,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned int uMsg; //消息号
|
||||
size_t uParam1;//消息参数1
|
||||
size_t uParam2;//消息参数2
|
||||
BlockFunc func;
|
||||
void* params;
|
||||
}InterMsg;
|
||||
public:
|
||||
WatchThread();
|
||||
virtual ~WatchThread();
|
||||
|
||||
bool Startup();
|
||||
void Stop();
|
||||
#ifdef _MSC_VER
|
||||
unsigned long GetThreadId()
|
||||
#else
|
||||
pthread_t GetThreadId()
|
||||
#endif
|
||||
{
|
||||
return thread_id_;
|
||||
}
|
||||
|
||||
void AddWatch(int id, int timeout, BlockFunc func = NULL, void* params = NULL)
|
||||
{
|
||||
PostMsg(wmAddWatch, id, timeout, func, params);
|
||||
}
|
||||
void RemoveWatch(int id)
|
||||
{
|
||||
PostMsg(wmRemove, id, 0, NULL, NULL);
|
||||
}
|
||||
void Report(int id)
|
||||
{
|
||||
PostMsg(wmReport, id, 0, NULL, NULL);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void OnWorkThreadStart();
|
||||
virtual void OnWorkThreadStop();
|
||||
protected:
|
||||
void SingleRun();
|
||||
void ProcessSysMsg();
|
||||
void OnRecvSysMsg(UINT msg, size_t p1, size_t p2, BlockFunc func, void* params);
|
||||
void PostMsg(UINT uMsg, size_t para1, size_t para2, BlockFunc func, void* params);
|
||||
|
||||
WatchData* Find(int id);
|
||||
private:
|
||||
#ifdef _MSC_VER
|
||||
static void STDCALL Work_R(WatchThread* ptr);
|
||||
#else
|
||||
static void* Work_R(void* ptr);
|
||||
#endif
|
||||
private:
|
||||
#ifdef _MSC_VER
|
||||
HANDLE handle_; //工作线程句柄
|
||||
unsigned long thread_id_; //线程ID
|
||||
#else
|
||||
pthread_t thread_id_;
|
||||
#endif
|
||||
bool stoped_; //是否标记为停止
|
||||
bool exit_;
|
||||
|
||||
QueueList<InterMsg> msg_list_; //内部消息列表
|
||||
Mutex msg_lock_; //内部消息列表锁
|
||||
|
||||
Vector<WatchData> list_;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user