From be55c4218d380230593a3c862d25726a7b4482df Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 25 Jul 2021 02:32:17 +0800 Subject: [PATCH] no message --- general/CMakeLists.txt | 1 - general/inc/loger.h | 2 +- general/src/algorithm/sorter.hpp | 1 + general/src/function/daemon.c | 85 +++++++++++ general/src/function/daemon.h | 24 +++ ...ckageReceiver.cpp => package_receiver.cpp} | 2 +- .../{PackageReceiver.h => package_receiver.h} | 0 .../src/net/{TcpClient.cpp => tcp_client.cpp} | 2 +- general/src/net/{TcpClient.h => tcp_client.h} | 56 ++++--- general/src/utils.cpp | 26 ++-- test/src/cpp11/CMakeLists.txt | 4 +- test/src/cpp11/cpp11_test.cpp | 140 +++++++++++++++--- test/src/cpp11/thread_usage.cpp | 2 + 13 files changed, 286 insertions(+), 59 deletions(-) create mode 100644 general/src/function/daemon.c create mode 100644 general/src/function/daemon.h rename general/src/net/{PackageReceiver.cpp => package_receiver.cpp} (99%) rename general/src/net/{PackageReceiver.h => package_receiver.h} (100%) rename general/src/net/{TcpClient.cpp => tcp_client.cpp} (99%) rename general/src/net/{TcpClient.h => tcp_client.h} (53%) diff --git a/general/CMakeLists.txt b/general/CMakeLists.txt index db56e2b..ec16a37 100644 --- a/general/CMakeLists.txt +++ b/general/CMakeLists.txt @@ -17,7 +17,6 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") # using Intel C++ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - add_compile_options("/std:c++17") # using Visual Studio C++ endif() diff --git a/general/inc/loger.h b/general/inc/loger.h index 2ae2523..0915ca3 100644 --- a/general/inc/loger.h +++ b/general/inc/loger.h @@ -31,7 +31,7 @@ namespace Loger{ public: _C_Loger(FILE *p); _C_Loger(string path); - + #define DEBUG_FILE_POSITION __FILE__,__LINE__ int Debug(string,string,int); int Log(); int LogFile(); diff --git a/general/src/algorithm/sorter.hpp b/general/src/algorithm/sorter.hpp index 5bd817e..3c57224 100644 --- a/general/src/algorithm/sorter.hpp +++ b/general/src/algorithm/sorter.hpp @@ -75,6 +75,7 @@ int selectsort(T *data,uint64_t len,CompareHanlder handle){ data[i] = tmp; } } + template void swap(T *a1,T *a2){ T tmp = *a1; diff --git a/general/src/function/daemon.c b/general/src/function/daemon.c new file mode 100644 index 0000000..ee5b791 --- /dev/null +++ b/general/src/function/daemon.c @@ -0,0 +1,85 @@ +/* + * @Author: your name + * @Date: 2021-07-23 23:39:57 + * @LastEditTime: 2021-07-25 02:31:24 + * @LastEditors: your name + * @Description: In User Settings Edit + * @FilePath: \generallib\general\src\function\daemon.c + */ +#include "daemon.h" + +BOOL SetProcessPrivilege(char *lpName, BOOL opt) +{ + HANDLE tokenhandle; + TOKEN_PRIVILEGES NewState; + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle)) + { + LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid); + NewState.PrivilegeCount = 1; + NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0; + AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL); + CloseHandle(tokenhandle); + return 1; + } + else + { + return 0; + } +} + +int RangeProcess() +{ + DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; + unsigned int i; + HMODULE hMod[1024]; + HANDLE hProcess; + char szModName[MAX_PATH]; + if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes)) + { + Proc_count = Retn_bytes / sizeof(DWORD); + SetProcessPrivilege("SeDebugPrivilege", 1); + for (i = 0; i < Proc_count; i++) + { + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]); + if (hProcess != NULL) + { + EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2); + GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName)); + printf("PID=%d Path=%s\n", Proc_pid[i], szModName); + } + CloseHandle(hProcess); + } + SetProcessPrivilege("SeDebugPrivilege", 0); + } + return 0; +} + +int test_fork() +{ + char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe"; + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 + si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口 + BOOL bRet = CreateProcess( + NULL, //不在此指定可执行文件的文件名 + szCommandLine, //命令行参数 + NULL, //默认进程安全性 + NULL, //默认进程安全性 + FALSE, //指定当前进程内句柄不可以被子进程继承 + CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 + NULL, //使用本进程的环境变量 + NULL, //使用本进程的驱动器和目录 + &si, + &pi); + if (bRet) + { + //不使用的句柄最好关掉 + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + printf("new process id %d\n", pi.dwProcessId); + printf("new thread id %d\n", pi.dwThreadId); + } + return 0; +} diff --git a/general/src/function/daemon.h b/general/src/function/daemon.h new file mode 100644 index 0000000..9119472 --- /dev/null +++ b/general/src/function/daemon.h @@ -0,0 +1,24 @@ +/* + * @Author: your name + * @Date: 2021-07-23 23:40:36 + * @LastEditTime: 2021-07-25 02:31:09 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edi + * @FilePath: \generallib\general\src\function\daemon.h + */ + +#if __cplusplus >= 201103L +#pragma message("编译器支持c++11") +#endif +#include +#include +#include +using namespace std; +extern "C"{ +#include +#include +} +#include +#include +#include + diff --git a/general/src/net/PackageReceiver.cpp b/general/src/net/package_receiver.cpp similarity index 99% rename from general/src/net/PackageReceiver.cpp rename to general/src/net/package_receiver.cpp index ec6bd72..d837672 100644 --- a/general/src/net/PackageReceiver.cpp +++ b/general/src/net/package_receiver.cpp @@ -1,4 +1,4 @@ -#include "PackageReceiver.h" +#include "package_receiver.h" PackageReceiver::PackageReceiver() { diff --git a/general/src/net/PackageReceiver.h b/general/src/net/package_receiver.h similarity index 100% rename from general/src/net/PackageReceiver.h rename to general/src/net/package_receiver.h diff --git a/general/src/net/TcpClient.cpp b/general/src/net/tcp_client.cpp similarity index 99% rename from general/src/net/TcpClient.cpp rename to general/src/net/tcp_client.cpp index 44da933..37f8c92 100644 --- a/general/src/net/TcpClient.cpp +++ b/general/src/net/tcp_client.cpp @@ -2,7 +2,7 @@ // Created by 29019 on 2020/4/18. // -#include "TcpClient.h" +#include "tcp_client.h" void conn_writecb(struct bufferevent *, void *); void conn_readcb(struct bufferevent *, void *); diff --git a/general/src/net/TcpClient.h b/general/src/net/tcp_client.h similarity index 53% rename from general/src/net/TcpClient.h rename to general/src/net/tcp_client.h index 40bf006..f432d8c 100644 --- a/general/src/net/TcpClient.h +++ b/general/src/net/tcp_client.h @@ -1,3 +1,11 @@ +/* + * @Author: your name + * @Date: 2021-06-12 14:42:28 + * @LastEditTime: 2021-07-24 00:47:43 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \generallib\general\src\net\tcp_client.h + */ // // Created by 29019 on 2020/4/18. // @@ -9,13 +17,14 @@ #define _WIN32_WINNT 0x0500 #endif #ifdef linux -#include -#include -#include +#include +#include +#include #define EVENT__HAVE_PTHREADS #endif -extern "C"{ +extern "C" +{ #include "third/include/event2/bufferevent.h" #include "third/include/event2/buffer.h" #include "third/include/event2/listener.h" @@ -25,29 +34,33 @@ extern "C"{ }; #include -#include "PackageReceiver.h" +#include "package_receiver.h" #include #include -using namespace std; +using namespace std; -class TcpClientLibevent { +class TcpClientLibevent +{ public: - typedef enum { - UNCONNECTED, // 未连接 - CONNECTED, //已经连接 - FAIL, // 连接失败 - }Status; - class TcpClientObserver{ + typedef enum + { + UNCONNECTED, // 未连接 + CONNECTED, //已经连接 + FAIL, // 连接失败 + } Status; + class TcpClientObserver + { public: - virtual ~TcpClientObserver(){return;} + virtual ~TcpClientObserver() { return; } mutex mMux; virtual void OnConnected() { return; }; virtual void OnDisConnected() { return; }; - virtual void OnData(uint8_t *dat,uint64_t len){return;}; - virtual void OnClose(){return;}; + virtual void OnData(uint8_t *dat, uint64_t len) { return; }; + virtual void OnClose() { return; }; }; - TcpClientLibevent(std::string addrinfo,int port, TcpClientObserver *p); - ~TcpClientLibevent(){ + TcpClientLibevent(std::string addrinfo, int port, TcpClientObserver *p); + ~TcpClientLibevent() + { event_base_free(mBase); }; int ConnectServer(); @@ -55,15 +68,16 @@ public: int Dispatch(); int OnTCPPackage(uint8_t *, uint16_t); int SetReconnect(bool); - int SetObserver(TcpClientObserver*); + int SetObserver(TcpClientObserver *); int Close(); Status mStatus; TcpClientObserver *mObserver; + private: bool mReConnect = false; - int sendData(void*,size_t); + int sendData(void *, size_t); struct event_base *mBase; - struct bufferevent* bev; + struct bufferevent *bev; struct sockaddr_in mSrv; std::thread *mThread; mutex mLock; diff --git a/general/src/utils.cpp b/general/src/utils.cpp index e1330d7..5aac482 100644 --- a/general/src/utils.cpp +++ b/general/src/utils.cpp @@ -1,3 +1,11 @@ +/* + * @Author: your name + * @Date: 2020-10-11 17:37:54 + * @LastEditTime: 2021-07-24 00:46:47 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \generallib\general\src\utils.cpp + */ // // Created by 29019 on 2019/5/2. // @@ -39,17 +47,17 @@ inline ENV_SYS CurrentEnvSys() { return ENV_LINUX; #endif #ifdef _WINDOWS - return ENV_WINDOWS - #endif - #ifdef _UNIX - return ENV_UNIX - #endif - #ifdef _WIN32 return ENV_WINDOWS; - #endif - #if !defined(linux) && defined(_WINDOWS)&&defined(_UNIX)&&defined(_WIN32) +#endif +#ifdef _UNIX + return ENV_UNIX +#endif +#ifdef _WIN32 + return ENV_WINDOWS; +#endif +#if !defined(linux) && defined(_WINDOWS) && defined(_UNIX) && defined(_WIN32) return ENV_NONE; - #endif +#endif } inline ENV_COMPILER CurrentEnvCompiler() diff --git a/test/src/cpp11/CMakeLists.txt b/test/src/cpp11/CMakeLists.txt index 07592e7..e968ebe 100644 --- a/test/src/cpp11/CMakeLists.txt +++ b/test/src/cpp11/CMakeLists.txt @@ -4,15 +4,13 @@ add_definitions(-std=c++11) message("current dir" ${CMAKE_CURRENT_SOURCE_DIR}) # set(CMAKE_CXX_FLAGS "-fno-elide-constructors") -aux_source_directory(. SOURCE) message(info ${SOURCE}) link_directories("./third/jsoncpp/lib/") link_directories("../../../obj/") -link_libraries(jsoncpp) link_libraries(generallib) -add_executable(cpp11 ${SOURCE} ) +add_executable(cpp11 cpp11_test.cpp ) include_directories("./third/jsoncpp/include/pkgsrc/include/json") include_directories("../../../obj/inc/") diff --git a/test/src/cpp11/cpp11_test.cpp b/test/src/cpp11/cpp11_test.cpp index f17bf89..7e5d39a 100644 --- a/test/src/cpp11/cpp11_test.cpp +++ b/test/src/cpp11/cpp11_test.cpp @@ -1,3 +1,11 @@ +/* + * @Author: your name + * @Date: 2021-03-15 23:07:25 + * @LastEditTime: 2021-07-24 23:19:57 + * @LastEditors: Please set LastEditors + * @Description: In User Settings Edit + * @FilePath: \cpp11\cpp11_test.cpp + */ #if __cplusplus >= 201103L #pragma message("编译器支持c++11") #endif @@ -13,9 +21,89 @@ extern "C"{ #include #include } +#include +#include #include "loger.h" +#include -int main(){ +BOOL SetProcessPrivilege(char *lpName, BOOL opt) +{ + HANDLE tokenhandle; + TOKEN_PRIVILEGES NewState; + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenhandle)) + { + LookupPrivilegeValue(NULL, lpName, &NewState.Privileges[0].Luid); + NewState.PrivilegeCount = 1; + NewState.Privileges[0].Attributes = opt != 0 ? 2 : 0; + AdjustTokenPrivileges(tokenhandle, FALSE, &NewState, sizeof(NewState), NULL, NULL); + CloseHandle(tokenhandle); + return 1; + } + else + { + return 0; + } +} + +int RangeProcess() +{ + DWORD Proc_pid[1024], Retn_bytes, Proc_count, Retn_bytes2; + unsigned int i; + HMODULE hMod[1024]; + HANDLE hProcess; + char szModName[MAX_PATH]; + if (EnumProcesses(Proc_pid, sizeof(Proc_pid), &Retn_bytes)) + { + Proc_count = Retn_bytes / sizeof(DWORD); + SetProcessPrivilege("SeDebugPrivilege", 1); + for (i = 0; i < Proc_count; i++) + { + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Proc_pid[i]); + if (hProcess != NULL) + { + EnumProcessModules(hProcess, hMod, sizeof(hMod), &Retn_bytes2); + GetModuleFileNameEx(hProcess, hMod[0], szModName, sizeof(szModName)); + printf("PID=%d Path=%s\n", Proc_pid[i], szModName); + } + CloseHandle(hProcess); + } + SetProcessPrivilege("SeDebugPrivilege", 0); + } + return 0; +} + +int test_fork() +{ + char szCommandLine[] = "D:\\game\\The Legend of Zelda Breath of the Wild\\cemu\\cemu.exe"; + STARTUPINFO si = {sizeof(si)}; + PROCESS_INFORMATION pi; + si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 + si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗口 + BOOL bRet = CreateProcess( + NULL, //不在此指定可执行文件的文件名 + szCommandLine, //命令行参数 + NULL, //默认进程安全性 + NULL, //默认进程安全性 + FALSE, //指定当前进程内句柄不可以被子进程继承 + CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 + NULL, //使用本进程的环境变量 + NULL, //使用本进程的驱动器和目录 + &si, + &pi); + if (bRet) + { + //不使用的句柄最好关掉 + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + printf("new process id %d\n", pi.dwProcessId); + printf("new thread id %d\n", pi.dwThreadId); + } + return 0; +} + +int main(int argc, char **argv) +{ // std::cout<<"test start"< i; - i.insert(i.begin(),2); - i.insert(i.begin(),3); - i.insert(i.begin(),4); - i.insert(i.begin(),5); - i.insert(i.begin(),6); - - list::iterator pos = (find(i.begin(), i.end(), 4)); - i.insert(pos,1); - - auto begin = i.begin(); - while(*begin != 4) - begin++; - - i.erase(begin); - - for(auto x : i){ - std::cout< + // i; + // i.insert(i.begin(),2); + // i.insert(i.begin(),3); + // i.insert(i.begin(),4); + // i.insert(i.begin(),5); + // i.insert(i.begin(),6); + + // list::iterator pos = (find(i.begin(), i.end(), 4)); + // i.insert(pos,1); + + // auto begin = i.begin(); + // while(*begin != 4) + // begin++; + + // i.erase(begin); + + // for(auto x : i){ + // std::cout<(t2-t1).count(); std::cout<<"count is "<