mirror of https://github.com/tp4a/teleport
convert source files to UTF-8 with BOM.
parent
03a21be954
commit
db0990d8c2
|
@ -102,3 +102,5 @@ profile
|
||||||
*.moved-aside
|
*.moved-aside
|
||||||
/server/share/tmp
|
/server/share/tmp
|
||||||
|
|
||||||
|
/server/tp_core/testssh/Debug
|
||||||
|
/server/tp_core/testssh/Release
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __LIB_EX_H__
|
#ifndef __LIB_EX_H__
|
||||||
#define __LIB_EX_H__
|
#define __LIB_EX_H__
|
||||||
|
|
||||||
#ifdef EX_HAVE_CONFIG
|
#ifdef EX_HAVE_CONFIG
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __LIB_EX_CONST_H__
|
#ifndef __LIB_EX_CONST_H__
|
||||||
#define __LIB_EX_CONST_H__
|
#define __LIB_EX_CONST_H__
|
||||||
|
|
||||||
#include "ex_platform.h"
|
#include "ex_platform.h"
|
||||||
|
@ -43,8 +43,8 @@
|
||||||
// error code.
|
// error code.
|
||||||
//====================================================
|
//====================================================
|
||||||
#define EXRV_OK 0
|
#define EXRV_OK 0
|
||||||
#define EXRV_SYS_ERR 1 // 系统错误,可以使用GetLastError或者errno来获取具体错误值
|
#define EXRV_SYS_ERR 1 // 系统错误,可以使用GetLastError或者errno来获取具体错误值
|
||||||
#define EXRV_FAILED 2 // 操作失败
|
#define EXRV_FAILED 2 // 操作失败
|
||||||
|
|
||||||
//#define EXRV_CANNOT_FOUND 9
|
//#define EXRV_CANNOT_FOUND 9
|
||||||
#define EXRV_CANNOT_CREATE 10
|
#define EXRV_CANNOT_CREATE 10
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __EX_INI_H__
|
#ifndef __EX_INI_H__
|
||||||
#define __EX_INI_H__
|
#define __EX_INI_H__
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __EX_LOG_H__
|
#ifndef __EX_LOG_H__
|
||||||
#define __EX_LOG_H__
|
#define __EX_LOG_H__
|
||||||
|
|
||||||
#include "ex_types.h"
|
#include "ex_types.h"
|
||||||
|
@ -27,7 +27,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool _open_file();
|
bool _open_file();
|
||||||
bool _rotate_file(void); // 将现有日志文件改名备份,然后新开一个日志文件
|
bool _rotate_file(void); // 将现有日志文件改名备份,然后新开一个日志文件
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExThreadLock lock;
|
ExThreadLock lock;
|
||||||
|
@ -63,7 +63,7 @@ void EXLOG_USE_LOGGER(ExLogger* logger);
|
||||||
void EXLOG_LEVEL(int min_level);
|
void EXLOG_LEVEL(int min_level);
|
||||||
void EXLOG_DEBUG(bool debug_mode);
|
void EXLOG_DEBUG(bool debug_mode);
|
||||||
|
|
||||||
// 设定日志文件名及路径,如未指定路径,则为可执行程序所在目录下的log目录。
|
// 设定日志文件名及路径,如未指定路径,则为可执行程序所在目录下的log目录。
|
||||||
void EXLOG_FILE(const wchar_t* log_file, const wchar_t* log_path = NULL, ex_u32 max_filesize = EX_LOG_FILE_MAX_SIZE, ex_u8 max_filecount = EX_LOG_FILE_MAX_COUNT);
|
void EXLOG_FILE(const wchar_t* log_file, const wchar_t* log_path = NULL, ex_u32 max_filesize = EX_LOG_FILE_MAX_SIZE, ex_u8 max_filecount = EX_LOG_FILE_MAX_COUNT);
|
||||||
|
|
||||||
void EXLOG_CONSOLE(bool output_to_console);
|
void EXLOG_CONSOLE(bool output_to_console);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __LIB_EX_PATH_H__
|
#ifndef __LIB_EX_PATH_H__
|
||||||
#define __LIB_EX_PATH_H__
|
#define __LIB_EX_PATH_H__
|
||||||
|
|
||||||
#include "ex_platform.h"
|
#include "ex_platform.h"
|
||||||
|
@ -39,7 +39,7 @@ bool ex_path_join(ex_wstr& inout_path, EX_BOOL auto_abspath, ...);
|
||||||
bool ex_abspath_to(const ex_wstr& base_abs_path, const ex_wstr& relate_path, ex_wstr& out_path);
|
bool ex_abspath_to(const ex_wstr& base_abs_path, const ex_wstr& relate_path, ex_wstr& out_path);
|
||||||
bool ex_mkdirs(const ex_wstr& in_path);
|
bool ex_mkdirs(const ex_wstr& in_path);
|
||||||
|
|
||||||
// 获取文件名中的扩展名部分(不包括.,例如abc.py,返回 py)
|
// 获取文件名中的扩展名部分(不包括.,例如abc.py,返回 py)
|
||||||
bool ex_path_ext_name(const ex_wstr& in_filename, ex_wstr& out_ext);
|
bool ex_path_ext_name(const ex_wstr& in_filename, ex_wstr& out_ext);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __LIB_EX_PLATFORM_H__
|
#ifndef __LIB_EX_PLATFORM_H__
|
||||||
#define __LIB_EX_PLATFORM_H__
|
#define __LIB_EX_PLATFORM_H__
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
#if defined(_WIN32) || defined(WIN32)
|
||||||
|
|
|
@ -1,86 +1,86 @@
|
||||||
#ifndef __LIB_EX_STR_H__
|
#ifndef __LIB_EX_STR_H__
|
||||||
#define __LIB_EX_STR_H__
|
#define __LIB_EX_STR_H__
|
||||||
|
|
||||||
#include "ex_types.h"
|
#include "ex_types.h"
|
||||||
|
|
||||||
#define EX_CODEPAGE_ACP 0
|
#define EX_CODEPAGE_ACP 0
|
||||||
#define EX_CODEPAGE_UTF8 1
|
#define EX_CODEPAGE_UTF8 1
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
# define EX_CODEPAGE_DEFAULT EX_CODEPAGE_ACP
|
# define EX_CODEPAGE_DEFAULT EX_CODEPAGE_ACP
|
||||||
#else
|
#else
|
||||||
# define EX_CODEPAGE_DEFAULT EX_CODEPAGE_UTF8
|
# define EX_CODEPAGE_DEFAULT EX_CODEPAGE_UTF8
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define EX_RSC_BEGIN 0x01
|
#define EX_RSC_BEGIN 0x01
|
||||||
#define EX_RSC_END 0x02
|
#define EX_RSC_END 0x02
|
||||||
#define EX_RSC_ALL EX_RSC_BEGIN | EX_RSC_END
|
#define EX_RSC_ALL EX_RSC_BEGIN | EX_RSC_END
|
||||||
|
|
||||||
//=================================================
|
//=================================================
|
||||||
// C Interface
|
// C Interface
|
||||||
//=================================================
|
//=================================================
|
||||||
|
|
||||||
// copy a string from `source` to `target`.
|
// copy a string from `source` to `target`.
|
||||||
// `size` is size of target buffer.
|
// `size` is size of target buffer.
|
||||||
// if buffer is to small, NULL will return, but `size-1` characters have been copied.
|
// if buffer is to small, NULL will return, but `size-1` characters have been copied.
|
||||||
char* ex_strcpy(char* target, size_t size, const char* source);
|
char* ex_strcpy(char* target, size_t size, const char* source);
|
||||||
wchar_t* ex_wcscpy(wchar_t* target, size_t size, const wchar_t* source);
|
wchar_t* ex_wcscpy(wchar_t* target, size_t size, const wchar_t* source);
|
||||||
|
|
||||||
|
|
||||||
// dupilicate a string.
|
// dupilicate a string.
|
||||||
// must use ex_free() to release the returned value.
|
// must use ex_free() to release the returned value.
|
||||||
char* ex_strdup(const char* src);
|
char* ex_strdup(const char* src);
|
||||||
wchar_t* ex_wcsdup(const wchar_t* src);
|
wchar_t* ex_wcsdup(const wchar_t* src);
|
||||||
|
|
||||||
// convert between mutli-bytes and wide char string.
|
// convert between mutli-bytes and wide char string.
|
||||||
// must use ex_free() to release the returned value.
|
// must use ex_free() to release the returned value.
|
||||||
wchar_t* ex_str2wcs_alloc(const char* in_buffer, int code_page);
|
wchar_t* ex_str2wcs_alloc(const char* in_buffer, int code_page);
|
||||||
char* ex_wcs2str_alloc(const wchar_t* in_buffer, int code_page);
|
char* ex_wcs2str_alloc(const wchar_t* in_buffer, int code_page);
|
||||||
|
|
||||||
// convert char** argv to wchar_t** argv.
|
// convert char** argv to wchar_t** argv.
|
||||||
// must use ex_free_argv() to release the returned value.
|
// must use ex_free_argv() to release the returned value.
|
||||||
wchar_t** ex_make_wargv(int argc, char** argv);
|
wchar_t** ex_make_wargv(int argc, char** argv);
|
||||||
void ex_free_wargv(int argc, wchar_t** argv);
|
void ex_free_wargv(int argc, wchar_t** argv);
|
||||||
|
|
||||||
EX_BOOL ex_str_only_white_space(const wchar_t* src);
|
EX_BOOL ex_str_only_white_space(const wchar_t* src);
|
||||||
EX_BOOL ex_wcs_only_white_space(const char* src);
|
EX_BOOL ex_wcs_only_white_space(const char* src);
|
||||||
|
|
||||||
|
|
||||||
int ex_strformat(char* out_buf, size_t buf_size, const char* fmt, ...);
|
int ex_strformat(char* out_buf, size_t buf_size, const char* fmt, ...);
|
||||||
int ex_wcsformat(wchar_t* out_buf, size_t buf_size, const wchar_t* fmt, ...);
|
int ex_wcsformat(wchar_t* out_buf, size_t buf_size, const wchar_t* fmt, ...);
|
||||||
|
|
||||||
//=================================================
|
//=================================================
|
||||||
// C++ Interface
|
// C++ Interface
|
||||||
//=================================================
|
//=================================================
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef std::string ex_astr;
|
typedef std::string ex_astr;
|
||||||
typedef std::wstring ex_wstr;
|
typedef std::wstring ex_wstr;
|
||||||
|
|
||||||
typedef std::vector<ex_astr> ex_astrs;
|
typedef std::vector<ex_astr> ex_astrs;
|
||||||
typedef std::vector<ex_wstr> ex_wstrs;
|
typedef std::vector<ex_wstr> ex_wstrs;
|
||||||
typedef std::vector<ex_utf16> ex_str_utf16le;
|
typedef std::vector<ex_utf16> ex_str_utf16le;
|
||||||
|
|
||||||
bool ex_wstr2astr(const ex_wstr& in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
bool ex_wstr2astr(const ex_wstr& in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
||||||
bool ex_wstr2astr(const wchar_t* in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
bool ex_wstr2astr(const wchar_t* in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
||||||
bool ex_astr2wstr(const ex_astr& in_str, ex_wstr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
bool ex_astr2wstr(const ex_astr& in_str, ex_wstr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
||||||
bool ex_astr2wstr(const char* in_str, ex_wstr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
bool ex_astr2wstr(const char* in_str, ex_wstr& out_str, int code_page = EX_CODEPAGE_DEFAULT);
|
||||||
|
|
||||||
bool ex_only_white_space(const ex_astr& str_check);
|
bool ex_only_white_space(const ex_astr& str_check);
|
||||||
bool ex_only_white_space(const ex_wstr& str_check);
|
bool ex_only_white_space(const ex_wstr& str_check);
|
||||||
|
|
||||||
void ex_remove_white_space(ex_astr& str_fix, int ulFlag = EX_RSC_ALL);
|
void ex_remove_white_space(ex_astr& str_fix, int ulFlag = EX_RSC_ALL);
|
||||||
void ex_remove_white_space(ex_wstr& str_fix, int ulFlag = EX_RSC_ALL);
|
void ex_remove_white_space(ex_wstr& str_fix, int ulFlag = EX_RSC_ALL);
|
||||||
|
|
||||||
ex_astr& ex_replace_all(ex_astr& str, const ex_astr& old_value, const ex_astr& new_value);
|
ex_astr& ex_replace_all(ex_astr& str, const ex_astr& old_value, const ex_astr& new_value);
|
||||||
ex_wstr& ex_replace_all(ex_wstr& str, const ex_wstr& old_value, const ex_wstr& new_value);
|
ex_wstr& ex_replace_all(ex_wstr& str, const ex_wstr& old_value, const ex_wstr& new_value);
|
||||||
|
|
||||||
// 将UTF8字符串转换为UTF16-LE字符串(输出结果包含\0结束符)
|
// 将UTF8字符串转换为UTF16-LE字符串(输出结果包含\0结束符)
|
||||||
bool ex_utf8_to_utf16le(const std::string& from, ex_str_utf16le& to);
|
bool ex_utf8_to_utf16le(const std::string& from, ex_str_utf16le& to);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif // __LIB_EX_STR_H__
|
#endif // __LIB_EX_STR_H__
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __EX_THREAD_H__
|
#ifndef __EX_THREAD_H__
|
||||||
#define __EX_THREAD_H__
|
#define __EX_THREAD_H__
|
||||||
|
|
||||||
#include "ex_str.h"
|
#include "ex_str.h"
|
||||||
|
@ -23,11 +23,11 @@ public:
|
||||||
|
|
||||||
bool is_running(void) { return m_is_running; }
|
bool is_running(void) { return m_is_running; }
|
||||||
|
|
||||||
// 创建并启动线程(执行被重载了的run()函数)
|
// 创建并启动线程(执行被重载了的run()函数)
|
||||||
bool start(void);
|
bool start(void);
|
||||||
// 结束线程(等待wait_timeout_ms毫秒,如果wait_timeout_ms为0,则无限等待)
|
// 结束线程(等待wait_timeout_ms毫秒,如果wait_timeout_ms为0,则无限等待)
|
||||||
bool stop(void);
|
bool stop(void);
|
||||||
// 直接结束线程(强杀,不建议使用)
|
// 直接结束线程(强杀,不建议使用)
|
||||||
bool terminate(void);
|
bool terminate(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -52,7 +52,7 @@ protected:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 线程锁(进程内使用)
|
// 线程锁(进程内使用)
|
||||||
class ExThreadLock
|
class ExThreadLock
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -70,7 +70,7 @@ private:
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// 线程锁辅助类
|
// 线程锁辅助类
|
||||||
class ExThreadSmartLock
|
class ExThreadSmartLock
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -109,12 +109,12 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 原子操作
|
// 原子操作
|
||||||
int ex_atomic_add(volatile int* pt, int t);
|
int ex_atomic_add(volatile int* pt, int t);
|
||||||
int ex_atomic_inc(volatile int* pt);
|
int ex_atomic_inc(volatile int* pt);
|
||||||
int ex_atomic_dec(volatile int* pt);
|
int ex_atomic_dec(volatile int* pt);
|
||||||
|
|
||||||
// 线程相关操作
|
// 线程相关操作
|
||||||
ex_u64 ex_get_thread_id(void);
|
ex_u64 ex_get_thread_id(void);
|
||||||
|
|
||||||
#endif // __EX_THREAD_H__
|
#endif // __EX_THREAD_H__
|
||||||
|
|
|
@ -1,44 +1,44 @@
|
||||||
#ifndef __LIB_EX_TYPE_H__
|
#ifndef __LIB_EX_TYPE_H__
|
||||||
#define __LIB_EX_TYPE_H__
|
#define __LIB_EX_TYPE_H__
|
||||||
|
|
||||||
#include "ex_platform.h"
|
#include "ex_platform.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef signed char ex_i8;
|
typedef signed char ex_i8;
|
||||||
typedef signed short ex_i16;
|
typedef signed short ex_i16;
|
||||||
|
|
||||||
typedef unsigned char ex_u8;
|
typedef unsigned char ex_u8;
|
||||||
typedef unsigned short ex_u16;
|
typedef unsigned short ex_u16;
|
||||||
typedef unsigned int ex_u32;
|
typedef unsigned int ex_u32;
|
||||||
typedef unsigned long ex_ulong;
|
typedef unsigned long ex_ulong;
|
||||||
|
|
||||||
#if defined(EX_OS_WIN32)
|
#if defined(EX_OS_WIN32)
|
||||||
typedef unsigned __int64 ex_u64;
|
typedef unsigned __int64 ex_u64;
|
||||||
typedef signed __int64 ex_i64;
|
typedef signed __int64 ex_i64;
|
||||||
typedef wchar_t ex_utf16;
|
typedef wchar_t ex_utf16;
|
||||||
#else
|
#else
|
||||||
typedef unsigned long long ex_u64;
|
typedef unsigned long long ex_u64;
|
||||||
typedef signed long long ex_i64;
|
typedef signed long long ex_i64;
|
||||||
typedef ex_i16 ex_utf16;
|
typedef ex_i16 ex_utf16;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef int EX_BOOL;
|
typedef int EX_BOOL;
|
||||||
#define EX_TRUE 1
|
#define EX_TRUE 1
|
||||||
#define EX_FALSE 0
|
#define EX_FALSE 0
|
||||||
|
|
||||||
|
|
||||||
typedef std::vector<ex_u8> ex_bin;
|
typedef std::vector<ex_u8> ex_bin;
|
||||||
typedef std::vector<char> ex_chars;
|
typedef std::vector<char> ex_chars;
|
||||||
|
|
||||||
typedef ex_u32 ex_rv;
|
typedef ex_u32 ex_rv;
|
||||||
|
|
||||||
|
|
||||||
#if defined(EX_OS_WIN32)
|
#if defined(EX_OS_WIN32)
|
||||||
# define EX_DYLIB_HANDLE HINSTANCE
|
# define EX_DYLIB_HANDLE HINSTANCE
|
||||||
#else
|
#else
|
||||||
# define EX_DYLIB_HANDLE void*
|
# define EX_DYLIB_HANDLE void*
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif // __LIB_EX_TYPE_H__
|
#endif // __LIB_EX_TYPE_H__
|
||||||
|
|
|
@ -1,55 +1,55 @@
|
||||||
#ifndef __LIB_EX_UTIL_H__
|
#ifndef __LIB_EX_UTIL_H__
|
||||||
#define __LIB_EX_UTIL_H__
|
#define __LIB_EX_UTIL_H__
|
||||||
|
|
||||||
#include "ex_types.h"
|
#include "ex_types.h"
|
||||||
#include "ex_str.h"
|
#include "ex_str.h"
|
||||||
|
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
# include <time.h>
|
# include <time.h>
|
||||||
//# include <io.h>
|
//# include <io.h>
|
||||||
//# include <stdio.h>
|
//# include <stdio.h>
|
||||||
// #include <direct.h>
|
// #include <direct.h>
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
#else
|
#else
|
||||||
// #include <dirent.h>
|
// #include <dirent.h>
|
||||||
# include <dlfcn.h>
|
# include <dlfcn.h>
|
||||||
# include <sys/time.h>
|
# include <sys/time.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
EX_BOOL ex_initialize(const char* lc_ctype);
|
EX_BOOL ex_initialize(const char* lc_ctype);
|
||||||
|
|
||||||
void ex_free(void* buffer);
|
void ex_free(void* buffer);
|
||||||
|
|
||||||
// 在haystack(长度为haystacklen字节)中查找needle(长度为needlelen)的起始地址,返回NULL表示没有找到
|
// 在haystack(长度为haystacklen字节)中查找needle(长度为needlelen)的起始地址,返回NULL表示没有找到
|
||||||
const ex_u8* ex_memmem(const ex_u8* haystack, size_t haystacklen, const ex_u8* needle, size_t needlelen);
|
const ex_u8* ex_memmem(const ex_u8* haystack, size_t haystacklen, const ex_u8* needle, size_t needlelen);
|
||||||
void ex_mem_reverse(ex_u8* p, size_t l);
|
void ex_mem_reverse(ex_u8* p, size_t l);
|
||||||
|
|
||||||
void ex_printf(const char* fmt, ...);
|
void ex_printf(const char* fmt, ...);
|
||||||
void ex_wprintf(const wchar_t* fmt, ...);
|
void ex_wprintf(const wchar_t* fmt, ...);
|
||||||
|
|
||||||
ex_u64 ex_get_tick_count(void);
|
ex_u64 ex_get_tick_count(void);
|
||||||
void ex_sleep_ms(int ms);
|
void ex_sleep_ms(int ms);
|
||||||
|
|
||||||
EX_BOOL ex_localtime_now(int* t, struct tm* dt);
|
EX_BOOL ex_localtime_now(int* t, struct tm* dt);
|
||||||
|
|
||||||
|
|
||||||
FILE* ex_fopen(const ex_wstr& filename, const wchar_t* mode);
|
FILE* ex_fopen(const ex_wstr& filename, const wchar_t* mode);
|
||||||
FILE* ex_fopen(const ex_astr& filename, const char* mode);
|
FILE* ex_fopen(const ex_astr& filename, const char* mode);
|
||||||
|
|
||||||
// open a text file and read all content.
|
// open a text file and read all content.
|
||||||
bool ex_read_text_file(const ex_wstr& file_name, ex_astr& file_content);
|
bool ex_read_text_file(const ex_wstr& file_name, ex_astr& file_content);
|
||||||
// open a file and write content.
|
// open a file and write content.
|
||||||
bool ex_write_text_file(const ex_wstr& file_name, const ex_astr& file_content);
|
bool ex_write_text_file(const ex_wstr& file_name, const ex_astr& file_content);
|
||||||
|
|
||||||
EX_DYLIB_HANDLE ex_dlopen(const wchar_t* dylib_path);
|
EX_DYLIB_HANDLE ex_dlopen(const wchar_t* dylib_path);
|
||||||
void ex_dlclose(EX_DYLIB_HANDLE dylib);
|
void ex_dlclose(EX_DYLIB_HANDLE dylib);
|
||||||
|
|
||||||
|
|
||||||
// inet...
|
// inet...
|
||||||
int ex_ip4_name(const struct sockaddr_in* src, char* dst, size_t size);
|
int ex_ip4_name(const struct sockaddr_in* src, char* dst, size_t size);
|
||||||
|
|
||||||
#define EX_IPV4_NAME_LEN 16
|
#define EX_IPV4_NAME_LEN 16
|
||||||
#define EX_IPV6_NAME_LEN 46
|
#define EX_IPV6_NAME_LEN 46
|
||||||
const char* ex_inet_ntop(int af, const void *src, char *dst, size_t size);
|
const char* ex_inet_ntop(int af, const void *src, char *dst, size_t size);
|
||||||
|
|
||||||
#endif // __LIB_EX_UTIL_H__
|
#endif // __LIB_EX_UTIL_H__
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __EX_WINSRV_H__
|
#ifndef __EX_WINSRV_H__
|
||||||
#define __EX_WINSRV_H__
|
#define __EX_WINSRV_H__
|
||||||
|
|
||||||
#include "ex_str.h"
|
#include "ex_str.h"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include <ex/ex_ini.h>
|
#include <ex/ex_ini.h>
|
||||||
#include <ex/ex_log.h>
|
#include <ex/ex_log.h>
|
||||||
#include <ex/ex_util.h>
|
#include <ex/ex_util.h>
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ bool ExIniFile::LoadFromFile(const ex_wstr& strFileName, bool bClearOld)
|
||||||
{
|
{
|
||||||
pOffset += 3;
|
pOffset += 3;
|
||||||
}
|
}
|
||||||
// 配置文件均使用UTF8编码
|
// 配置文件均使用UTF8编码
|
||||||
ex_wstr fileData;
|
ex_wstr fileData;
|
||||||
if (!ex_astr2wstr(pOffset, fileData, EX_CODEPAGE_UTF8))
|
if (!ex_astr2wstr(pOffset, fileData, EX_CODEPAGE_UTF8))
|
||||||
return false;
|
return false;
|
||||||
|
@ -357,7 +357,7 @@ void ExIniFile::Save(int codepage/* = EX_CODEPAGE_UTF8*/)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果有不属于任何小节的值对,先保存之
|
// 如果有不属于任何小节的值对,先保存之
|
||||||
if (m_dumy_sec.Count() > 0)
|
if (m_dumy_sec.Count() > 0)
|
||||||
m_dumy_sec.Save(file, codepage);
|
m_dumy_sec.Save(file, codepage);
|
||||||
|
|
||||||
|
@ -415,16 +415,16 @@ ExIniSection* ExIniFile::GetSection(const ex_wstr& strName, bool bCreateIfNotExi
|
||||||
}
|
}
|
||||||
|
|
||||||
// static function.
|
// static function.
|
||||||
// 解析一行,返回值为 [节名/值对/注释/什么也不是/出错了]
|
// 解析一行,返回值为 [节名/值对/注释/什么也不是/出错了]
|
||||||
// 节名 => strKey = [section_name]
|
// 节名 => strKey = [section_name]
|
||||||
// 值对 => strKey = strValue
|
// 值对 => strKey = strValue
|
||||||
ExIniFile::PARSE_RV ExIniFile::_ParseLine(const ex_wstr& strOrigLine, ex_wstr& strKey, ex_wstr& strValue)
|
ExIniFile::PARSE_RV ExIniFile::_ParseLine(const ex_wstr& strOrigLine, ex_wstr& strKey, ex_wstr& strValue)
|
||||||
{
|
{
|
||||||
// 首先去掉行首的空格或者 TAB 控制
|
// 首先去掉行首的空格或者 TAB 控制
|
||||||
ex_wstr strLine(strOrigLine);
|
ex_wstr strLine(strOrigLine);
|
||||||
ex_remove_white_space(strLine, EX_RSC_BEGIN);
|
ex_remove_white_space(strLine, EX_RSC_BEGIN);
|
||||||
|
|
||||||
// 判断是否为注释。 .ini 文件以 分号';'/'#' 作为注释行的第一个字符
|
// 判断是否为注释。 .ini 文件以 分号';'/'#' 作为注释行的第一个字符
|
||||||
if (';' == strLine[0] || '#' == strLine[0])
|
if (';' == strLine[0] || '#' == strLine[0])
|
||||||
{
|
{
|
||||||
return PARSE_COMMENT;
|
return PARSE_COMMENT;
|
||||||
|
@ -432,7 +432,7 @@ ExIniFile::PARSE_RV ExIniFile::_ParseLine(const ex_wstr& strOrigLine, ex_wstr& s
|
||||||
|
|
||||||
if ('[' == strLine[0])
|
if ('[' == strLine[0])
|
||||||
{
|
{
|
||||||
// 这是一个节(section)
|
// 这是一个节(section)
|
||||||
ex_wstr::size_type startPos = strLine.find('[');
|
ex_wstr::size_type startPos = strLine.find('[');
|
||||||
ex_wstr::size_type endPos = strLine.rfind(']');
|
ex_wstr::size_type endPos = strLine.rfind(']');
|
||||||
strLine.erase(endPos);
|
strLine.erase(endPos);
|
||||||
|
@ -443,23 +443,23 @@ ExIniFile::PARSE_RV ExIniFile::_ParseLine(const ex_wstr& strOrigLine, ex_wstr& s
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 看看能否找到等号(=),这是 key=value 的判别方法
|
// 看看能否找到等号(=),这是 key=value 的判别方法
|
||||||
ex_wstr::size_type pos = strLine.find('=');
|
ex_wstr::size_type pos = strLine.find('=');
|
||||||
if (ex_wstr::npos == pos)
|
if (ex_wstr::npos == pos)
|
||||||
{
|
{
|
||||||
//return PARSE_OTHER; // 没有等号
|
//return PARSE_OTHER; // 没有等号
|
||||||
ex_remove_white_space(strLine);
|
ex_remove_white_space(strLine);
|
||||||
strKey = strLine;
|
strKey = strLine;
|
||||||
strValue.clear();
|
strValue.clear();
|
||||||
return PARSE_KEYVALUE;
|
return PARSE_KEYVALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将等号前面的与等号后面的分割
|
// 将等号前面的与等号后面的分割
|
||||||
strKey.assign(strLine, 0, pos);
|
strKey.assign(strLine, 0, pos);
|
||||||
strValue.assign(strLine, pos + 1, strLine.length() - pos);
|
strValue.assign(strLine, pos + 1, strLine.length() - pos);
|
||||||
ex_remove_white_space(strKey);
|
ex_remove_white_space(strKey);
|
||||||
|
|
||||||
// 等号后面的应该原封不动,不应该移除空白字符
|
// 等号后面的应该原封不动,不应该移除空白字符
|
||||||
ex_remove_white_space(strValue, EX_RSC_BEGIN);
|
ex_remove_white_space(strValue, EX_RSC_BEGIN);
|
||||||
|
|
||||||
return PARSE_KEYVALUE;
|
return PARSE_KEYVALUE;
|
||||||
|
@ -489,7 +489,7 @@ bool ExIniFile::_ProcessLine(const ex_wstr strLine, ExIniSection** pCurSection)
|
||||||
break;
|
break;
|
||||||
case PARSE_SECTION:
|
case PARSE_SECTION:
|
||||||
{
|
{
|
||||||
// 创建一个节
|
// 创建一个节
|
||||||
ExIniSection* pSection = GetSection(strKey, true);
|
ExIniSection* pSection = GetSection(strKey, true);
|
||||||
if (NULL == pSection)
|
if (NULL == pSection)
|
||||||
{
|
{
|
||||||
|
@ -508,7 +508,7 @@ bool ExIniFile::_ProcessLine(const ex_wstr strLine, ExIniSection** pCurSection)
|
||||||
*pCurSection = &m_dumy_sec;
|
*pCurSection = &m_dumy_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建一个值对
|
// 创建一个值对
|
||||||
if (!(*pCurSection)->SetValue(strKey, strValue, true))
|
if (!(*pCurSection)->SetValue(strKey, strValue, true))
|
||||||
{
|
{
|
||||||
bError = true;
|
bError = true;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,4 +1,4 @@
|
||||||
#include <ex/ex_path.h>
|
#include <ex/ex_path.h>
|
||||||
#include <ex/ex_const.h>
|
#include <ex/ex_const.h>
|
||||||
#include <ex/ex_util.h>
|
#include <ex/ex_util.h>
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,224 +1,224 @@
|
||||||
#include <ex/ex_thread.h>
|
#include <ex/ex_thread.h>
|
||||||
#include <ex/ex_log.h>
|
#include <ex/ex_log.h>
|
||||||
|
|
||||||
//=========================================================
|
//=========================================================
|
||||||
//
|
//
|
||||||
//=========================================================
|
//=========================================================
|
||||||
|
|
||||||
|
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
unsigned int WINAPI ExThreadBase::_thread_func(LPVOID pParam)
|
unsigned int WINAPI ExThreadBase::_thread_func(LPVOID pParam)
|
||||||
#else
|
#else
|
||||||
|
|
||||||
void *ExThreadBase::_thread_func(void *pParam)
|
void *ExThreadBase::_thread_func(void *pParam)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
ExThreadBase *_this = (ExThreadBase *) pParam;
|
ExThreadBase *_this = (ExThreadBase *) pParam;
|
||||||
|
|
||||||
_this->m_is_running = true;
|
_this->m_is_running = true;
|
||||||
_this->_thread_loop();
|
_this->_thread_loop();
|
||||||
_this->m_is_running = false;
|
_this->m_is_running = false;
|
||||||
_this->m_handle = 0;
|
_this->m_handle = 0;
|
||||||
|
|
||||||
EXLOGV(" # thread [%s] exit.\n", _this->m_thread_name.c_str());
|
EXLOGV(" # thread [%s] exit.\n", _this->m_thread_name.c_str());
|
||||||
_this->_on_stopped();
|
_this->_on_stopped();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExThreadBase::ExThreadBase(const char *thread_name) :
|
ExThreadBase::ExThreadBase(const char *thread_name) :
|
||||||
m_handle(0),
|
m_handle(0),
|
||||||
m_is_running(false),
|
m_is_running(false),
|
||||||
m_need_stop(false) {
|
m_need_stop(false) {
|
||||||
m_thread_name = thread_name;
|
m_thread_name = thread_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExThreadBase::~ExThreadBase() {
|
ExThreadBase::~ExThreadBase() {
|
||||||
if(m_is_running) {
|
if(m_is_running) {
|
||||||
EXLOGE(" # thread [%s] not stop before destroy.\n", m_thread_name.c_str());
|
EXLOGE(" # thread [%s] not stop before destroy.\n", m_thread_name.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExThreadBase::start(void) {
|
bool ExThreadBase::start(void) {
|
||||||
m_need_stop = false;
|
m_need_stop = false;
|
||||||
EXLOGV(" . thread [%s] starting.\n", m_thread_name.c_str());
|
EXLOGV(" . thread [%s] starting.\n", m_thread_name.c_str());
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
HANDLE h = (HANDLE)_beginthreadex(NULL, 0, _thread_func, (void*)this, 0, NULL);
|
HANDLE h = (HANDLE)_beginthreadex(NULL, 0, _thread_func, (void*)this, 0, NULL);
|
||||||
|
|
||||||
if (NULL == h)
|
if (NULL == h)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
m_handle = h;
|
m_handle = h;
|
||||||
#else
|
#else
|
||||||
pthread_t ptid = 0;
|
pthread_t ptid = 0;
|
||||||
int ret = pthread_create(&ptid, NULL, _thread_func, (void *) this);
|
int ret = pthread_create(&ptid, NULL, _thread_func, (void *) this);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
m_handle = ptid;
|
m_handle = ptid;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExThreadBase::stop(void) {
|
bool ExThreadBase::stop(void) {
|
||||||
if (m_handle == 0) {
|
if (m_handle == 0) {
|
||||||
EXLOGW("[thread] thread [%s] already stopped.\n", m_thread_name.c_str());
|
EXLOGW("[thread] thread [%s] already stopped.\n", m_thread_name.c_str());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
EXLOGV("[thread] try to stop thread [%s].\n", m_thread_name.c_str());
|
EXLOGV("[thread] try to stop thread [%s].\n", m_thread_name.c_str());
|
||||||
m_need_stop = true;
|
m_need_stop = true;
|
||||||
_on_stop();
|
_on_stop();
|
||||||
|
|
||||||
EXLOGV("[thread] wait thread [%s] exit.\n", m_thread_name.c_str());
|
EXLOGV("[thread] wait thread [%s] exit.\n", m_thread_name.c_str());
|
||||||
|
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
if (WaitForSingleObject(m_handle, INFINITE) != WAIT_OBJECT_0)
|
if (WaitForSingleObject(m_handle, INFINITE) != WAIT_OBJECT_0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (pthread_join(m_handle, NULL) != 0) {
|
if (pthread_join(m_handle, NULL) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExThreadBase::terminate(void) {
|
bool ExThreadBase::terminate(void) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
return (TerminateThread(m_handle, 1) == TRUE);
|
return (TerminateThread(m_handle, 1) == TRUE);
|
||||||
#else
|
#else
|
||||||
return (pthread_cancel(m_handle) == 0);
|
return (pthread_cancel(m_handle) == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//=========================================================
|
//=========================================================
|
||||||
//
|
//
|
||||||
//=========================================================
|
//=========================================================
|
||||||
|
|
||||||
ExThreadManager::ExThreadManager() {}
|
ExThreadManager::ExThreadManager() {}
|
||||||
|
|
||||||
ExThreadManager::~ExThreadManager() {
|
ExThreadManager::~ExThreadManager() {
|
||||||
if (!m_threads.empty()) {
|
if (!m_threads.empty()) {
|
||||||
EXLOGE("when destroy thread manager, there are %d thread not exit.\n", m_threads.size());
|
EXLOGE("when destroy thread manager, there are %d thread not exit.\n", m_threads.size());
|
||||||
stop_all();
|
stop_all();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExThreadManager::stop_all(void) {
|
void ExThreadManager::stop_all(void) {
|
||||||
ExThreadSmartLock locker(m_lock);
|
ExThreadSmartLock locker(m_lock);
|
||||||
|
|
||||||
ex_threads::iterator it = m_threads.begin();
|
ex_threads::iterator it = m_threads.begin();
|
||||||
for (; it != m_threads.end(); ++it) {
|
for (; it != m_threads.end(); ++it) {
|
||||||
(*it)->stop();
|
(*it)->stop();
|
||||||
}
|
}
|
||||||
m_threads.clear();
|
m_threads.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExThreadManager::add(ExThreadBase *tb) {
|
void ExThreadManager::add(ExThreadBase *tb) {
|
||||||
ExThreadSmartLock locker(m_lock);
|
ExThreadSmartLock locker(m_lock);
|
||||||
|
|
||||||
ex_threads::iterator it = m_threads.begin();
|
ex_threads::iterator it = m_threads.begin();
|
||||||
for (; it != m_threads.end(); ++it) {
|
for (; it != m_threads.end(); ++it) {
|
||||||
if ((*it) == tb) {
|
if ((*it) == tb) {
|
||||||
EXLOGE("when add thread to manager, it already exist.\n");
|
EXLOGE("when add thread to manager, it already exist.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_threads.push_back(tb);
|
m_threads.push_back(tb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExThreadManager::remove(ExThreadBase *tb) {
|
void ExThreadManager::remove(ExThreadBase *tb) {
|
||||||
ExThreadSmartLock locker(m_lock);
|
ExThreadSmartLock locker(m_lock);
|
||||||
|
|
||||||
ex_threads::iterator it = m_threads.begin();
|
ex_threads::iterator it = m_threads.begin();
|
||||||
for (; it != m_threads.end(); ++it) {
|
for (; it != m_threads.end(); ++it) {
|
||||||
if ((*it) == tb) {
|
if ((*it) == tb) {
|
||||||
m_threads.erase(it);
|
m_threads.erase(it);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EXLOGE("thread not hold by thread-manager while remove it.\n");
|
EXLOGE("thread not hold by thread-manager while remove it.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//=========================================================
|
//=========================================================
|
||||||
//
|
//
|
||||||
//=========================================================
|
//=========================================================
|
||||||
|
|
||||||
ExThreadLock::ExThreadLock() {
|
ExThreadLock::ExThreadLock() {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
InitializeCriticalSection(&m_locker);
|
InitializeCriticalSection(&m_locker);
|
||||||
#else
|
#else
|
||||||
pthread_mutexattr_t attr;
|
pthread_mutexattr_t attr;
|
||||||
pthread_mutexattr_init(&attr);
|
pthread_mutexattr_init(&attr);
|
||||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||||
pthread_mutex_init(&m_locker, &attr);
|
pthread_mutex_init(&m_locker, &attr);
|
||||||
pthread_mutexattr_destroy(&attr);
|
pthread_mutexattr_destroy(&attr);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ExThreadLock::~ExThreadLock() {
|
ExThreadLock::~ExThreadLock() {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
DeleteCriticalSection(&m_locker);
|
DeleteCriticalSection(&m_locker);
|
||||||
#else
|
#else
|
||||||
pthread_mutex_destroy(&m_locker);
|
pthread_mutex_destroy(&m_locker);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExThreadLock::lock(void) {
|
void ExThreadLock::lock(void) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
EnterCriticalSection(&m_locker);
|
EnterCriticalSection(&m_locker);
|
||||||
#else
|
#else
|
||||||
pthread_mutex_lock(&m_locker);
|
pthread_mutex_lock(&m_locker);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExThreadLock::unlock(void) {
|
void ExThreadLock::unlock(void) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
LeaveCriticalSection(&m_locker);
|
LeaveCriticalSection(&m_locker);
|
||||||
#else
|
#else
|
||||||
pthread_mutex_unlock(&m_locker);
|
pthread_mutex_unlock(&m_locker);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//=========================================================
|
//=========================================================
|
||||||
//
|
//
|
||||||
//=========================================================
|
//=========================================================
|
||||||
|
|
||||||
int ex_atomic_add(volatile int *pt, int t) {
|
int ex_atomic_add(volatile int *pt, int t) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
return (int)InterlockedExchangeAdd((long*)pt, (long)t);
|
return (int)InterlockedExchangeAdd((long*)pt, (long)t);
|
||||||
#else
|
#else
|
||||||
return __sync_add_and_fetch(pt, t);
|
return __sync_add_and_fetch(pt, t);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int ex_atomic_inc(volatile int *pt) {
|
int ex_atomic_inc(volatile int *pt) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
return (int)InterlockedIncrement((long*)pt);
|
return (int)InterlockedIncrement((long*)pt);
|
||||||
#else
|
#else
|
||||||
return __sync_add_and_fetch(pt, 1);
|
return __sync_add_and_fetch(pt, 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int ex_atomic_dec(volatile int *pt) {
|
int ex_atomic_dec(volatile int *pt) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
return (int)InterlockedDecrement((long*)pt);
|
return (int)InterlockedDecrement((long*)pt);
|
||||||
#else
|
#else
|
||||||
return __sync_add_and_fetch(pt, -1);
|
return __sync_add_and_fetch(pt, -1);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ex_u64 ex_get_thread_id(void) {
|
ex_u64 ex_get_thread_id(void) {
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
return GetCurrentThreadId();
|
return GetCurrentThreadId();
|
||||||
#else
|
#else
|
||||||
return (ex_u64) pthread_self();
|
return (ex_u64) pthread_self();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include <ex/ex_platform.h>
|
#include <ex/ex_platform.h>
|
||||||
#include <ex/ex_util.h>
|
#include <ex/ex_util.h>
|
||||||
#include <ex/ex_str.h>
|
#include <ex/ex_str.h>
|
||||||
#include <ex/ex_log.h>
|
#include <ex/ex_log.h>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include <ex/ex_winsrv.h>
|
#include <ex/ex_winsrv.h>
|
||||||
|
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
|
|
||||||
|
@ -44,14 +44,14 @@ ex_rv ex_winsrv_install(const ex_wstr& srv_name, const ex_wstr& disp_name, const
|
||||||
}
|
}
|
||||||
|
|
||||||
SERVICE_FAILURE_ACTIONS failure_action;
|
SERVICE_FAILURE_ACTIONS failure_action;
|
||||||
failure_action.dwResetPeriod = 0; // reset failure count to zero 的时间,单位为秒
|
failure_action.dwResetPeriod = 0; // reset failure count to zero 的时间,单位为秒
|
||||||
failure_action.lpRebootMsg = NULL; // Message to broadcast to server users before rebooting
|
failure_action.lpRebootMsg = NULL; // Message to broadcast to server users before rebooting
|
||||||
failure_action.lpCommand = NULL; // Command line of the process for the CreateProcess function to execute in response
|
failure_action.lpCommand = NULL; // Command line of the process for the CreateProcess function to execute in response
|
||||||
failure_action.cActions = 3; // action数组的个数
|
failure_action.cActions = 3; // action数组的个数
|
||||||
|
|
||||||
SC_ACTION actionarray[3];
|
SC_ACTION actionarray[3];
|
||||||
actionarray[0].Type = SC_ACTION_RESTART; // 重新启动服务
|
actionarray[0].Type = SC_ACTION_RESTART; // 重新启动服务
|
||||||
actionarray[0].Delay = 60000; // 单位为毫秒
|
actionarray[0].Delay = 60000; // 单位为毫秒
|
||||||
actionarray[1].Type = SC_ACTION_RESTART;
|
actionarray[1].Type = SC_ACTION_RESTART;
|
||||||
actionarray[1].Delay = 60000;
|
actionarray[1].Delay = 60000;
|
||||||
actionarray[2].Type = SC_ACTION_RESTART;
|
actionarray[2].Type = SC_ACTION_RESTART;
|
||||||
|
|
|
@ -1,151 +1,151 @@
|
||||||
#ifndef __TELEPORT_CONST_H__
|
#ifndef __TELEPORT_CONST_H__
|
||||||
#define __TELEPORT_CONST_H__
|
#define __TELEPORT_CONST_H__
|
||||||
|
|
||||||
// 注意同步更新三个不同语言的const文件
|
// 注意同步更新三个不同语言的const文件
|
||||||
|
|
||||||
// 本文件设定teleport各个模块之间通讯时的错误值(JSON数据),包括:
|
// 本文件设定teleport各个模块之间通讯时的错误值(JSON数据),包括:
|
||||||
// - WEB界面与助手
|
// - WEB界面与助手
|
||||||
// - WEB界面与WEB后台
|
// - WEB界面与WEB后台
|
||||||
// - WEB后台与CORE核心服务
|
// - WEB后台与CORE核心服务
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// Urlprotocol相关
|
// Urlprotocol相关
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_URLPROTO_APP_NAME "teleport"
|
#define TP_URLPROTO_APP_NAME "teleport"
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 远程连接认证方式
|
// 远程连接认证方式
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_AUTH_TYPE_NONE 0
|
#define TP_AUTH_TYPE_NONE 0
|
||||||
#define TP_AUTH_TYPE_PASSWORD 1
|
#define TP_AUTH_TYPE_PASSWORD 1
|
||||||
#define TP_AUTH_TYPE_PRIVATE_KEY 2
|
#define TP_AUTH_TYPE_PRIVATE_KEY 2
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 远程连接协议
|
// 远程连接协议
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_PROTOCOL_TYPE_RDP 1
|
#define TP_PROTOCOL_TYPE_RDP 1
|
||||||
#define TP_PROTOCOL_TYPE_SSH 2
|
#define TP_PROTOCOL_TYPE_SSH 2
|
||||||
#define TP_PROTOCOL_TYPE_TELNET 3
|
#define TP_PROTOCOL_TYPE_TELNET 3
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 远程连接子协议
|
// 远程连接子协议
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_PROTOCOL_TYPE_RDP_DESKTOP 100
|
#define TP_PROTOCOL_TYPE_RDP_DESKTOP 100
|
||||||
#define TP_PROTOCOL_TYPE_SSH_SHELL 200
|
#define TP_PROTOCOL_TYPE_SSH_SHELL 200
|
||||||
#define TP_PROTOCOL_TYPE_SSH_SFTP 201
|
#define TP_PROTOCOL_TYPE_SSH_SFTP 201
|
||||||
#define TP_PROTOCOL_TYPE_TELNET_SHELL 300
|
#define TP_PROTOCOL_TYPE_TELNET_SHELL 300
|
||||||
|
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 远程主机操作系统
|
// 远程主机操作系统
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_OS_TYPE_WINDOWS 1
|
#define TP_OS_TYPE_WINDOWS 1
|
||||||
#define TP_OS_TYPE_LINUX 2
|
#define TP_OS_TYPE_LINUX 2
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 远程连接会话状态
|
// 远程连接会话状态
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_SESS_STAT_RUNNING 0 // 会话开始了,正在连接
|
#define TP_SESS_STAT_RUNNING 0 // 会话开始了,正在连接
|
||||||
#define TP_SESS_STAT_END 9999 // 会话成功结束
|
#define TP_SESS_STAT_END 9999 // 会话成功结束
|
||||||
#define TP_SESS_STAT_ERR_AUTH_DENIED 1 // 会话结束,因为认证失败
|
#define TP_SESS_STAT_ERR_AUTH_DENIED 1 // 会话结束,因为认证失败
|
||||||
#define TP_SESS_STAT_ERR_CONNECT 2 // 会话结束,因为无法连接到远程主机
|
#define TP_SESS_STAT_ERR_CONNECT 2 // 会话结束,因为无法连接到远程主机
|
||||||
#define TP_SESS_STAT_ERR_BAD_SSH_KEY 3 // 会话结束,因为无法识别SSH私钥
|
#define TP_SESS_STAT_ERR_BAD_SSH_KEY 3 // 会话结束,因为无法识别SSH私钥
|
||||||
#define TP_SESS_STAT_ERR_INTERNAL 4 // 会话结束,因为内部错误
|
#define TP_SESS_STAT_ERR_INTERNAL 4 // 会话结束,因为内部错误
|
||||||
#define TP_SESS_STAT_ERR_UNSUPPORT_PROTOCOL 5 // 会话结束,因为协议不支持(RDP)
|
#define TP_SESS_STAT_ERR_UNSUPPORT_PROTOCOL 5 // 会话结束,因为协议不支持(RDP)
|
||||||
#define TP_SESS_STAT_ERR_BAD_PKG 6 // 会话结束,因为收到错误的报文
|
#define TP_SESS_STAT_ERR_BAD_PKG 6 // 会话结束,因为收到错误的报文
|
||||||
#define TP_SESS_STAT_ERR_RESET 7 // 会话结束,因为teleport核心服务重置了
|
#define TP_SESS_STAT_ERR_RESET 7 // 会话结束,因为teleport核心服务重置了
|
||||||
#define TP_SESS_STAT_ERR_IO 8 // 会话结束,因为网络中断
|
#define TP_SESS_STAT_ERR_IO 8 // 会话结束,因为网络中断
|
||||||
#define TP_SESS_STAT_ERR_SESSION 9 // 会话结束,因为无效的会话ID
|
#define TP_SESS_STAT_ERR_SESSION 9 // 会话结束,因为无效的会话ID
|
||||||
#define TP_SESS_STAT_ERR_AUTH_TYPE 10 // 会话结束,因为不被允许的认证方式
|
#define TP_SESS_STAT_ERR_AUTH_TYPE 10 // 会话结束,因为不被允许的认证方式
|
||||||
#define TP_SESS_STAT_STARTED 100 // 已经连接成功了,开始记录录像了
|
#define TP_SESS_STAT_STARTED 100 // 已经连接成功了,开始记录录像了
|
||||||
#define TP_SESS_STAT_ERR_START_INTERNAL 104 // 会话结束,因为内部错误
|
#define TP_SESS_STAT_ERR_START_INTERNAL 104 // 会话结束,因为内部错误
|
||||||
#define TP_SESS_STAT_ERR_START_BAD_PKG 106 // 会话结束,因为收到错误的报文
|
#define TP_SESS_STAT_ERR_START_BAD_PKG 106 // 会话结束,因为收到错误的报文
|
||||||
#define TP_SESS_STAT_ERR_START_RESET 107 // 会话结束,因为teleport核心服务重置了
|
#define TP_SESS_STAT_ERR_START_RESET 107 // 会话结束,因为teleport核心服务重置了
|
||||||
#define TP_SESS_STAT_ERR_START_IO 108 // 会话结束,因为网络中断
|
#define TP_SESS_STAT_ERR_START_IO 108 // 会话结束,因为网络中断
|
||||||
|
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 授权标记
|
// 授权标记
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TP_FLAG_ALL 0xFFFFFFFF
|
#define TP_FLAG_ALL 0xFFFFFFFF
|
||||||
// 会话记录相关
|
// 会话记录相关
|
||||||
#define TP_FLAG_RECORD_REPLAY 0x00000001 // 允许记录历史(录像回放)
|
#define TP_FLAG_RECORD_REPLAY 0x00000001 // 允许记录历史(录像回放)
|
||||||
#define TP_FLAG_RECORD_REAL_TIME 0x00000002 // 允许实时监控
|
#define TP_FLAG_RECORD_REAL_TIME 0x00000002 // 允许实时监控
|
||||||
// RDP相关
|
// RDP相关
|
||||||
#define TP_FLAG_RDP_DESKTOP 0x00000001 // 允许远程桌面
|
#define TP_FLAG_RDP_DESKTOP 0x00000001 // 允许远程桌面
|
||||||
#define TP_FLAG_RDP_CLIPBOARD 0x00000002 // 允许剪贴板
|
#define TP_FLAG_RDP_CLIPBOARD 0x00000002 // 允许剪贴板
|
||||||
#define TP_FLAG_RDP_DISK 0x00000004 // 允许磁盘映射
|
#define TP_FLAG_RDP_DISK 0x00000004 // 允许磁盘映射
|
||||||
#define TP_FLAG_RDP_APP 0x00000008 // 允许远程APP(尚未实现)
|
#define TP_FLAG_RDP_APP 0x00000008 // 允许远程APP(尚未实现)
|
||||||
#define TP_FLAG_RDP_CONSOLE 0x00001000 //允许连接到管理员会话(RDP的console选项)
|
#define TP_FLAG_RDP_CONSOLE 0x00001000 //允许连接到管理员会话(RDP的console选项)
|
||||||
// SSH相关
|
// SSH相关
|
||||||
#define TP_FLAG_SSH_SHELL 0x00000001 // 允许SHELL
|
#define TP_FLAG_SSH_SHELL 0x00000001 // 允许SHELL
|
||||||
#define TP_FLAG_SSH_SFTP 0x00000002 // 允许SFTP
|
#define TP_FLAG_SSH_SFTP 0x00000002 // 允许SFTP
|
||||||
#define TP_FLAG_SSH_X11 0x00000004 // 允许X11转发(尚未实现)
|
#define TP_FLAG_SSH_X11 0x00000004 // 允许X11转发(尚未实现)
|
||||||
#define TP_FLAG_SSH_EXEC 0x00000008 // 允许exec执行远程命令(尚未实现)
|
#define TP_FLAG_SSH_EXEC 0x00000008 // 允许exec执行远程命令(尚未实现)
|
||||||
#define TP_FLAG_SSH_TUNNEL 0x00000010 // allow ssh tunnel. (not impl.)
|
#define TP_FLAG_SSH_TUNNEL 0x00000010 // allow ssh tunnel. (not impl.)
|
||||||
|
|
||||||
|
|
||||||
//=======================================================
|
//=======================================================
|
||||||
// 错误值
|
// 错误值
|
||||||
//=======================================================
|
//=======================================================
|
||||||
#define TPE_OK 0 // 成功
|
#define TPE_OK 0 // 成功
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
// 通用错误值
|
// 通用错误值
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
#define TPE_NEED_MORE_DATA 1 // 需要更多数据(不一定是错误)
|
#define TPE_NEED_MORE_DATA 1 // 需要更多数据(不一定是错误)
|
||||||
#define TPE_NEED_LOGIN 2 // 需要登录
|
#define TPE_NEED_LOGIN 2 // 需要登录
|
||||||
#define TPE_PRIVILEGE 3 // 没有操作权限
|
#define TPE_PRIVILEGE 3 // 没有操作权限
|
||||||
#define TPE_NOT_IMPLEMENT 7 // 功能尚未实现
|
#define TPE_NOT_IMPLEMENT 7 // 功能尚未实现
|
||||||
#define TPE_EXISTS 8 // 目标已经存在
|
#define TPE_EXISTS 8 // 目标已经存在
|
||||||
#define TPE_NOT_EXISTS 9 // 目标不存在
|
#define TPE_NOT_EXISTS 9 // 目标不存在
|
||||||
|
|
||||||
// 100~299是通用错误值
|
// 100~299是通用错误值
|
||||||
|
|
||||||
#define TPE_FAILED 100 // 内部错误
|
#define TPE_FAILED 100 // 内部错误
|
||||||
#define TPE_NETWORK 101 // 网络错误
|
#define TPE_NETWORK 101 // 网络错误
|
||||||
#define TPE_DATABASE 102 // 数据库操作失败
|
#define TPE_DATABASE 102 // 数据库操作失败
|
||||||
|
|
||||||
// HTTP请求相关错误
|
// HTTP请求相关错误
|
||||||
#define TPE_HTTP_METHOD 120 // 无效的请求方法(不是GET/POST等),或者错误的请求方法(例如需要POST,却使用GET方式请求)
|
#define TPE_HTTP_METHOD 120 // 无效的请求方法(不是GET/POST等),或者错误的请求方法(例如需要POST,却使用GET方式请求)
|
||||||
#define TPE_HTTP_URL_ENCODE 121 // URL编码错误(无法解码)
|
#define TPE_HTTP_URL_ENCODE 121 // URL编码错误(无法解码)
|
||||||
//#define TPE_HTTP_URI 122 // 无效的URI
|
//#define TPE_HTTP_URI 122 // 无效的URI
|
||||||
|
|
||||||
#define TPE_UNKNOWN_CMD 124 // 未知的命令
|
#define TPE_UNKNOWN_CMD 124 // 未知的命令
|
||||||
#define TPE_JSON_FORMAT 125 // 错误的JSON格式(需要JSON格式数据,但是却无法按JSON格式解码)
|
#define TPE_JSON_FORMAT 125 // 错误的JSON格式(需要JSON格式数据,但是却无法按JSON格式解码)
|
||||||
#define TPE_PARAM 126 // 参数错误
|
#define TPE_PARAM 126 // 参数错误
|
||||||
#define TPE_DATA 127 // 数据错误
|
#define TPE_DATA 127 // 数据错误
|
||||||
|
|
||||||
// #define TPE_OPENFILE_ERROR 0x1007 // 无法打开文件
|
// #define TPE_OPENFILE_ERROR 0x1007 // 无法打开文件
|
||||||
// #define TPE_GETTEMPPATH_ERROR 0x1007
|
// #define TPE_GETTEMPPATH_ERROR 0x1007
|
||||||
#define TPE_OPENFILE 300
|
#define TPE_OPENFILE 300
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
// WEB服务专用错误值
|
// WEB服务专用错误值
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
#define TPE_CAPTCHA_EXPIRED 10000 // 验证码已过期
|
#define TPE_CAPTCHA_EXPIRED 10000 // 验证码已过期
|
||||||
#define TPE_CAPTCHA_MISMATCH 10001 // 验证码错误
|
#define TPE_CAPTCHA_MISMATCH 10001 // 验证码错误
|
||||||
#define TPE_OATH_MISMATCH 10002 // 身份验证器动态验证码错误
|
#define TPE_OATH_MISMATCH 10002 // 身份验证器动态验证码错误
|
||||||
#define TPE_SYS_MAINTENANCE 10003 // 系统维护中
|
#define TPE_SYS_MAINTENANCE 10003 // 系统维护中
|
||||||
|
|
||||||
#define TPE_USER_LOCKED 10100 // 用户已经被锁定(连续多次错误密码)
|
#define TPE_USER_LOCKED 10100 // 用户已经被锁定(连续多次错误密码)
|
||||||
#define TPE_USER_DISABLED 10101 // 用户已经被禁用
|
#define TPE_USER_DISABLED 10101 // 用户已经被禁用
|
||||||
#define TPE_USER_AUTH 10102 // 身份验证失败
|
#define TPE_USER_AUTH 10102 // 身份验证失败
|
||||||
|
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
// 助手程序专用错误值
|
// 助手程序专用错误值
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
#define TPE_NO_ASSIST 100000 // 未能检测到助手程序
|
#define TPE_NO_ASSIST 100000 // 未能检测到助手程序
|
||||||
#define TPE_OLD_ASSIST 100001 // 助手程序版本太低
|
#define TPE_OLD_ASSIST 100001 // 助手程序版本太低
|
||||||
#define TPE_START_CLIENT 100002 // 无法启动客户端程序(无法创建进程)
|
#define TPE_START_CLIENT 100002 // 无法启动客户端程序(无法创建进程)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
// 核心服务专用错误值
|
// 核心服务专用错误值
|
||||||
//-------------------------------------------------------
|
//-------------------------------------------------------
|
||||||
#define TPE_NO_CORE_SERVER 200000 // 未能检测到核心服务
|
#define TPE_NO_CORE_SERVER 200000 // 未能检测到核心服务
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // __TELEPORT_CONST_H__
|
#endif // __TELEPORT_CONST_H__
|
||||||
|
|
|
@ -1,48 +1,48 @@
|
||||||
#include "base_env.h"
|
#include "base_env.h"
|
||||||
|
|
||||||
TppEnvBase::TppEnvBase()
|
TppEnvBase::TppEnvBase()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
TppEnvBase::~TppEnvBase()
|
TppEnvBase::~TppEnvBase()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool TppEnvBase::init(TPP_INIT_ARGS* args)
|
bool TppEnvBase::init(TPP_INIT_ARGS* args)
|
||||||
{
|
{
|
||||||
if (NULL == args)
|
if (NULL == args)
|
||||||
{
|
{
|
||||||
EXLOGE("invalid init args(1).\n");
|
EXLOGE("invalid init args(1).\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
EXLOG_USE_LOGGER(args->logger);
|
EXLOG_USE_LOGGER(args->logger);
|
||||||
|
|
||||||
exec_path = args->exec_path;
|
exec_path = args->exec_path;
|
||||||
etc_path = args->etc_path;
|
etc_path = args->etc_path;
|
||||||
replay_path = args->replay_path;
|
replay_path = args->replay_path;
|
||||||
|
|
||||||
get_connect_info = args->func_get_connect_info;
|
get_connect_info = args->func_get_connect_info;
|
||||||
free_connect_info = args->func_free_connect_info;
|
free_connect_info = args->func_free_connect_info;
|
||||||
session_begin = args->func_session_begin;
|
session_begin = args->func_session_begin;
|
||||||
session_update = args->func_session_update;
|
session_update = args->func_session_update;
|
||||||
session_end = args->func_session_end;
|
session_end = args->func_session_end;
|
||||||
|
|
||||||
if (NULL == get_connect_info || NULL == free_connect_info || NULL == session_begin || NULL == session_update || NULL == session_end)
|
if (NULL == get_connect_info || NULL == free_connect_info || NULL == session_begin || NULL == session_update || NULL == session_end)
|
||||||
{
|
{
|
||||||
EXLOGE("invalid init args(2).\n");
|
EXLOGE("invalid init args(2).\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NULL == args->cfg)
|
if (NULL == args->cfg)
|
||||||
{
|
{
|
||||||
EXLOGE("invalid init args(3).\n");
|
EXLOGE("invalid init args(3).\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_on_init(args))
|
if (!_on_init(args))
|
||||||
{
|
{
|
||||||
EXLOGE("invalid init args(4).\n");
|
EXLOGE("invalid init args(4).\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
#ifndef __TS_BASE_ENV_H__
|
#ifndef __TS_BASE_ENV_H__
|
||||||
#define __TS_BASE_ENV_H__
|
#define __TS_BASE_ENV_H__
|
||||||
|
|
||||||
#include "protocol_interface.h"
|
#include "protocol_interface.h"
|
||||||
|
|
||||||
class TppEnvBase
|
class TppEnvBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TppEnvBase();
|
TppEnvBase();
|
||||||
virtual ~TppEnvBase();
|
virtual ~TppEnvBase();
|
||||||
|
|
||||||
bool init(TPP_INIT_ARGS* args);
|
bool init(TPP_INIT_ARGS* args);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ex_wstr exec_path;
|
ex_wstr exec_path;
|
||||||
ex_wstr etc_path; // 配置文件、SSH服务器的私钥文件的存放路径
|
ex_wstr etc_path; // 配置文件、SSH服务器的私钥文件的存放路径
|
||||||
ex_wstr replay_path;
|
ex_wstr replay_path;
|
||||||
|
|
||||||
TPP_GET_CONNNECT_INFO_FUNC get_connect_info;
|
TPP_GET_CONNNECT_INFO_FUNC get_connect_info;
|
||||||
TPP_FREE_CONNECT_INFO_FUNC free_connect_info;
|
TPP_FREE_CONNECT_INFO_FUNC free_connect_info;
|
||||||
TPP_SESSION_BEGIN_FUNC session_begin;
|
TPP_SESSION_BEGIN_FUNC session_begin;
|
||||||
TPP_SESSION_UPDATE_FUNC session_update;
|
TPP_SESSION_UPDATE_FUNC session_update;
|
||||||
TPP_SESSION_END_FUNC session_end;
|
TPP_SESSION_END_FUNC session_end;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool _on_init(TPP_INIT_ARGS* args) = 0;
|
virtual bool _on_init(TPP_INIT_ARGS* args) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __TS_BASE_ENV_H__
|
#endif // __TS_BASE_ENV_H__
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "base_record.h"
|
#include "base_record.h"
|
||||||
|
|
||||||
|
|
|
@ -1,101 +1,101 @@
|
||||||
#ifndef __TS_BASE_RECORD_H__
|
#ifndef __TS_BASE_RECORD_H__
|
||||||
#define __TS_BASE_RECORD_H__
|
#define __TS_BASE_RECORD_H__
|
||||||
|
|
||||||
#include "base_env.h"
|
#include "base_env.h"
|
||||||
#include "ts_membuf.h"
|
#include "ts_membuf.h"
|
||||||
#include "protocol_interface.h"
|
#include "protocol_interface.h"
|
||||||
|
|
||||||
#include <ex.h>
|
#include <ex.h>
|
||||||
|
|
||||||
#define MAX_SIZE_PER_FILE 4194304 // 4M = 1024*1024*4
|
#define MAX_SIZE_PER_FILE 4194304 // 4M = 1024*1024*4
|
||||||
|
|
||||||
#pragma pack(push,1)
|
#pragma pack(push,1)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 录像
|
* 录像
|
||||||
*
|
*
|
||||||
* 一个录像分为两个文件,一个信息文件,一个数据文件。
|
* 一个录像分为两个文件,一个信息文件,一个数据文件。
|
||||||
* 服务内部缓存最大4M,或者5秒,就将数据写入数据文件中,并同时更新信息文件。
|
* 服务内部缓存最大4M,或者5秒,就将数据写入数据文件中,并同时更新信息文件。
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
// 录像文件头(随着录像数据写入,会改变的部分)
|
// 录像文件头(随着录像数据写入,会改变的部分)
|
||||||
typedef struct TS_RECORD_HEADER_INFO
|
typedef struct TS_RECORD_HEADER_INFO
|
||||||
{
|
{
|
||||||
ex_u32 magic; // "TPPR" 标志 TelePort Protocol Record
|
ex_u32 magic; // "TPPR" 标志 TelePort Protocol Record
|
||||||
ex_u16 ver; // 录像文件版本,目前为3
|
ex_u16 ver; // 录像文件版本,目前为3
|
||||||
ex_u32 packages; // 总包数
|
ex_u32 packages; // 总包数
|
||||||
ex_u32 time_ms; // 总耗时(毫秒)
|
ex_u32 time_ms; // 总耗时(毫秒)
|
||||||
//ex_u32 file_size; // 数据文件大小
|
//ex_u32 file_size; // 数据文件大小
|
||||||
}TS_RECORD_HEADER_INFO;
|
}TS_RECORD_HEADER_INFO;
|
||||||
#define ts_record_header_info_size sizeof(TS_RECORD_HEADER_INFO)
|
#define ts_record_header_info_size sizeof(TS_RECORD_HEADER_INFO)
|
||||||
|
|
||||||
// 录像文件头(固定不变部分)
|
// 录像文件头(固定不变部分)
|
||||||
typedef struct TS_RECORD_HEADER_BASIC
|
typedef struct TS_RECORD_HEADER_BASIC
|
||||||
{
|
{
|
||||||
ex_u16 protocol_type; // 协议:1=RDP, 2=SSH, 3=Telnet
|
ex_u16 protocol_type; // 协议:1=RDP, 2=SSH, 3=Telnet
|
||||||
ex_u16 protocol_sub_type; // 子协议:100=RDP-DESKTOP, 200=SSH-SHELL, 201=SSH-SFTP, 300=Telnet
|
ex_u16 protocol_sub_type; // 子协议:100=RDP-DESKTOP, 200=SSH-SHELL, 201=SSH-SFTP, 300=Telnet
|
||||||
ex_u64 timestamp; // 本次录像的起始时间(UTC时间戳)
|
ex_u64 timestamp; // 本次录像的起始时间(UTC时间戳)
|
||||||
ex_u16 width; // 初始屏幕尺寸:宽
|
ex_u16 width; // 初始屏幕尺寸:宽
|
||||||
ex_u16 height; // 初始屏幕尺寸:高
|
ex_u16 height; // 初始屏幕尺寸:高
|
||||||
char user_username[64]; // teleport账号
|
char user_username[64]; // teleport账号
|
||||||
char acc_username[64]; // 远程主机用户名
|
char acc_username[64]; // 远程主机用户名
|
||||||
|
|
||||||
char host_ip[40]; // 远程主机IP
|
char host_ip[40]; // 远程主机IP
|
||||||
char conn_ip[40]; // 远程主机IP
|
char conn_ip[40]; // 远程主机IP
|
||||||
ex_u16 conn_port; // 远程主机端口
|
ex_u16 conn_port; // 远程主机端口
|
||||||
|
|
||||||
char client_ip[40]; // 客户端IP
|
char client_ip[40]; // 客户端IP
|
||||||
|
|
||||||
// RDP专有
|
// RDP专有
|
||||||
ex_u8 rdp_security; // 0 = RDP, 1 = TLS
|
ex_u8 rdp_security; // 0 = RDP, 1 = TLS
|
||||||
|
|
||||||
ex_u8 _reserve[512 - 2 - 2 - 8 - 2 - 2 - 64 - 64 - 40 - 40 - 2 - 40 - 1 - ts_record_header_info_size];
|
ex_u8 _reserve[512 - 2 - 2 - 8 - 2 - 2 - 64 - 64 - 40 - 40 - 2 - 40 - 1 - ts_record_header_info_size];
|
||||||
}TS_RECORD_HEADER_BASIC;
|
}TS_RECORD_HEADER_BASIC;
|
||||||
#define ts_record_header_basic_size sizeof(TS_RECORD_HEADER_BASIC)
|
#define ts_record_header_basic_size sizeof(TS_RECORD_HEADER_BASIC)
|
||||||
|
|
||||||
typedef struct TS_RECORD_HEADER
|
typedef struct TS_RECORD_HEADER
|
||||||
{
|
{
|
||||||
TS_RECORD_HEADER_INFO info;
|
TS_RECORD_HEADER_INFO info;
|
||||||
TS_RECORD_HEADER_BASIC basic;
|
TS_RECORD_HEADER_BASIC basic;
|
||||||
}TS_RECORD_HEADER;
|
}TS_RECORD_HEADER;
|
||||||
|
|
||||||
// header部分(header-info + header-basic) = 512B
|
// header部分(header-info + header-basic) = 512B
|
||||||
#define ts_record_header_size sizeof(TS_RECORD_HEADER)
|
#define ts_record_header_size sizeof(TS_RECORD_HEADER)
|
||||||
|
|
||||||
|
|
||||||
// 一个数据包的头
|
// 一个数据包的头
|
||||||
typedef struct TS_RECORD_PKG
|
typedef struct TS_RECORD_PKG
|
||||||
{
|
{
|
||||||
ex_u8 type; // 包的数据类型
|
ex_u8 type; // 包的数据类型
|
||||||
ex_u32 size; // 这个包的总大小(不含包头)
|
ex_u32 size; // 这个包的总大小(不含包头)
|
||||||
ex_u32 time_ms; // 这个包距起始时间的时间差(毫秒,意味着一个连接不能持续超过49天)
|
ex_u32 time_ms; // 这个包距起始时间的时间差(毫秒,意味着一个连接不能持续超过49天)
|
||||||
ex_u8 _reserve[3]; // 保留
|
ex_u8 _reserve[3]; // 保留
|
||||||
}TS_RECORD_PKG;
|
}TS_RECORD_PKG;
|
||||||
|
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
class TppRecBase
|
class TppRecBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TppRecBase();
|
TppRecBase();
|
||||||
virtual ~TppRecBase();
|
virtual ~TppRecBase();
|
||||||
|
|
||||||
bool begin(const wchar_t* base_path, const wchar_t* base_fname, int record_id, const TPP_CONNECT_INFO* info);
|
bool begin(const wchar_t* base_path, const wchar_t* base_fname, int record_id, const TPP_CONNECT_INFO* info);
|
||||||
bool end();
|
bool end();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool _on_begin(const TPP_CONNECT_INFO* info) = 0;
|
virtual bool _on_begin(const TPP_CONNECT_INFO* info) = 0;
|
||||||
virtual bool _on_end() = 0;
|
virtual bool _on_end() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ex_wstr m_base_path; // 录像文件基础路径,例如 /usr/local/teleport/data/replay/ssh/123,数字编号是内部附加的,作为本次会话录像文件的目录名称
|
ex_wstr m_base_path; // 录像文件基础路径,例如 /usr/local/teleport/data/replay/ssh/123,数字编号是内部附加的,作为本次会话录像文件的目录名称
|
||||||
ex_wstr m_base_fname; // 录像文件的文件名,不含扩展名部分,内部会以此为基础合成文件全名,并将录像文件存放在 m_base_path 指向的目录中
|
ex_wstr m_base_fname; // 录像文件的文件名,不含扩展名部分,内部会以此为基础合成文件全名,并将录像文件存放在 m_base_path 指向的目录中
|
||||||
|
|
||||||
ex_u64 m_start_time;
|
ex_u64 m_start_time;
|
||||||
|
|
||||||
MemBuffer m_cache;
|
MemBuffer m_cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __TS_BASE_RECORD_H__
|
#endif // __TS_BASE_RECORD_H__
|
||||||
|
|
|
@ -1,100 +1,100 @@
|
||||||
#ifndef __TP_PROTOCOL_INTERFACE_H__
|
#ifndef __TP_PROTOCOL_INTERFACE_H__
|
||||||
#define __TP_PROTOCOL_INTERFACE_H__
|
#define __TP_PROTOCOL_INTERFACE_H__
|
||||||
|
|
||||||
#include "ts_const.h"
|
#include "ts_const.h"
|
||||||
#include <ex.h>
|
#include <ex.h>
|
||||||
|
|
||||||
#ifdef EX_OS_WIN32
|
#ifdef EX_OS_WIN32
|
||||||
# ifdef TPP_EXPORTS
|
# ifdef TPP_EXPORTS
|
||||||
# define TPP_API __declspec(dllexport)
|
# define TPP_API __declspec(dllexport)
|
||||||
# else
|
# else
|
||||||
# define TPP_API __declspec(dllimport)
|
# define TPP_API __declspec(dllimport)
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# define TPP_API
|
# define TPP_API
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TPP_CMD_INIT 0x00000000
|
#define TPP_CMD_INIT 0x00000000
|
||||||
#define TPP_CMD_SET_RUNTIME_CFG 0x00000005
|
#define TPP_CMD_SET_RUNTIME_CFG 0x00000005
|
||||||
#define TPP_CMD_KILL_SESSIONS 0x00000006
|
#define TPP_CMD_KILL_SESSIONS 0x00000006
|
||||||
|
|
||||||
typedef struct TPP_CONNECT_INFO
|
typedef struct TPP_CONNECT_INFO
|
||||||
{
|
{
|
||||||
char* sid;
|
char* sid;
|
||||||
|
|
||||||
// 与此连接信息相关的三个要素的ID
|
// 与此连接信息相关的三个要素的ID
|
||||||
int user_id;
|
int user_id;
|
||||||
int host_id;
|
int host_id;
|
||||||
int acc_id;
|
int acc_id;
|
||||||
|
|
||||||
char* user_username; // 申请本次连接的用户名
|
char* user_username; // 申请本次连接的用户名
|
||||||
|
|
||||||
char* host_ip; // 真正的远程主机IP(如果是直接连接模式,则与remote_host_ip相同)
|
char* host_ip; // 真正的远程主机IP(如果是直接连接模式,则与remote_host_ip相同)
|
||||||
char* conn_ip; // 要连接的远程主机的IP(如果是端口映射模式,则为路由主机的IP)
|
char* conn_ip; // 要连接的远程主机的IP(如果是端口映射模式,则为路由主机的IP)
|
||||||
int conn_port; // 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口)
|
int conn_port; // 要连接的远程主机的端口(如果是端口映射模式,则为路由主机的端口)
|
||||||
char* client_ip;
|
char* client_ip;
|
||||||
|
|
||||||
char* acc_username; // 远程主机的账号
|
char* acc_username; // 远程主机的账号
|
||||||
char* acc_secret; // 远程主机账号的密码(或者私钥)
|
char* acc_secret; // 远程主机账号的密码(或者私钥)
|
||||||
char* username_prompt; // for telnet
|
char* username_prompt; // for telnet
|
||||||
char* password_prompt; // for telnet
|
char* password_prompt; // for telnet
|
||||||
|
|
||||||
int protocol_type;
|
int protocol_type;
|
||||||
int protocol_sub_type;
|
int protocol_sub_type;
|
||||||
int protocol_flag;
|
int protocol_flag;
|
||||||
int record_flag;
|
int record_flag;
|
||||||
int auth_type;
|
int auth_type;
|
||||||
}TPP_CONNECT_INFO;
|
}TPP_CONNECT_INFO;
|
||||||
|
|
||||||
typedef TPP_CONNECT_INFO* (*TPP_GET_CONNNECT_INFO_FUNC)(const char* sid);
|
typedef TPP_CONNECT_INFO* (*TPP_GET_CONNNECT_INFO_FUNC)(const char* sid);
|
||||||
typedef void(*TPP_FREE_CONNECT_INFO_FUNC)(TPP_CONNECT_INFO* info);
|
typedef void(*TPP_FREE_CONNECT_INFO_FUNC)(TPP_CONNECT_INFO* info);
|
||||||
typedef bool(*TPP_SESSION_BEGIN_FUNC)(const TPP_CONNECT_INFO* info, int* db_id);
|
typedef bool(*TPP_SESSION_BEGIN_FUNC)(const TPP_CONNECT_INFO* info, int* db_id);
|
||||||
typedef bool(*TPP_SESSION_UPDATE_FUNC)(int db_id, int protocol_sub_type, int state);
|
typedef bool(*TPP_SESSION_UPDATE_FUNC)(int db_id, int protocol_sub_type, int state);
|
||||||
typedef bool(*TPP_SESSION_END_FUNC)(const char* sid, int db_id, int ret);
|
typedef bool(*TPP_SESSION_END_FUNC)(const char* sid, int db_id, int ret);
|
||||||
|
|
||||||
|
|
||||||
typedef struct TPP_INIT_ARGS
|
typedef struct TPP_INIT_ARGS
|
||||||
{
|
{
|
||||||
ExLogger* logger;
|
ExLogger* logger;
|
||||||
ex_wstr exec_path;
|
ex_wstr exec_path;
|
||||||
ex_wstr etc_path;
|
ex_wstr etc_path;
|
||||||
ex_wstr replay_path;
|
ex_wstr replay_path;
|
||||||
ExIniFile* cfg;
|
ExIniFile* cfg;
|
||||||
|
|
||||||
TPP_GET_CONNNECT_INFO_FUNC func_get_connect_info;
|
TPP_GET_CONNNECT_INFO_FUNC func_get_connect_info;
|
||||||
TPP_FREE_CONNECT_INFO_FUNC func_free_connect_info;
|
TPP_FREE_CONNECT_INFO_FUNC func_free_connect_info;
|
||||||
TPP_SESSION_BEGIN_FUNC func_session_begin;
|
TPP_SESSION_BEGIN_FUNC func_session_begin;
|
||||||
TPP_SESSION_UPDATE_FUNC func_session_update;
|
TPP_SESSION_UPDATE_FUNC func_session_update;
|
||||||
TPP_SESSION_END_FUNC func_session_end;
|
TPP_SESSION_END_FUNC func_session_end;
|
||||||
}TPP_INIT_ARGS;
|
}TPP_INIT_ARGS;
|
||||||
|
|
||||||
// typedef struct TPP_SET_CFG_ARGS {
|
// typedef struct TPP_SET_CFG_ARGS {
|
||||||
// ex_u32 noop_timeout; // as second.
|
// ex_u32 noop_timeout; // as second.
|
||||||
// }TPP_SET_CFG_ARGS;
|
// }TPP_SET_CFG_ARGS;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TPP_API ex_rv tpp_init(TPP_INIT_ARGS* init_args);
|
TPP_API ex_rv tpp_init(TPP_INIT_ARGS* init_args);
|
||||||
TPP_API ex_rv tpp_start(void);
|
TPP_API ex_rv tpp_start(void);
|
||||||
TPP_API ex_rv tpp_stop(void);
|
TPP_API ex_rv tpp_stop(void);
|
||||||
TPP_API void tpp_timer(void);
|
TPP_API void tpp_timer(void);
|
||||||
// TPP_API void tpp_set_cfg(TPP_SET_CFG_ARGS* cfg_args);
|
// TPP_API void tpp_set_cfg(TPP_SET_CFG_ARGS* cfg_args);
|
||||||
|
|
||||||
TPP_API ex_rv tpp_command(ex_u32 cmd, const char* param);
|
TPP_API ex_rv tpp_command(ex_u32 cmd, const char* param);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef ex_rv (*TPP_INIT_FUNC)(TPP_INIT_ARGS* init_args);
|
typedef ex_rv (*TPP_INIT_FUNC)(TPP_INIT_ARGS* init_args);
|
||||||
typedef ex_rv (*TPP_START_FUNC)(void);
|
typedef ex_rv (*TPP_START_FUNC)(void);
|
||||||
typedef ex_rv(*TPP_STOP_FUNC)(void);
|
typedef ex_rv(*TPP_STOP_FUNC)(void);
|
||||||
typedef void(*TPP_TIMER_FUNC)(void);
|
typedef void(*TPP_TIMER_FUNC)(void);
|
||||||
// typedef void(*TPP_SET_CFG_FUNC)(TPP_SET_CFG_ARGS* cfg_args);
|
// typedef void(*TPP_SET_CFG_FUNC)(TPP_SET_CFG_ARGS* cfg_args);
|
||||||
|
|
||||||
typedef ex_rv(*TPP_COMMAND_FUNC)(ex_u32 cmd, const char* param); // param is a JSON formatted string.
|
typedef ex_rv(*TPP_COMMAND_FUNC)(ex_u32 cmd, const char* param); // param is a JSON formatted string.
|
||||||
|
|
||||||
#endif // __TP_PROTOCOL_INTERFACE_H__
|
#endif // __TP_PROTOCOL_INTERFACE_H__
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
#ifndef __TS_ERRNO_H__
|
#ifndef __TS_ERRNO_H__
|
||||||
#define __TS_ERRNO_H__
|
#define __TS_ERRNO_H__
|
||||||
|
|
||||||
//#include "ts_types.h"
|
//#include "ts_types.h"
|
||||||
|
|
||||||
#define TS_RDP_PROXY_PORT 52089
|
#define TS_RDP_PROXY_PORT 52089
|
||||||
#define TS_RDP_PROXY_HOST "0.0.0.0"
|
#define TS_RDP_PROXY_HOST "0.0.0.0"
|
||||||
|
|
||||||
#define TS_SSH_PROXY_PORT 52189
|
#define TS_SSH_PROXY_PORT 52189
|
||||||
#define TS_SSH_PROXY_HOST "0.0.0.0"
|
#define TS_SSH_PROXY_HOST "0.0.0.0"
|
||||||
|
|
||||||
#define TS_TELNET_PROXY_PORT 52389
|
#define TS_TELNET_PROXY_PORT 52389
|
||||||
#define TS_TELNET_PROXY_HOST "0.0.0.0"
|
#define TS_TELNET_PROXY_HOST "0.0.0.0"
|
||||||
|
|
||||||
#define TS_HTTP_RPC_PORT 52080
|
#define TS_HTTP_RPC_PORT 52080
|
||||||
//#define TS_HTTP_RPC_HOST "127.0.0.1"
|
//#define TS_HTTP_RPC_HOST "127.0.0.1"
|
||||||
#define TS_HTTP_RPC_HOST "localhost"
|
#define TS_HTTP_RPC_HOST "localhost"
|
||||||
|
|
||||||
|
|
||||||
#define TS_RDP_PROTOCOL_RDP 0
|
#define TS_RDP_PROTOCOL_RDP 0
|
||||||
#define TS_RDP_PROTOCOL_TLS 1
|
#define TS_RDP_PROTOCOL_TLS 1
|
||||||
#define TS_RDP_PROTOCOL_HYBRID 2
|
#define TS_RDP_PROTOCOL_HYBRID 2
|
||||||
#define TS_RDP_PROTOCOL_RDSTLS 4
|
#define TS_RDP_PROTOCOL_RDSTLS 4
|
||||||
#define TS_RDP_PROTOCOL_HYBRID_EX 8
|
#define TS_RDP_PROTOCOL_HYBRID_EX 8
|
||||||
|
|
||||||
#endif // __TS_ERRNO_H__
|
#endif // __TS_ERRNO_H__
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#include "ts_membuf.h"
|
#include "ts_membuf.h"
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
|
||||||
MemBuffer::MemBuffer()// : m_buffer(NULL), m_data_size(0), m_buffer_size(0)
|
MemBuffer::MemBuffer()// : m_buffer(NULL), m_data_size(0), m_buffer_size(0)
|
||||||
|
@ -45,7 +45,7 @@ void MemBuffer::reserve(size_t size)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将新的缓冲区大小取整到 MEMBUF_BLOCK_SIZE 的整数倍
|
// 将新的缓冲区大小取整到 MEMBUF_BLOCK_SIZE 的整数倍
|
||||||
size_t new_size = (size + MEMBUF_BLOCK_SIZE - 1) & ~(MEMBUF_BLOCK_SIZE - 1);
|
size_t new_size = (size + MEMBUF_BLOCK_SIZE - 1) & ~(MEMBUF_BLOCK_SIZE - 1);
|
||||||
//EXLOGD("[mbuf:%p] reserve(2): m_buf: %p, m_buf_size: %d, size: %d, new size: %d.\n", this, m_buffer, m_buffer_size, size, new_size);
|
//EXLOGD("[mbuf:%p] reserve(2): m_buf: %p, m_buf_size: %d, size: %d, new size: %d.\n", this, m_buffer, m_buffer_size, size, new_size);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#ifndef __TS_MEMBUF_H__
|
#ifndef __TS_MEMBUF_H__
|
||||||
#define __TS_MEMBUF_H__
|
#define __TS_MEMBUF_H__
|
||||||
|
|
||||||
#include <ex.h>
|
#include <ex.h>
|
||||||
|
@ -11,15 +11,15 @@ public:
|
||||||
MemBuffer();
|
MemBuffer();
|
||||||
virtual ~MemBuffer();
|
virtual ~MemBuffer();
|
||||||
|
|
||||||
// 附加size字节的数据到缓冲区末尾(可能会导致缓冲区扩大)
|
// 附加size字节的数据到缓冲区末尾(可能会导致缓冲区扩大)
|
||||||
void append(const ex_u8* data, size_t size);
|
void append(const ex_u8* data, size_t size);
|
||||||
// 缓冲区至少为指定字节数(可能会扩大缓冲区,但不会缩小缓冲区,保证有效数据不会被改变)
|
// 缓冲区至少为指定字节数(可能会扩大缓冲区,但不会缩小缓冲区,保证有效数据不会被改变)
|
||||||
void reserve(size_t size);
|
void reserve(size_t size);
|
||||||
// 将m的有效数据附加到自己的有效数据末尾,可能会扩大缓冲区,m内容不变
|
// 将m的有效数据附加到自己的有效数据末尾,可能会扩大缓冲区,m内容不变
|
||||||
void concat(const MemBuffer& m);
|
void concat(const MemBuffer& m);
|
||||||
// 从缓冲区头部移除size字节(缓冲区大小可能并不会收缩),剩下的有效数据前移。
|
// 从缓冲区头部移除size字节(缓冲区大小可能并不会收缩),剩下的有效数据前移。
|
||||||
void pop(size_t size);
|
void pop(size_t size);
|
||||||
// 清空缓冲区(有效数据为0字节,缓冲区不变)
|
// 清空缓冲区(有效数据为0字节,缓冲区不变)
|
||||||
void empty(void) { m_data_size = 0; }
|
void empty(void) { m_data_size = 0; }
|
||||||
bool is_empty(void) { return m_data_size == 0; }
|
bool is_empty(void) { return m_data_size == 0; }
|
||||||
|
|
||||||
|
|
|
@ -1,222 +1,222 @@
|
||||||
#include "ts_memstream.h"
|
#include "ts_memstream.h"
|
||||||
|
|
||||||
MemStream::MemStream(MemBuffer& mbuf) : m_mbuf(mbuf)
|
MemStream::MemStream(MemBuffer& mbuf) : m_mbuf(mbuf)
|
||||||
{
|
{
|
||||||
m_offset = 0;
|
m_offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
MemStream::~MemStream()
|
MemStream::~MemStream()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void MemStream::reset(void)
|
void MemStream::reset(void)
|
||||||
{
|
{
|
||||||
m_mbuf.empty();
|
m_mbuf.empty();
|
||||||
rewind();
|
rewind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MemStream::seek(size_t offset)
|
bool MemStream::seek(size_t offset)
|
||||||
{
|
{
|
||||||
if (offset > m_mbuf.size())
|
if (offset > m_mbuf.size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_offset = offset;
|
m_offset = offset;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemStream::skip(size_t n)
|
bool MemStream::skip(size_t n)
|
||||||
{
|
{
|
||||||
if (0 == n)
|
if (0 == n)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (m_offset + n > m_mbuf.size())
|
if (m_offset + n > m_mbuf.size())
|
||||||
return false;
|
return false;
|
||||||
m_offset += n;
|
m_offset += n;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MemStream::rewind(size_t n)
|
bool MemStream::rewind(size_t n)
|
||||||
{
|
{
|
||||||
if (m_offset < n)
|
if (m_offset < n)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (0 == n)
|
if (0 == n)
|
||||||
m_offset = 0;
|
m_offset = 0;
|
||||||
else
|
else
|
||||||
m_offset -= n;
|
m_offset -= n;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ex_u8 MemStream::get_u8(void)
|
ex_u8 MemStream::get_u8(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 1 <= m_mbuf.size());
|
ASSERT(m_offset + 1 <= m_mbuf.size());
|
||||||
|
|
||||||
ex_u8 v = (m_mbuf.data() + m_offset)[0];
|
ex_u8 v = (m_mbuf.data() + m_offset)[0];
|
||||||
m_offset++;
|
m_offset++;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ex_u16 MemStream::get_u16_le(void)
|
ex_u16 MemStream::get_u16_le(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 2 <= m_mbuf.size());
|
ASSERT(m_offset + 2 <= m_mbuf.size());
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
ex_u16 v = (ex_u16)(p[0] | (p[1] << 8));
|
ex_u16 v = (ex_u16)(p[0] | (p[1] << 8));
|
||||||
#else
|
#else
|
||||||
ex_u16 v = ((ex_u16*)p)[0];
|
ex_u16 v = ((ex_u16*)p)[0];
|
||||||
#endif
|
#endif
|
||||||
m_offset += 2;
|
m_offset += 2;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ex_u16 MemStream::get_u16_be(void)
|
ex_u16 MemStream::get_u16_be(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 2 <= m_mbuf.size());
|
ASSERT(m_offset + 2 <= m_mbuf.size());
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
ex_u16 v = ((ex_u16*)p)[0];
|
ex_u16 v = ((ex_u16*)p)[0];
|
||||||
#else
|
#else
|
||||||
ex_u16 v = (ex_u16)((p[0] << 8) | p[1]);
|
ex_u16 v = (ex_u16)((p[0] << 8) | p[1]);
|
||||||
#endif
|
#endif
|
||||||
m_offset += 2;
|
m_offset += 2;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ex_u32 MemStream::get_u32_le(void)
|
ex_u32 MemStream::get_u32_le(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 4 <= m_mbuf.size());
|
ASSERT(m_offset + 4 <= m_mbuf.size());
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
ex_u32 v = (ex_u32)(p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
|
ex_u32 v = (ex_u32)(p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
|
||||||
#else
|
#else
|
||||||
ex_u32 v = ((ex_u32*)p)[0];
|
ex_u32 v = ((ex_u32*)p)[0];
|
||||||
#endif
|
#endif
|
||||||
m_offset += 4;
|
m_offset += 4;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ex_u32 MemStream::get_u32_be(void)
|
ex_u32 MemStream::get_u32_be(void)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 4 <= m_mbuf.size());
|
ASSERT(m_offset + 4 <= m_mbuf.size());
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
ex_u32 v = ((ex_u32*)p)[0];
|
ex_u32 v = ((ex_u32*)p)[0];
|
||||||
#else
|
#else
|
||||||
ex_u32 v = (ex_u32)((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
|
ex_u32 v = (ex_u32)((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
|
||||||
#endif
|
#endif
|
||||||
m_offset += 4;
|
m_offset += 4;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ex_u8* MemStream::get_bin(size_t n)
|
ex_u8* MemStream::get_bin(size_t n)
|
||||||
{
|
{
|
||||||
ASSERT(m_offset + 4 <= m_mbuf.size());
|
ASSERT(m_offset + 4 <= m_mbuf.size());
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
m_offset += n;
|
m_offset += n;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MemStream::put_zero(size_t n)
|
void MemStream::put_zero(size_t n)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + n);
|
m_mbuf.reserve(m_mbuf.size() + n);
|
||||||
memset(m_mbuf.data() + m_offset, 0, n);
|
memset(m_mbuf.data() + m_offset, 0, n);
|
||||||
m_offset += n;
|
m_offset += n;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_u8(ex_u8 v)
|
void MemStream::put_u8(ex_u8 v)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + 1);
|
m_mbuf.reserve(m_mbuf.size() + 1);
|
||||||
|
|
||||||
(m_mbuf.data() + m_offset)[0] = v;
|
(m_mbuf.data() + m_offset)[0] = v;
|
||||||
m_offset++;
|
m_offset++;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_u16_le(ex_u16 v)
|
void MemStream::put_u16_le(ex_u16 v)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + 2);
|
m_mbuf.reserve(m_mbuf.size() + 2);
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
p[0] = (ex_u8)v;
|
p[0] = (ex_u8)v;
|
||||||
p[1] = (ex_u8)(v >> 8);
|
p[1] = (ex_u8)(v >> 8);
|
||||||
#else
|
#else
|
||||||
((ex_u16*)p)[0] = v;
|
((ex_u16*)p)[0] = v;
|
||||||
#endif
|
#endif
|
||||||
m_offset += 2;
|
m_offset += 2;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_u16_be(ex_u16 v)
|
void MemStream::put_u16_be(ex_u16 v)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + 2);
|
m_mbuf.reserve(m_mbuf.size() + 2);
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
((ex_u16*)p)[0] = v;
|
((ex_u16*)p)[0] = v;
|
||||||
#else
|
#else
|
||||||
ex_u8* _v = (ex_u8*)&v;
|
ex_u8* _v = (ex_u8*)&v;
|
||||||
p[0] = _v[1];
|
p[0] = _v[1];
|
||||||
p[1] = _v[0];
|
p[1] = _v[0];
|
||||||
#endif
|
#endif
|
||||||
m_offset += 2;
|
m_offset += 2;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_u32_le(ex_u32 v)
|
void MemStream::put_u32_le(ex_u32 v)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + 4);
|
m_mbuf.reserve(m_mbuf.size() + 4);
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
p[0] = (ex_u8)v;
|
p[0] = (ex_u8)v;
|
||||||
p[1] = (ex_u8)(v >> 8);
|
p[1] = (ex_u8)(v >> 8);
|
||||||
p[2] = (ex_u8)(v >> 16);
|
p[2] = (ex_u8)(v >> 16);
|
||||||
p[3] = (ex_u8)(v >> 24);
|
p[3] = (ex_u8)(v >> 24);
|
||||||
#else
|
#else
|
||||||
((ex_u32*)p)[0] = v;
|
((ex_u32*)p)[0] = v;
|
||||||
#endif
|
#endif
|
||||||
m_offset += 4;
|
m_offset += 4;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_u32_be(ex_u32 v)
|
void MemStream::put_u32_be(ex_u32 v)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + 4);
|
m_mbuf.reserve(m_mbuf.size() + 4);
|
||||||
|
|
||||||
ex_u8* p = m_mbuf.data() + m_offset;
|
ex_u8* p = m_mbuf.data() + m_offset;
|
||||||
#if defined(B_ENDIAN)
|
#if defined(B_ENDIAN)
|
||||||
((ex_u32*)p)[0] = v;
|
((ex_u32*)p)[0] = v;
|
||||||
#else
|
#else
|
||||||
ex_u8* _v = (ex_u8*)&v;
|
ex_u8* _v = (ex_u8*)&v;
|
||||||
p[0] = _v[3];
|
p[0] = _v[3];
|
||||||
p[1] = _v[2];
|
p[1] = _v[2];
|
||||||
p[2] = _v[1];
|
p[2] = _v[1];
|
||||||
p[3] = _v[0];
|
p[3] = _v[0];
|
||||||
#endif
|
#endif
|
||||||
m_offset += 4;
|
m_offset += 4;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemStream::put_bin(const ex_u8* p, size_t n)
|
void MemStream::put_bin(const ex_u8* p, size_t n)
|
||||||
{
|
{
|
||||||
m_mbuf.reserve(m_mbuf.size() + n);
|
m_mbuf.reserve(m_mbuf.size() + n);
|
||||||
memcpy(m_mbuf.data() + m_offset, p, n);
|
memcpy(m_mbuf.data() + m_offset, p, n);
|
||||||
m_offset += n;
|
m_offset += n;
|
||||||
if (m_mbuf.size() < m_offset)
|
if (m_mbuf.size() < m_offset)
|
||||||
m_mbuf.size(m_offset);
|
m_mbuf.size(m_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,45 +1,45 @@
|
||||||
#ifndef __TS_MEMSTREAM_H__
|
#ifndef __TS_MEMSTREAM_H__
|
||||||
#define __TS_MEMSTREAM_H__
|
#define __TS_MEMSTREAM_H__
|
||||||
|
|
||||||
#include "ts_membuf.h"
|
#include "ts_membuf.h"
|
||||||
|
|
||||||
class MemStream
|
class MemStream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MemStream(MemBuffer& mbuf);
|
MemStream(MemBuffer& mbuf);
|
||||||
~MemStream();
|
~MemStream();
|
||||||
|
|
||||||
void reset(void); // 清空缓冲区数据(但不释放内存),指针移动到头部
|
void reset(void); // 清空缓冲区数据(但不释放内存),指针移动到头部
|
||||||
|
|
||||||
bool seek(size_t offset); // 移动指针到指定偏移,如果越界,则返回错误
|
bool seek(size_t offset); // 移动指针到指定偏移,如果越界,则返回错误
|
||||||
bool rewind(size_t n = 0); // 回退n字节,如果越界,返回错误,如果n为0,则回退到最开始处
|
bool rewind(size_t n = 0); // 回退n字节,如果越界,返回错误,如果n为0,则回退到最开始处
|
||||||
bool skip(size_t n); // 跳过n字节,如果越界,则返回错误
|
bool skip(size_t n); // 跳过n字节,如果越界,则返回错误
|
||||||
|
|
||||||
ex_u8* ptr(void) { return m_mbuf.data() + m_offset; } // 返回当前数据指针
|
ex_u8* ptr(void) { return m_mbuf.data() + m_offset; } // 返回当前数据指针
|
||||||
size_t offset(void) { return m_offset; } // 返回当前指针相对数据起始的偏移
|
size_t offset(void) { return m_offset; } // 返回当前指针相对数据起始的偏移
|
||||||
|
|
||||||
size_t left(void) { return m_mbuf.size() - m_offset; } // 返回剩余数据的大小(从当前数据指针到缓冲区结尾)
|
size_t left(void) { return m_mbuf.size() - m_offset; } // 返回剩余数据的大小(从当前数据指针到缓冲区结尾)
|
||||||
|
|
||||||
ex_u8 get_u8(void);
|
ex_u8 get_u8(void);
|
||||||
ex_u16 get_u16_le(void);
|
ex_u16 get_u16_le(void);
|
||||||
ex_u16 get_u16_be(void);
|
ex_u16 get_u16_be(void);
|
||||||
ex_u32 get_u32_le(void);
|
ex_u32 get_u32_le(void);
|
||||||
ex_u32 get_u32_be(void);
|
ex_u32 get_u32_be(void);
|
||||||
ex_u8* get_bin(size_t n); // 返回当前指向的数据的指针,内部偏移会向后移动n字节
|
ex_u8* get_bin(size_t n); // 返回当前指向的数据的指针,内部偏移会向后移动n字节
|
||||||
|
|
||||||
void put_zero(size_t n); // 填充n字节的0
|
void put_zero(size_t n); // 填充n字节的0
|
||||||
void put_u8(ex_u8 v);
|
void put_u8(ex_u8 v);
|
||||||
void put_u16_le(ex_u16 v);
|
void put_u16_le(ex_u16 v);
|
||||||
void put_u16_be(ex_u16 v);
|
void put_u16_be(ex_u16 v);
|
||||||
void put_u32_le(ex_u32 v);
|
void put_u32_le(ex_u32 v);
|
||||||
void put_u32_be(ex_u32 v);
|
void put_u32_be(ex_u32 v);
|
||||||
void put_bin(const ex_u8* p, size_t n); // 填充p指向的n字节数据
|
void put_bin(const ex_u8* p, size_t n); // 填充p指向的n字节数据
|
||||||
|
|
||||||
size_t size(void) { return m_mbuf.size(); }
|
size_t size(void) { return m_mbuf.size(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MemBuffer& m_mbuf;
|
MemBuffer& m_mbuf;
|
||||||
size_t m_offset;
|
size_t m_offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __TS_MEMSTREAM_H__
|
#endif // __TS_MEMSTREAM_H__
|
||||||
|
|
Loading…
Reference in New Issue