diff --git a/tests/test_creator.cpp b/tests/test_creator.cpp index 7657c1cef..e8ec2b793 100644 --- a/tests/test_creator.cpp +++ b/tests/test_creator.cpp @@ -13,7 +13,8 @@ using namespace std; using namespace toolkit; -//测试onCreate和onDestory同时存在 +//测试onCreate和onDestory同时存在 [AUTO-TRANSLATED:152351be] +// Test when both onCreate and onDestroy exist class TestA { public: TestA() { @@ -33,7 +34,8 @@ class TestA { } }; -//测试只存在onCreate +//测试只存在onCreate [AUTO-TRANSLATED:721019f3] +// Test when only onCreate exists class TestB { public: TestB() { @@ -49,7 +51,8 @@ class TestB { } }; -//测试只存在onDestory +//测试只存在onDestory [AUTO-TRANSLATED:65090f10] +// Test when only onDestroy exists class TestC { public: TestC() { @@ -65,7 +68,8 @@ class TestC { } }; -//测试onCreate和onDestory返回值不为void时 +//测试onCreate和onDestory返回值不为void时 [AUTO-TRANSLATED:cafa864e] +// Test when onCreate and onDestroy return values are not void class TestD { public: TestD() { @@ -87,7 +91,8 @@ class TestD { } }; -//测试onCreate和onDestory都不存在时 +//测试onCreate和onDestory都不存在时 [AUTO-TRANSLATED:475fb8f1] +// Test when neither onCreate nor onDestroy exist class TestE { public: TestE() { @@ -99,7 +104,8 @@ class TestE { } }; -//测试自定义构造函数 +//测试自定义构造函数 [AUTO-TRANSLATED:13ffcdcf] +// Test custom constructor class TestF { public: TestF(int a, const char *b) { @@ -111,7 +117,8 @@ class TestF { } }; -//测试自定义onCreate函数 +//测试自定义onCreate函数 [AUTO-TRANSLATED:35c11999] +// Test custom onCreate function class TestH { public: TestH() { @@ -128,7 +135,8 @@ class TestH { } }; -//测试onDestory函数抛异常 +//测试onDestory函数抛异常 [AUTO-TRANSLATED:6d70c971] +// Test onDestroy function throws an exception class TestI { public: TestI() { @@ -145,7 +153,8 @@ class TestI { } }; -//测试自定义onDestory,onDestory将被忽略调用 +//测试自定义onDestory,onDestory将被忽略调用 [AUTO-TRANSLATED:5ab2ba7d] +// Test custom onDestroy, onDestroy will be ignored when called class TestJ { public: TestJ() { @@ -162,7 +171,8 @@ class TestJ { }; int main() { - //初始化日志系统 + //初始化日志系统 [AUTO-TRANSLATED:25c549de] + // Initialize the logging system Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); diff --git a/tests/test_delayTask.cpp b/tests/test_delayTask.cpp index 46ea42b98..c4ca1cad3 100644 --- a/tests/test_delayTask.cpp +++ b/tests/test_delayTask.cpp @@ -20,7 +20,8 @@ using namespace std; using namespace toolkit; int main() { - //设置日志 + //设置日志 [AUTO-TRANSLATED:50372045] + // Set log Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); @@ -64,7 +65,8 @@ int main() { tag1->cancel(); WarnL << "取消task 0、1"; - //退出程序事件处理 + //退出程序事件处理 [AUTO-TRANSLATED:80065cb7] + // Exit program event handling static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_eventPoller.cpp b/tests/test_eventPoller.cpp index 2b26fe7cf..a5959d87e 100644 --- a/tests/test_eventPoller.cpp +++ b/tests/test_eventPoller.cpp @@ -21,11 +21,16 @@ using namespace toolkit; /** * cpu负载均衡测试 * @return + * CPU load balancing test + * @return + + * [AUTO-TRANSLATED:620fe7ab] */ int main() { static bool exit_flag = false; signal(SIGINT, [](int) { exit_flag = true; }); - //设置日志 + //设置日志 [AUTO-TRANSLATED:50372045] + // Set log Logger::Instance().add(std::make_shared()); Ticker ticker; diff --git a/tests/test_ini.cpp b/tests/test_ini.cpp index 69559279d..158b07d00 100644 --- a/tests/test_ini.cpp +++ b/tests/test_ini.cpp @@ -15,7 +15,8 @@ using namespace std; using namespace toolkit; int main() { - //初始化日志系统 + //初始化日志系统 [AUTO-TRANSLATED:25c549de] + // Initialize the logging system Logger::Instance().add(std::make_shared ()); Logger::Instance().setWriter(std::make_shared()); mINI ini; diff --git a/tests/test_logger.cpp b/tests/test_logger.cpp index 7a09b53da..7ad6a3402 100644 --- a/tests/test_logger.cpp +++ b/tests/test_logger.cpp @@ -23,7 +23,8 @@ class TestLog }; ~TestLog(){}; - //通过此友元方法,可以打印自定义数据类型 + //通过此友元方法,可以打印自定义数据类型 [AUTO-TRANSLATED:f9d17d41] + // Through this friend method, you can print custom data types friend ostream& operator<<(ostream& out,const TestLog& obj){ return out << obj._ss.str(); } @@ -32,13 +33,15 @@ class TestLog }; int main() { - //初始化日志系统 + //初始化日志系统 [AUTO-TRANSLATED:25c549de] + // Initialize the logging system Logger::Instance().add(std::make_shared ()); Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); InfoL << "测试std::cout风格打印:"; - //ostream支持的数据类型都支持,可以通过友元的方式打印自定义类型数据 + //ostream支持的数据类型都支持,可以通过友元的方式打印自定义类型数据 [AUTO-TRANSLATED:c857af94] + // All data types supported by ostream are supported, and custom type data can be printed through friend methods TraceL << "object int"<< TestLog((int)1) << endl; DebugL << "object short:"<()); - //对事件NOTICE_NAME1新增一个监听 - //addListener方法第一个参数是标签,用来删除监听时使用 - //需要注意的是监听回调的参数列表个数类型需要与emitEvent广播时的完全一致,否则会有无法预知的错误 + //对事件NOTICE_NAME1新增一个监听 [AUTO-TRANSLATED:c8e83e55] + // Add a Listener to the Event NOTICE_NAME1 + //addListener方法第一个参数是标签,用来删除监听时使用 [AUTO-TRANSLATED:918a506c] + // The First Parameter of the addListener Method is a Tag, Used to Delete the Listener + //需要注意的是监听回调的参数列表个数类型需要与emitEvent广播时的完全一致,否则会有无法预知的错误 [AUTO-TRANSLATED:b36668f5] + // Note that the Number and Type of Parameters in the Listener Callback Must be Exactly the Same as Those in the emitEvent Broadcast, Otherwise Unpredictable Errors May Occur NoticeCenter::Instance().addListener(0,NOTICE_NAME1, [](int &a,const char * &b,double &c,string &d){ DebugL << a << " " << b << " " << c << " " << d; @@ -44,7 +52,8 @@ int main() { }); }); - //监听NOTICE_NAME2事件 + //监听NOTICE_NAME2事件 [AUTO-TRANSLATED:36bfaf8a] + // Listen for the NOTICE_NAME2 Event NoticeCenter::Instance().addListener(0,NOTICE_NAME2, [](string &d,double &c,const char *&b,int &a){ DebugL << a << " " << b << " " << c << " " << d; @@ -61,7 +70,8 @@ int main() { const char *b = "b"; double c = 3.14; string d("d"); - //每隔1秒广播一次事件,如果无法确定参数类型,可加强制转换 + //每隔1秒广播一次事件,如果无法确定参数类型,可加强制转换 [AUTO-TRANSLATED:dc815907] + // Broadcast the Event Every 1 Second, If the Parameter Type is Uncertain, a Forced Conversion Can be Added NoticeCenter::Instance().emitEvent(NOTICE_NAME1,++a,(const char *)"b",c,d); NoticeCenter::Instance().emitEvent(NOTICE_NAME2,d,c,b,a); sleep(1); // sleep 1 second diff --git a/tests/test_pingpong.cpp b/tests/test_pingpong.cpp index df24d71c0..1969b640e 100644 --- a/tests/test_pingpong.cpp +++ b/tests/test_pingpong.cpp @@ -21,6 +21,9 @@ using namespace toolkit; /** * 回显会话 + * Echo Session + + * [AUTO-TRANSLATED:bc2a4e9e] */ class EchoSession : public Session { public: @@ -41,29 +44,37 @@ class EchoSession : public Session { void onManager() override {} }; -//命令(http) +//命令(http) [AUTO-TRANSLATED:d96c7331] +// Command (http) class CMD_pingpong: public CMD { public: CMD_pingpong(){ _parser.reset(new OptionParser(nullptr)); (*_parser) << Option('l', "listen", Option::ArgRequired, "10000", false, "服务器模式:监听端口", nullptr); - //测试客户端个数,默认10个 + //测试客户端个数,默认10个 [AUTO-TRANSLATED:5a5a229a] + // Test client count, default 10 (*_parser) << Option('c', "count", Option::ArgRequired, to_string(10).data(), false, "客户端模式:测试客户端个数", nullptr); - //默认每次发送1MB的数据 + //默认每次发送1MB的数据 [AUTO-TRANSLATED:22373e35] + // Default send 1MB data each time (*_parser) << Option('b', "block", Option::ArgRequired, to_string(1024 * 1024).data(), false, "客户端模式:测试数据块大小", nullptr); - //默认1秒发送10次,总速度率为1MB/s * 10 * 10 = 100MB/s + //默认1秒发送10次,总速度率为1MB/s * 10 * 10 = 100MB/s [AUTO-TRANSLATED:d3b7bb36] + // Default send 10 times per second, total speed rate is 1MB/s * 10 * 10 = 100MB/s (*_parser) << Option('i', "interval", Option::ArgRequired, to_string(100).data(), false, "客户端模式:测试数据发送间隔,单位毫秒", nullptr); - //客户端启动间隔时间 + //客户端启动间隔时间 [AUTO-TRANSLATED:b401adf1] + // Client startup interval time (*_parser) << Option('d', "delay", Option::ArgRequired, "50", false, "服务器模式:客户端启动间隔时间", nullptr); - //指定服务器地址 + //指定服务器地址 [AUTO-TRANSLATED:867c9c2d] + // Specify server address (*_parser) << Option('s', "server", Option::ArgRequired, "127.0.0.1:10000", false, "客户端模式:测试服务器地址", [] (const std::shared_ptr &stream, const string &arg) { if (arg.find(":") == string::npos) { - //中断后续选项的解析以及解析完毕回调等操作 + //中断后续选项的解析以及解析完毕回调等操作 [AUTO-TRANSLATED:15b7592f] + // Interrupt subsequent option parsing and parsing completion callback, etc. throw std::runtime_error("\t地址必须指明端口号."); } - //如果返回false则忽略后续选项的解析 + //如果返回false则忽略后续选项的解析 [AUTO-TRANSLATED:01a3d6bc] + // If return false, ignore subsequent option parsing return true; }); } @@ -100,7 +111,8 @@ int main(int argc,char *argv[]){ return 0; } - //初始化环境 + //初始化环境 [AUTO-TRANSLATED:efbad911] + // Initialize environment Logger::Instance().add(std::shared_ptr(new ConsoleChannel())); Logger::Instance().setWriter(std::shared_ptr(new AsyncLogWriter())); @@ -146,7 +158,8 @@ int main(int argc,char *argv[]){ usleep(delay * 1000); } - //设置退出信号处理函数 + //设置退出信号处理函数 [AUTO-TRANSLATED:4f047770] + // Set exit signal handling function static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_pipe.cpp b/tests/test_pipe.cpp index d6e642664..bff41874f 100644 --- a/tests/test_pipe.cpp +++ b/tests/test_pipe.cpp @@ -18,29 +18,36 @@ using namespace std; using namespace toolkit; int main() { - //设置日志 + //设置日志 [AUTO-TRANSLATED:50372045] + // Set up logging Logger::Instance().add(std::make_shared()); #if defined(_WIN32) ErrorL << "该测试程序不能再windows下运行,因为我不会windows下的多进程编程,但是管道模块是可以在windows下正常工作的。" << endl; #else - //获取父进程的PID + //获取父进程的PID [AUTO-TRANSLATED:0a35f39a] + // Get the parent process's PID auto parentPid = getpid(); InfoL << "parent pid:" << parentPid << endl; - //定义一个管道,lambada类型的参数是管道收到数据的回调 + //定义一个管道,lambada类型的参数是管道收到数据的回调 [AUTO-TRANSLATED:d5eeb28a] + // Define a pipe, with a lambda type parameter as the callback for when data is received Pipe pipe([](int size,const char *buf) { - //该管道有数据可读了 + //该管道有数据可读了 [AUTO-TRANSLATED:e542256f] + // The pipe has data available for reading InfoL << getpid() << " recv:" << buf; }); - //创建子进程 + //创建子进程 [AUTO-TRANSLATED:8d5746fc] + // Create a child process auto pid = fork(); if (pid == 0) { - //子进程 + //子进程 [AUTO-TRANSLATED:3f793797] + // Child process int i = 10; while (i--) { - //在子进程每隔一秒把数据写入管道,共计发送10次 + //在子进程每隔一秒把数据写入管道,共计发送10次 [AUTO-TRANSLATED:5a340f4b] + // In the child process, write data to the pipe every second, for a total of 10 times sleep(1); string msg = StrPrinter << "message " << i << " form subprocess:" << getpid(); DebugL << "子进程发送:" << msg << endl; @@ -48,7 +55,8 @@ int main() { } DebugL << "子进程退出" << endl; } else { - //父进程设置退出信号处理函数 + //父进程设置退出信号处理函数 [AUTO-TRANSLATED:b2a0b432] + // Parent process sets up exit signal handling function static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_resourcePool.cpp b/tests/test_resourcePool.cpp index f1b88473e..18aa9376c 100644 --- a/tests/test_resourcePool.cpp +++ b/tests/test_resourcePool.cpp @@ -20,7 +20,8 @@ using namespace std; using namespace toolkit; -//程序退出标志 +//程序退出标志 [AUTO-TRANSLATED:9ac79dfb] +// Program exit flag bool g_bExitFlag = false; @@ -36,23 +37,29 @@ class string_imp : public string{ }; -//后台线程任务 +//后台线程任务 [AUTO-TRANSLATED:1411f16e] +// Background thread task void onRun(ResourcePool &pool,int threadNum){ std::random_device rd; while(!g_bExitFlag){ - //从循环池获取一个可用的对象 + //从循环池获取一个可用的对象 [AUTO-TRANSLATED:10783ad7] + // Get an available object from the loop pool auto obj_ptr = pool.obtain(); if(obj_ptr->empty()){ - //这个对象是全新未使用的 + //这个对象是全新未使用的 [AUTO-TRANSLATED:9aaebe21] + // This object is brand new and unused InfoL << "后台线程 " << threadNum << ":" << "obtain a emptry object!"; }else{ - //这个对象是循环使用的 + //这个对象是循环使用的 [AUTO-TRANSLATED:c13c04d6] + // This object is looped for reuse InfoL << "后台线程 " << threadNum << ":" << *obj_ptr; } - //标记该对象被本线程使用 + //标记该对象被本线程使用 [AUTO-TRANSLATED:4730d28a] + // Mark this object as used by the current thread obj_ptr->assign(StrPrinter << "keeped by thread:" << threadNum ); - //随机休眠,打乱循环使用顺序 + //随机休眠,打乱循环使用顺序 [AUTO-TRANSLATED:313f439a] + // Random sleep to disrupt the loop usage order usleep( 1000 * (rd()% 10)); obj_ptr.reset();//手动释放,也可以注释这句代码。根据RAII的原理,该对象会被自动释放并重新进入循环列队 usleep( 1000 * (rd()% 1000)); @@ -60,22 +67,28 @@ void onRun(ResourcePool &pool,int threadNum){ } int main() { - //初始化日志 + //初始化日志 [AUTO-TRANSLATED:371bb4e5] + // Initialize log Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); - //大小为50的循环池 + //大小为50的循环池 [AUTO-TRANSLATED:2ba6e2b3] + // Loop pool of size 50 ResourcePool pool; pool.setSize(50); - //获取一个对象,该对象将被主线程持有,并且不会被后台线程获取并赋值 + //获取一个对象,该对象将被主线程持有,并且不会被后台线程获取并赋值 [AUTO-TRANSLATED:fa8654ed] + // Get an object that will be held by the main thread and will not be obtained and assigned by the background thread auto reservedObj = pool.obtain(); - //在主线程赋值该对象 + //在主线程赋值该对象 [AUTO-TRANSLATED:a04a9b13] + // Assign the object in the main thread reservedObj->assign("This is a reserved object , and will never be used!"); thread_group group; - //创建4个后台线程,该4个线程模拟循环池的使用场景, - //理论上4个线程在同一时间最多同时总共占用4个对象 + //创建4个后台线程,该4个线程模拟循环池的使用场景, [AUTO-TRANSLATED:a574c5f2] + // Create 4 background threads, these 4 threads simulate the usage scenario of a loop pool, + //理论上4个线程在同一时间最多同时总共占用4个对象 [AUTO-TRANSLATED:f2a82316] + // In theory, the 4 threads can occupy at most 4 objects at the same time WarnL << "主线程打印:" << "开始测试,主线程已经获取到的对象应该不会被后台线程获取到:" << *reservedObj; @@ -86,25 +99,32 @@ int main() { }); } - //等待3秒钟,此时循环池里面可用的对象基本上最少都被使用过一遍了 + //等待3秒钟,此时循环池里面可用的对象基本上最少都被使用过一遍了 [AUTO-TRANSLATED:e5893625] + // Wait for 3 seconds, at this time, the objects available in the loop pool have been used at least once sleep(3); - //但是由于reservedObj早已被主线程持有,后台线程是获取不到该对象的 - //所以其值应该尚未被覆盖 + //但是由于reservedObj早已被主线程持有,后台线程是获取不到该对象的 [AUTO-TRANSLATED:2c0e93ce] + // However, since reservedObj has been held by the main thread, the background threads cannot obtain the object + //所以其值应该尚未被覆盖 [AUTO-TRANSLATED:4cc91f7b] + // So its value should not have been overwritten WarnL << "主线程打印: 该对象还在被主线程持有,其值应该保持不变:" << *reservedObj; - //获取该对象的引用 + //获取该对象的引用 [AUTO-TRANSLATED:ca198d5f] + // Get a reference to the object auto &objref = *reservedObj; - //显式释放对象,让对象重新进入循环列队,这时该对象应该会被后台线程持有并赋值 + //显式释放对象,让对象重新进入循环列队,这时该对象应该会被后台线程持有并赋值 [AUTO-TRANSLATED:8d97a1af] + // Explicitly release the object, allowing it to re-enter the loop queue, at this time the object should be held and assigned by the background thread reservedObj.reset(); WarnL << "主线程打印: 已经释放该对象,它应该会被后台线程获取到并被覆盖值"; - //再休眠3秒,让reservedObj被后台线程循环使用 + //再休眠3秒,让reservedObj被后台线程循环使用 [AUTO-TRANSLATED:4830df93] + // Sleep for another 3 seconds, allowing reservedObj to be looped and used by the background thread sleep(3); - //这时,reservedObj还在循环池内,引用应该还是有效的,但是值应该被覆盖了 + //这时,reservedObj还在循环池内,引用应该还是有效的,但是值应该被覆盖了 [AUTO-TRANSLATED:1323f66e] + // At this time, reservedObj is still in the loop pool, the reference should still be valid, but the value should have been overwritten WarnL << "主线程打印:对象已被后台线程赋值为:" << objref << endl; { @@ -121,9 +141,11 @@ int main() { } sleep(3); - //通知后台线程退出 + //通知后台线程退出 [AUTO-TRANSLATED:dbeda936] + // Notify background thread to exit g_bExitFlag = true; - //等待后台线程退出 + //等待后台线程退出 [AUTO-TRANSLATED:49b72af6] + // Wait for background thread to exit group.join_all(); return 0; } diff --git a/tests/test_ringBuffer.cpp b/tests/test_ringBuffer.cpp index 399e0900a..353a41be6 100644 --- a/tests/test_ringBuffer.cpp +++ b/tests/test_ringBuffer.cpp @@ -19,50 +19,61 @@ using namespace std; using namespace toolkit; -//环形缓存写线程退出标记 +//环形缓存写线程退出标记 [AUTO-TRANSLATED:ceeb4c96] +// Ring buffer write thread exit flag bool g_bExitWrite = false; -//一个30个string对象的环形缓存 +//一个30个string对象的环形缓存 [AUTO-TRANSLATED:fee6a014] +// A ring buffer of 30 string objects RingBuffer::Ptr g_ringBuf(new RingBuffer(30)); -//写事件回调函数 +//写事件回调函数 [AUTO-TRANSLATED:19f6b6fa] +// Write event callback function void onReadEvent(const string &str){ - //读事件模式性 + //读事件模式性 [AUTO-TRANSLATED:12cdd3a8] + // Read event mode DebugL << str; } -//环形缓存销毁事件 +//环形缓存销毁事件 [AUTO-TRANSLATED:7da356eb] +// Ring buffer destruction event void onDetachEvent(){ WarnL; } -//写环形缓存任务 +//写环形缓存任务 [AUTO-TRANSLATED:1467fea5] +// Write ring buffer task void doWrite(){ int i = 0; while(!g_bExitWrite){ - //每隔100ms写一个数据到环形缓存 + //每隔100ms写一个数据到环形缓存 [AUTO-TRANSLATED:aedec620] + // Write data to the ring buffer every 100ms g_ringBuf->write(to_string(++i),true); usleep(100 * 1000); } } int main() { - //初始化日志 + //初始化日志 [AUTO-TRANSLATED:371bb4e5] + // Initialize log Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); auto poller = EventPollerPool::Instance().getPoller(); RingBuffer::RingReader::Ptr ringReader; poller->sync([&](){ - //从环形缓存获取一个读取器 + //从环形缓存获取一个读取器 [AUTO-TRANSLATED:c61b1c37] + // Get a reader from the ring buffer ringReader = g_ringBuf->attach(poller); - //设置读取事件 + //设置读取事件 [AUTO-TRANSLATED:6d9e7c68] + // Set read event ringReader->setReadCB([](const string &pkt){ onReadEvent(pkt); }); - //设置环形缓存销毁事件 + //设置环形缓存销毁事件 [AUTO-TRANSLATED:8fc24dc3] + // Set ring buffer destruction event ringReader->setDetachCB([](){ onDetachEvent(); }); @@ -70,24 +81,31 @@ int main() { thread_group group; - //写线程 + //写线程 [AUTO-TRANSLATED:f91ceacd] + // Write thread group.create_thread([](){ doWrite(); }); - //测试3秒钟 + //测试3秒钟 [AUTO-TRANSLATED:942e7c08] + // Test for 3 seconds sleep(3); - //通知写线程退出 + //通知写线程退出 [AUTO-TRANSLATED:b2b2a2af] + // Notify write thread to exit g_bExitWrite = true; - //等待写线程退出 + //等待写线程退出 [AUTO-TRANSLATED:8150eaf3] + // Wait for write thread to exit group.join_all(); - //释放环形缓冲,此时异步触发Detach事件 + //释放环形缓冲,此时异步触发Detach事件 [AUTO-TRANSLATED:3aaaa2f4] + // Release ring buffer, triggering Detach event asynchronously g_ringBuf.reset(); - //等待异步触发Detach事件 + //等待异步触发Detach事件 [AUTO-TRANSLATED:a6ea0f45] + // Wait for asynchronous Detach event trigger sleep(1); - //消除对EventPoller对象的引用 + //消除对EventPoller对象的引用 [AUTO-TRANSLATED:4f22453d] + // Remove reference to EventPoller object ringReader.reset(); sleep(1); return 0; diff --git a/tests/test_semaphore.cpp b/tests/test_semaphore.cpp index 140443e21..8c6991b19 100644 --- a/tests/test_semaphore.cpp +++ b/tests/test_semaphore.cpp @@ -24,18 +24,21 @@ semaphore g_sem;//信号量 atomic_llong g_produced(0); atomic_llong g_consumed(0); -//消费者线程 +//消费者线程 [AUTO-TRANSLATED:db1fb231] +// Consumer thread void onConsum() { while (true) { g_sem.wait(); if (++g_consumed > g_produced) { - //如果打印这句log则表明有bug + //如果打印这句log则表明有bug [AUTO-TRANSLATED:190165e0] + // If this log is printed, it indicates a bug ErrorL << g_consumed << " > " << g_produced; } } } -//生产者线程 +//生产者线程 [AUTO-TRANSLATED:3f37e15e] +// Producer thread void onProduce() { while(true){ ++ g_produced; @@ -46,14 +49,16 @@ void onProduce() { } } int main() { - //初始化log + //初始化log [AUTO-TRANSLATED:585df223] + // Initialize log Logger::Instance().add(std::make_shared()); Ticker ticker; thread_group thread_producer; for (size_t i = 0; i < thread::hardware_concurrency(); ++i) { thread_producer.create_thread([]() { - //1个生产者线程 + //1个生产者线程 [AUTO-TRANSLATED:a9a01b88] + // 1 producer thread onProduce(); }); } @@ -61,14 +66,16 @@ int main() { thread_group thread_consumer; for (int i = 0; i < 4; ++i) { thread_consumer.create_thread([i]() { - //4个消费者线程 + //4个消费者线程 [AUTO-TRANSLATED:9dabe877] + // 4 consumer threads onConsum(); }); } - //等待所有生成者线程退出 + //等待所有生成者线程退出 [AUTO-TRANSLATED:321d2677] + // Wait for all producer threads to exit thread_producer.join_all(); DebugL << "生产者线程退出,耗时:" << ticker.elapsedTime() << "ms," << "生产任务数:" << g_produced << ",消费任务数:" << g_consumed; @@ -78,7 +85,8 @@ int main() { sleep(1); } - //程序强制退出可能core dump;在程序推出时,生产的任务数应该跟消费任务数一致 + //程序强制退出可能core dump;在程序推出时,生产的任务数应该跟消费任务数一致 [AUTO-TRANSLATED:59593bf0] + // The program may force exit and core dump; when the program exits, the number of produced tasks should be consistent with the number of consumed tasks WarnL << "强制关闭消费线程,可能触发core dump" ; return 0; } diff --git a/tests/test_shell.cpp b/tests/test_shell.cpp index 9983298e4..5d20b226b 100644 --- a/tests/test_shell.cpp +++ b/tests/test_shell.cpp @@ -48,32 +48,39 @@ class TestClient: public TcpClient { protected: virtual void onConnect(const SockException &ex) override{ - //连接结果事件 + //连接结果事件 [AUTO-TRANSLATED:46887902] + // Connection established event InfoL << (ex ? ex.what() : "success"); } virtual void onRecv(const Buffer::Ptr &pBuf) override{ - //接收数据事件 + //接收数据事件 [AUTO-TRANSLATED:397ef7e4] + // Data received event DebugL << pBuf->data(); } virtual void onFlush() override{ - //发送阻塞后,缓存清空事件 + //发送阻塞后,缓存清空事件 [AUTO-TRANSLATED:46e8bca0] + // Buffer cleared after send blocking event DebugL; } virtual void onError(const SockException &ex) override{ - //断开连接事件,一般是EOF + //断开连接事件,一般是EOF [AUTO-TRANSLATED:7359fecf] + // Disconnected event, usually EOF WarnL << ex; } }; -//命令(http) +//命令(http) [AUTO-TRANSLATED:d96c7331] +// Command (http) class CMD_http: public CMD { public: CMD_http(){ _client.reset(new TestClient); _parser.reset(new OptionParser([this](const std::shared_ptr &stream,mINI &args){ - //所有选项解析完毕后触发该回调,我们可以在这里做一些全局的操作 + //所有选项解析完毕后触发该回调,我们可以在这里做一些全局的操作 [AUTO-TRANSLATED:cce43d55] + // All options parsed, trigger this callback, we can do some global operations here if(hasKey("connect")){ - //发起连接操作 + //发起连接操作 [AUTO-TRANSLATED:405329cf] + // Initiate connection operation connect(stream); return; } @@ -93,16 +100,19 @@ class CMD_http: public CMD { "tcp服务器地址,以冒号分隔端口号",/*该选项说明文字*/ [this](const std::shared_ptr &stream, const string &arg){/*解析到该选项的回调*/ if(arg.find(":") == string::npos){ - //中断后续选项的解析以及解析完毕回调等操作 + //中断后续选项的解析以及解析完毕回调等操作 [AUTO-TRANSLATED:15b7592f] + // Interrupt subsequent option parsing and parsing completion callback operations throw std::runtime_error("\t地址必须指明端口号."); } - //如果返回false则忽略后续选项的解析 + //如果返回false则忽略后续选项的解析 [AUTO-TRANSLATED:01a3d6bc] + // If return false, ignore subsequent option parsing return true; }); (*_parser) << Option('d', "disconnect", Option::ArgNone, nullptr ,false, "是否断开连接", [this](const std::shared_ptr &stream, const string &arg){ - //断开连接操作,所以后续的参数我们都不解析了 + //断开连接操作,所以后续的参数我们都不解析了 [AUTO-TRANSLATED:8aa1db56] + // Disconnect operation, so we don't parse subsequent parameters disconnect(stream); return false; }); @@ -159,7 +169,8 @@ int main(int argc,char *argv[]){ return 0; } GET_CMD("http").delOption("type"); - //初始化环境 + //初始化环境 [AUTO-TRANSLATED:efbad911] + // Initialize environment Logger::Instance().add(std::shared_ptr(new ConsoleChannel())); Logger::Instance().setWriter(std::shared_ptr(new AsyncLogWriter())); diff --git a/tests/test_sql.cpp b/tests/test_sql.cpp index 85d95ea98..d4e8b1998 100644 --- a/tests/test_sql.cpp +++ b/tests/test_sql.cpp @@ -17,13 +17,19 @@ using namespace std; using namespace toolkit; int main() { - //初始化日志 + //初始化日志 [AUTO-TRANSLATED:371bb4e5] + // Initialize log Logger::Instance().add(std::make_shared ()); #if defined(ENABLE_MYSQL) /* * 测试方法: * 请按照实际数据库情况修改源码然后编译执行测试 + /* + * Test method: + * Please modify the source code according to the actual database situation and then compile and run the test + + * [AUTO-TRANSLATED:0bf528e2] */ string sql_ip = "127.0.0.1"; unsigned short sql_port = 3306; @@ -32,28 +38,34 @@ int main() { string character = "utf8mb4"; #if defined(SUPPORT_DYNAMIC_TEMPLATE) - //初始化数据 + //初始化数据 [AUTO-TRANSLATED:62fabcf5] + // Initialize data SqlPool::Instance().Init(sql_ip,sql_port,"",user,password/*,character*/); #else - //由于需要编译器对可变参数模板的支持,所以gcc5.0以下一般都不支持,否则编译报错 + //由于需要编译器对可变参数模板的支持,所以gcc5.0以下一般都不支持,否则编译报错 [AUTO-TRANSLATED:fde51ba6] + // Because compiler support for variable parameter templates is required, versions below gcc5.0 generally do not support it, otherwise a compilation error will occur ErrorL << "your compiler does not support variable parameter templates!" << endl; return -1; #endif //defined(SUPPORT_DYNAMIC_TEMPLATE) - //建议数据库连接池大小设置跟CPU个数一致(大一点为佳) + //建议数据库连接池大小设置跟CPU个数一致(大一点为佳) [AUTO-TRANSLATED:14ca5335] + // It is recommended to set the database connection pool size to be consistent with the number of CPUs (slightly larger is better) SqlPool::Instance().setSize(3 + thread::hardware_concurrency()); vector > sqlRet; SqlWriter("create database test_db;", false) << sqlRet; SqlWriter("create table test_db.test_table(user_name varchar(128),user_id int auto_increment primary key,user_pwd varchar(128));", false) << sqlRet; - //同步插入 + //同步插入 [AUTO-TRANSLATED:0c010898] + // Synchronous insertion SqlWriter insertSql("insert into test_db.test_table(user_name,user_pwd) values('?','?');"); insertSql<< "zltoolkit" << "123456" << sqlRet; - //我们可以知道插入了几条数据,并且可以获取新插入(第一条)数据的rowID + //我们可以知道插入了几条数据,并且可以获取新插入(第一条)数据的rowID [AUTO-TRANSLATED:6c3fe4ae] + // We can know how many pieces of data were inserted, and we can get the rowID of the newly inserted (first) data DebugL << "AffectedRows:" << insertSql.getAffectedRows() << ",RowID:" << insertSql.getRowID(); - //同步查询 + //同步查询 [AUTO-TRANSLATED:6f60ace1] + // Synchronous query SqlWriter sqlSelect("select user_id , user_pwd from test_db.test_table where user_name='?' limit 1;") ; sqlSelect << "zltoolkit" ; @@ -86,13 +98,17 @@ int main() { DebugL << "unordered_map user_id:" << line["user_id"] << ",user_pwd:"<< line["user_pwd"]; } - //异步删除 + //异步删除 [AUTO-TRANSLATED:4359ab91] + // Asynchronous deletion SqlWriter insertDel("delete from test_db.test_table where user_name='?';"); insertDel << "zltoolkit" << endl; - //注意! - //如果SqlWriter 的 "<<" 操作符后面紧跟SqlPool::SqlRetType类型,则说明是同步操作并等待结果 - //如果紧跟std::endl ,则是异步操作,在后台线程完成sql操作。 + //注意! [AUTO-TRANSLATED:acc754b6] + // Note! + //如果SqlWriter 的 "<<" 操作符后面紧跟SqlPool::SqlRetType类型,则说明是同步操作并等待结果 [AUTO-TRANSLATED:be2b37a9] + // If the "<<" operator of SqlWriter is followed by SqlPool::SqlRetType type, it indicates a synchronous operation and waits for the result + //如果紧跟std::endl ,则是异步操作,在后台线程完成sql操作。 [AUTO-TRANSLATED:982b1ad9] + // If followed by std::endl, it is an asynchronous operation, which completes the sql operation in the background thread. #else ErrorL << "ENABLE_MYSQL not defined!" << endl; return -1; diff --git a/tests/test_ssl.cpp b/tests/test_ssl.cpp index 6cc38b39f..42d516c03 100644 --- a/tests/test_ssl.cpp +++ b/tests/test_ssl.cpp @@ -16,41 +16,53 @@ using namespace std; using namespace toolkit; int main(int argc,char *argv[]) { - //初始化设置日志 + //初始化设置日志 [AUTO-TRANSLATED:f8d72b7b] + // Initialize log settings Logger::Instance().add(std::make_shared ()); Logger::Instance().setWriter(std::make_shared()); - //加载证书,证书包含公钥和私钥 + //加载证书,证书包含公钥和私钥 [AUTO-TRANSLATED:fce78641] + // Load certificate, certificate contains public key and private key SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data()); SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data()); SSL_Initor::Instance().ignoreInvalidCertificate(false); - //定义客户端和服务端 + //定义客户端和服务端 [AUTO-TRANSLATED:d419f035] + // Define client and server SSL_Box client(false), server(true); - //设置客户端解密输出回调 + //设置客户端解密输出回调 [AUTO-TRANSLATED:b98ceb19] + // Set client decryption output callback client.setOnDecData([&](const Buffer::Ptr &buffer) { - //打印来自服务端数据解密后的明文 + //打印来自服务端数据解密后的明文 [AUTO-TRANSLATED:c672d9f5] + // Print plaintext from server after decryption InfoL << "client recv:" << buffer->toString(); }); - //设置客户端加密输出回调 + //设置客户端加密输出回调 [AUTO-TRANSLATED:e69a01e4] + // Set client encryption output callback client.setOnEncData([&](const Buffer::Ptr &buffer) { - //把客户端加密后的密文发送给服务端 + //把客户端加密后的密文发送给服务端 [AUTO-TRANSLATED:eb54076a] + // Send encrypted ciphertext from client to server server.onRecv(buffer); }); - //设置服务端解密输出回调 + //设置服务端解密输出回调 [AUTO-TRANSLATED:79eb87c8] + // Set server decryption output callback server.setOnDecData([&](const Buffer::Ptr &buffer) { - //打印来自客户端数据解密后的明文 + //打印来自客户端数据解密后的明文 [AUTO-TRANSLATED:71ba8425] + // Print plaintext from client after decryption InfoL << "server recv:" << buffer->toString(); - //把数据回显给客户端 + //把数据回显给客户端 [AUTO-TRANSLATED:cb8fd00a] + // Echo data back to client server.onSend(buffer); }); - //设置服务端加密输出回调 + //设置服务端加密输出回调 [AUTO-TRANSLATED:b39c8f28] + // Set server-side encryption output callback server.setOnEncData([&](const Buffer::Ptr &buffer) { - //把加密的回显信息回复给客户端; + //把加密的回显信息回复给客户端; [AUTO-TRANSLATED:cd3754ed] + // Return the encrypted echo information to the client; client.onRecv(buffer); }); @@ -62,7 +74,8 @@ int main(int argc,char *argv[]) { if (input == "quit") { break; } - //把明文数据输入给客户端 + //把明文数据输入给客户端 [AUTO-TRANSLATED:0c10359d] + // Input plaintext data to the client client.onSend(std::make_shared(std::move(input))); } return 0; diff --git a/tests/test_tcpClient.cpp b/tests/test_tcpClient.cpp index 17ba67fcd..1fbed7f0c 100644 --- a/tests/test_tcpClient.cpp +++ b/tests/test_tcpClient.cpp @@ -26,23 +26,28 @@ class TestClient: public TcpClient { } protected: virtual void onConnect(const SockException &ex) override{ - //连接结果事件 + //连接结果事件 [AUTO-TRANSLATED:46887902] + // Connection established event InfoL << (ex ? ex.what() : "success"); } virtual void onRecv(const Buffer::Ptr &pBuf) override{ - //接收数据事件 + //接收数据事件 [AUTO-TRANSLATED:397ef7e4] + // Data received event DebugL << pBuf->data() << " from port:" << get_peer_port(); } virtual void onFlush() override{ - //发送阻塞后,缓存清空事件 + //发送阻塞后,缓存清空事件 [AUTO-TRANSLATED:46e8bca0] + // Send blocked, cache cleared event DebugL; } virtual void onError(const SockException &ex) override{ - //断开连接事件,一般是EOF + //断开连接事件,一般是EOF [AUTO-TRANSLATED:7359fecf] + // Disconnected event, usually EOF WarnL << ex.what(); } virtual void onManager() override{ - //定时发送数据到服务器 + //定时发送数据到服务器 [AUTO-TRANSLATED:688c9148] + // Periodically send data to the server auto buf = BufferRaw::create(); if(buf){ buf->assign("[BufferRaw]\0"); @@ -58,7 +63,8 @@ class TestClient: public TcpClient { int main() { - // 设置日志系统 + // 设置日志系统 [AUTO-TRANSLATED:45646031] + // Set up the logging system Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); @@ -68,7 +74,8 @@ int main() { TcpClientWithSSL::Ptr clientSSL(new TcpClientWithSSL());//必须使用智能指针 clientSSL->startConnect("127.0.0.1",9001);//连接服务器 - //退出程序事件处理 + //退出程序事件处理 [AUTO-TRANSLATED:80065cb7] + // Exit program event handling static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_tcpEchoServer.cpp b/tests/test_tcpEchoServer.cpp index 7e3d08a28..ee2613647 100644 --- a/tests/test_tcpEchoServer.cpp +++ b/tests/test_tcpEchoServer.cpp @@ -33,16 +33,19 @@ class EchoSession: public Session { DebugL; } virtual void onRecv(const Buffer::Ptr &buf) override{ - //处理客户端发送过来的数据 + //处理客户端发送过来的数据 [AUTO-TRANSLATED:c095b82e] + // Handle data sent from the client TraceL << buf->data() << " from port:" << get_local_port(); send(buf); } virtual void onError(const SockException &err) override{ - //客户端断开连接或其他原因导致该对象脱离TCPServer管理 + //客户端断开连接或其他原因导致该对象脱离TCPServer管理 [AUTO-TRANSLATED:6b958a7b] + // Client disconnects or other reasons cause the object to be removed from TCPServer management WarnL << err; } virtual void onManager() override{ - //定时管理该对象,譬如会话超时检查 + //定时管理该对象,譬如会话超时检查 [AUTO-TRANSLATED:2caa54f6] + // Periodically manage the object, such as session timeout check DebugL; } @@ -52,11 +55,13 @@ class EchoSession: public Session { int main() { - //初始化日志模块 + //初始化日志模块 [AUTO-TRANSLATED:fd9321b2] + // Initialize the log module Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); - //加载证书,证书包含公钥和私钥 + //加载证书,证书包含公钥和私钥 [AUTO-TRANSLATED:fce78641] + // Load the certificate, the certificate contains the public key and private key SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data()); SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data()); SSL_Initor::Instance().ignoreInvalidCertificate(false); @@ -67,7 +72,8 @@ int main() { TcpServer::Ptr serverSSL(new TcpServer()); serverSSL->start >(9001);//监听9001端口 - //退出程序事件处理 + //退出程序事件处理 [AUTO-TRANSLATED:80065cb7] + // Exit program event handling static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_threadPool.cpp b/tests/test_threadPool.cpp index af495f742..80d4b68a4 100644 --- a/tests/test_threadPool.cpp +++ b/tests/test_threadPool.cpp @@ -18,15 +18,18 @@ using namespace std; using namespace toolkit; int main() { - //初始化日志系统 + //初始化日志系统 [AUTO-TRANSLATED:25c549de] + // Initialize the logging system Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); ThreadPool pool(thread::hardware_concurrency(), ThreadPool::PRIORITY_HIGHEST, true); - //每个任务耗时3秒 + //每个任务耗时3秒 [AUTO-TRANSLATED:c1b83e8e] + // Each task takes 3 seconds auto task_second = 3; - //每个线程平均执行4次任务,总耗时应该为12秒 + //每个线程平均执行4次任务,总耗时应该为12秒 [AUTO-TRANSLATED:ceab38cc] + // Each thread executes 4 tasks on average, the total time should be 12 seconds auto task_count = thread::hardware_concurrency() * 4; semaphore sem; @@ -34,7 +37,8 @@ int main() { vec.resize(task_count); Ticker ticker; { - //放在作用域中确保token引用次数减1 + //放在作用域中确保token引用次数减1 [AUTO-TRANSLATED:a81d2393] + // Put it in a scope to ensure the token reference count is decremented auto token = std::make_shared(nullptr, [&]() { sem.post(); }); @@ -52,7 +56,8 @@ int main() { sem.wait(); InfoL << "all task done, used milliseconds:" << ticker.elapsedTime(); - //打印执行结果 + //打印执行结果 [AUTO-TRANSLATED:08995cc8] + // Print the execution result for (auto i = 0; i < task_count; ++i) { InfoL << vec[i]; } diff --git a/tests/test_threadPoolBenchmark.cpp b/tests/test_threadPoolBenchmark.cpp index 6b32a3499..d6f788543 100644 --- a/tests/test_threadPoolBenchmark.cpp +++ b/tests/test_threadPoolBenchmark.cpp @@ -22,7 +22,8 @@ int main() { signal(SIGINT,[](int ){ exit(0); }); - //初始化日志系统 + //初始化日志系统 [AUTO-TRANSLATED:25c549de] + // Initialize the logging system Logger::Instance().add(std::make_shared ()); atomic_llong count(0); @@ -39,7 +40,8 @@ int main() { InfoL << "1000万任务入队耗时:" << ticker.elapsedTime() << "ms" << endl; uint64_t lastCount = 0 ,nowCount = 1; ticker.resetTime(); - //此处才开始启动线程 + //此处才开始启动线程 [AUTO-TRANSLATED:b68d0810] + // The thread starts here pool.start(); while (true){ sleep(1); diff --git a/tests/test_timer.cpp b/tests/test_timer.cpp index 5935e23aa..6a39ae9b2 100644 --- a/tests/test_timer.cpp +++ b/tests/test_timer.cpp @@ -19,7 +19,8 @@ using namespace std; using namespace toolkit; int main() { - //设置日志 + //设置日志 [AUTO-TRANSLATED:50372045] + // Set log Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); @@ -43,7 +44,8 @@ int main() { throw std::runtime_error("timer2,测试任务中抛异常"); },nullptr); - //退出程序事件处理 + //退出程序事件处理 [AUTO-TRANSLATED:80065cb7] + // Exit program event handling static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 sem.wait(); diff --git a/tests/test_udpSock.cpp b/tests/test_udpSock.cpp index ea718c43b..dd972c5b5 100644 --- a/tests/test_udpSock.cpp +++ b/tests/test_udpSock.cpp @@ -17,23 +17,28 @@ using namespace std; using namespace toolkit; -//主线程退出标志 +//主线程退出标志 [AUTO-TRANSLATED:4465f04c] +// Main thread exit flag bool exitProgram = false; -//赋值struct sockaddr +//赋值struct sockaddr [AUTO-TRANSLATED:07f9df9d] +// Assign struct sockaddr void makeAddr(struct sockaddr_storage *out,const char *ip,uint16_t port){ *out = SockUtil::make_sockaddr(ip, port); } -//获取struct sockaddr的IP字符串 +//获取struct sockaddr的IP字符串 [AUTO-TRANSLATED:651562f1] +// Get IP string from struct sockaddr string getIP(struct sockaddr *addr){ return SockUtil::inet_ntoa(addr); } int main() { - //设置程序退出信号处理函数 + //设置程序退出信号处理函数 [AUTO-TRANSLATED:419fb1c3] + // Set program exit signal handling function signal(SIGINT, [](int){exitProgram = true;}); - //设置日志系统 + //设置日志系统 [AUTO-TRANSLATED:ad15b8d6] + // Set up logging system Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); @@ -43,7 +48,8 @@ int main() { sockSend->bindUdpSock(0, "0.0.0.0");//发送UDP随机端口 sockRecv->setOnRead([](const Buffer::Ptr &buf, struct sockaddr *addr , int){ - //接收到数据回调 + //接收到数据回调 [AUTO-TRANSLATED:1cd064ad] + // Data received callback DebugL << "recv data form " << getIP(addr) << ":" << buf->data(); }); @@ -52,7 +58,8 @@ int main() { // sockSend->bindPeerAddr(&addrDst); int i = 0; while(!exitProgram){ - //每隔一秒往对方发送数据 + //每隔一秒往对方发送数据 [AUTO-TRANSLATED:d70ac05f] + // Send data to the other side every second sockSend->send(to_string(i++), (struct sockaddr *)&addrDst); sleep(1); } diff --git a/tests/test_variant.cpp b/tests/test_variant.cpp index 51b9dca14..3066c7bbc 100644 --- a/tests/test_variant.cpp +++ b/tests/test_variant.cpp @@ -14,7 +14,8 @@ using namespace toolkit; int main() { - //设置日志 + //设置日志 [AUTO-TRANSLATED:50372045] + // Set log Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared());