📄 test_frame.c
字号:
/*** HMYLISTERNER /frame/mylisterner.h* "监听器"测试程序**/#include "mylisterner.h"#ifdef WIN32#include <windows.h>#else#include <unistd.h>#define Sleep(x) sleep(x/1000)#endif#include "mylog.h"#include <assert.h>#include "mypipe.h"#include "mysysvmsg.h"static void test_timer_interface(HMYLISTERNER lsn);static void test_msg(HMYLISTERNER lsn);static void test_fd(HMYLISTERNER lsn);static void test_sys_msg(HMYLISTERNER lsn);void test_listerner(){ /* * 构造监听器, * NULL表示不使用内存池, * 0表示使用默认的消息队列大小(优先级与不带优先级) */ HMYLISTERNER lsn = MyListernerConstruct(NULL, 0); /* * 运行监听线程 */ MyListernerRun(lsn); /* * 测试sysvmsg */ test_sys_msg(lsn); /* * 测试定时器接口 */ test_timer_interface(lsn); /* * 测试消息接口 */ test_msg(lsn); /* * 测试fd接口 */ test_fd(lsn); Sleep(10 * 1000); MyListernerDestruct(lsn); MyMemPoolMemReport(1);}static int test_timer_thread_cb(unsigned long context_data, unsigned long timer_user_data){ struct timeval now = {0}; gettimeofday(&now, NULL); LOG_DEBUG(("context:%d timer:%d %d %d\r\n", context_data, timer_user_data, now.tv_sec, now.tv_usec)); } static void test_timer_interface(HMYLISTERNER lsn){ HTIMERID timer_id = NULL; HTIMERID timer_id1 = NULL; /* 添加定时器 */ { mytimer_node_t node = {0}; node.timeout_cb = test_timer_thread_cb; node.context_data = 1231; node.timer_user_data = 1231; node.first_expire.tv_sec = 1; node.first_expire.tv_usec = 0; node.period.tv_sec = 5; node.period.tv_usec = 500 * 1000; timer_id1 = MyListernerAddTimer(lsn, &node); } Sleep(2 * 1000); { mytimer_node_t node = {0}; node.timeout_cb = test_timer_thread_cb; node.context_data = 1235; node.timer_user_data = 1235; node.first_expire.tv_sec = 1; node.first_expire.tv_usec = 0; node.period.tv_sec = 1; node.period.tv_usec = 0; timer_id = MyListernerAddTimer(lsn, &node); } Sleep(2 * 1000); MyListernerDelTimer(lsn, timer_id); Sleep(3 * 1000); { mytimer_node_t node = {0}; node.timeout_cb = test_timer_thread_cb; node.context_data = 1236; node.timer_user_data = 1236; node.first_expire.tv_sec = 1; node.first_expire.tv_usec = 0; node.period.tv_sec = 0; node.period.tv_usec = 500 * 1000; timer_id = MyListernerResetTimer(lsn, timer_id1, &node); }}static int cb_test_lsn_promsg(unsigned long context_data, void * msg){ printf("%d ", (int)msg); return 0;}static void test_msg(HMYLISTERNER lsn){ int i = 0; LOG_INFO(("测试带优先级的消息队列")); for(i = 0; i < 50; i ++) { MyListernerAddProrityMsg(lsn, i, (void *)i, 1, cb_test_lsn_promsg); //if(!(i % 25)) // Sleep(1 * 1000); } printf("\n"); Sleep(1000); LOG_INFO(("测试不带优先级的消息队列")); for(i = 0; i < 50; i ++) { MyListernerAddMsg(lsn, (void *)i, 2, cb_test_lsn_promsg); //if(!(i % 25)) // Sleep(1 * 1000); } printf("\n");}static int cb_listerner_input(unsigned long context_data, int fd){ char actemp[256] = {0}; int ret = 0; ret = read(fd, actemp, sizeof(actemp)); LOG_INFO(("context_data:%d fd:%d [%d:%s]", context_data, fd, ret, actemp)); return 0; }static void test_fd(HMYLISTERNER lsn){ int i = 0; HMYPIPE hpipe1 = MyPipeConstruct(NULL); HMYPIPE hpipe2 = MyPipeConstruct(NULL); { event_handle_t evt_handle = {cb_listerner_input, NULL, NULL, 1}; MyListernerAddFD(lsn, MyPipeGetReadFD(hpipe1), E_FD_READ, &evt_handle); } { event_handle_t evt_handle = {cb_listerner_input, NULL, NULL, 2}; MyListernerAddFD(lsn, MyPipeGetReadFD(hpipe2), E_FD_READ, &evt_handle); } for(i = 0; i < 10; i ++) { int j = 0; char actemp[] = "pipe1 msg"; snprintf(actemp, sizeof(actemp), "pipe%d msg", i); MyPipeWrite(hpipe1, actemp, sizeof(actemp)); MyPipeWrite(hpipe1, actemp, sizeof(actemp)); //usleep(1); //Sleep(1000); } for(i = 0; i < 10; i ++) { int j = 0; char actemp[] = "pipe1 msg"; snprintf(actemp, sizeof(actemp), "pipe%d msg", i); MyPipeWrite(hpipe2, actemp, sizeof(actemp)); MyPipeWrite(hpipe2, actemp, sizeof(actemp)); //usleep(1);; //Sleep(1000); } Sleep(3 * 1000); MyListernerDelFD(lsn, MyPipeGetReadFD(hpipe1)); MyListernerDelFD(lsn, MyPipeGetReadFD(hpipe2)); { char actemp[] = "pipe1 msg"; snprintf(actemp, sizeof(actemp), "pipe%d msg", 1); MyPipeWrite(hpipe2, actemp, sizeof(actemp)); Sleep(1000); } { char actemp[] = "pipe1 msg"; snprintf(actemp, sizeof(actemp), "pipe%d msg", 1); MyPipeWrite(hpipe1, actemp, sizeof(actemp)); Sleep(1000); } MyPipeDestruct(hpipe1); MyPipeDestruct(hpipe2);}static int cb_listerner_input_sysmq(unsigned long context_data, int fd){ char actemp[256] = {0}; int ret = 0; static count = 0; ret = MySVMsgQRead((HMYSVMSGQ)context_data, actemp, sizeof(actemp), 0); LOG_WARN(("context_data:%d fd:%d [%d:%s] %d", context_data, fd, ret, actemp, ++count)); return 0; }static void test_sys_msg(HMYLISTERNER lsn){ int i = 0; HMYSVMSGQ hsysvmq1 = MySVMsgQConstruct(NULL, 1000, 1, 0, "abc"); HMYSVMSGQ hsysvmq2 = MySVMsgQConstruct(NULL, 1000, 0, 0, "abc"); HMYSVMSGQ hsysvmq3 = MySVMsgQGet(NULL, MySVMsgQGetID(hsysvmq1), "abc"); LOG_WARN(("hsysvmq1:%x hsysvmq2:%x hsysvmq3:%x", hsysvmq1, hsysvmq2, hsysvmq3)); { event_handle_t evt_handle = {cb_listerner_input_sysmq, NULL, NULL, (unsigned long)hsysvmq1}; MyListernerAddFD(lsn, MySVMsgQGetSelectFd(hsysvmq1), E_FD_READ, &evt_handle); } for(i = 0; i < 10; i ++) { MySVMsgQWrite(hsysvmq2, "abc", strlen("abc")); MySVMsgQWrite(hsysvmq3, "xyz", strlen("xyz")); //sleep(1); } sleep(1); MyListernerDelFD(lsn, MySVMsgQGetSelectFd(hsysvmq1)); MySVMsgQDestruct(hsysvmq1); MySVMsgQDestruct(hsysvmq2); MySVMsgQDestruct(hsysvmq3);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -