|
|

代码从写出来到整理完成有好久了,如果有什么疑问或者代码中发现有缺陷,直接与我联系duanjigang1983@126.com
概述一下原理和每个类的作用
(1): CTask 一个任务基类,作为线程的参数用
(2): CWorkThread:工作线程类,轮询检测参数是否可用,如果可用的话,
就去调用参数CTask的执 行函数Execute,如果参数不可用就等待
(3): CWorkQueue:是一个任务队列,里面装载CTask子类的对象,是一个对象的容器
(4): CWorkThreadPool:工作线程池,其中包含了工作线程组,调度线程,调度线程不停的检测
任务队列中是否有可用任务对象,如过取到一个有效的任务对象,就从工作线程组中查找出来
一个空闲的工作线程,把这个任务交给这个空闲的线程去执行,如果没有空闲线程则创建新
的工作线程并添加到工作线程组中。
(1),(2),(3),(4)组成了库的核心,可以稍加修改进行应用
其他附属类:
(5):CSimpleUDP 一个简易的封装的UDP通讯类
(6):CLogHelper 一个线程安全的日志类,将日志存储到指定文件并能打印到标准输出
特点:支持windows和Linux平台
使用举例:CUDPClient 和CUDPServer 一个线程发送UDP包,另外一个接收UDP包
使用方法:
在src目录执行make命令,相应的库将生成到/usr/local/lib下
然后在/etc/ld.so.conf中添加/usr/local/lib
保存退出
执行ldconfig -v
开发时,只需要新建的类继承CTask类,然后将编译好的Tasklib库链接进去就可,Tasklib封装了main函数和线程以及任务队
列的管理,最好修改UDPClient的例子
日志:linux系统存放在/cucme.log,windows存放在C:\cucme.log中,如需修改,直接修改头文件,重新编译库。
注意:在windows先使用时最好将库文件一起重新编译,链接使用lib文件有异常存在。另外运行UDPClient例子时,首先
将UDPCLient.cpp中的IP地址进行修改,否则发送目的地错误。server将不能接收到。
代码有些地方可能会写的比较臭,希望高手赐教,更希望有朋友能在系统框架设计上给出好的建议。
如果有改进或者新的应用例子,我会及时贴上来的- #define _COMMON_H_
- #define __DEBUG__
- #include
- #include
- #include
- #include "stdio.h"
- ////////////////
- #include
- #ifdef _WIN32
- #pragma comment(lib, "Ws2_32.lib")
- #include
- #include
- const char * const LogFile = "C://cucme.log";
- #define CLOSE_FILE(fd) _close(fd)
- #endif
- #ifdef __LINUX__
- #include
- #include
- #include
- const char* const LogFile = "/cucme.log";
- #endif
- #ifdef _WIN32
- //head file
- #include
- #include
- #pragma comment(lib, "Ws2_32.lib")
- //关闭套结字
- #define CLOSE(s) closesocket(s);
- #define CLEAN_SCREEEN system("cls");
- static void SLEEP(int t1, int t2)
- {
- Sleep(t1*1000 + t2);
- }
- //types
- typedef CRITICAL_SECTION LockType;
- typedef int socklen_t;
- #endif
- #ifdef __LINUX__
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define CLEAN_SCREEEN system("clear");
- #define CLOSE(s) close(s)
- static void SLEEP(int t1, int t2)
- {
- struct timeval tm;
- tm.tv_sec = t1;
- tm.tv_usec = t2;
- select(0, NULL, NULL, NULL, &tm);
- }
- typedef pthread_mutex_t LockType;
- #endif
- //---------------------------global functions-------------------------
- //初始化锁变量
- static int InitLock(LockType * lock)
- {
- #ifdef _WIN32
- InitializeCriticalSection(lock);
- #else
- if(pthread_mutex_init(lock, NULL) != 0)
- {
- return -1;
- }
- #endif
- return 1;
- }
- //加锁操作
- static int LockOn(LockType* lock)
- {
- #ifdef _WIN32
- EnterCriticalSection(lock);
- #endif
- #ifdef __LINUX__
- if(pthread_mutex_lock(lock) != 0)
- {
- return -1;
- }
- #endif
- return 1;
- }
- //解锁操作
- static int LockOff(LockType* lock)
- {
- #ifdef _WIN32
- LeaveCriticalSection(lock);
- #endif
- #ifdef __LINUX__
- if(pthread_mutex_unlock(lock) != 0)
- {
- return -1;
- }
- #endif
- return 1;
- }
- //初始化网络
- static int InitNetWork()
- {
- #ifdef _WIN32
- WSAData w;
- if(WSAStartup(0x0101, &w) != 0)
- {
- return -1;
- }
- #endif
- srand((unsigned)time(NULL));
- return 1;
- }
- //关闭网络连接
- static int TerminateNetWork()
- {
- #ifdef _WIN32
- if(WSACleanup())
- {
- return -1;
- }
- #endif
- return 1;
- }
- static LockType LockPrint;
- static bool Initted = false;
- static void ThreadSafePrint(const char * szMsg)
- {
- if(!Initted)
- {
- InitLock(&LockPrint);
- Initted = true;
- }
- LockOn(&LockPrint);
- #ifdef __DEBUG__
- printf(szMsg);
- #endif
- LockOff(&LockPrint);
- }
- #endif
复制代码 |
|