|
@@ -0,0 +1,300 @@
|
|
|
+#include "stdafx.h"
|
|
|
+#include "ProcessRemodule.h"
|
|
|
+
|
|
|
+#include<Windows.h>
|
|
|
+#include<iostream>
|
|
|
+using namespace std;
|
|
|
+//#include <thread>
|
|
|
+//#include <mutex>
|
|
|
+//#include <chrono>
|
|
|
+
|
|
|
+#define BUF_SIZE 5001
|
|
|
+#define REAL_BUF BUF_SIZE-1
|
|
|
+ParseData* gBuf = NULL;
|
|
|
+unsigned int gBufFlag[BUF_SIZE] = {0};
|
|
|
+HANDLE gBufMutex;
|
|
|
+
|
|
|
+HANDLE hThread[MSG_PROCESS_THREAD_NUM];
|
|
|
+unsigned nThreadID[MSG_PROCESS_THREAD_NUM];
|
|
|
+
|
|
|
+HANDLE hSqlThread[SQL_PROCESS_THREAD_NUM];
|
|
|
+unsigned nSqlThreadID[SQL_PROCESS_THREAD_NUM];
|
|
|
+
|
|
|
+HANDLE hErrLogThread[ERR_LOG_PROCESS_THREAD_NUM];
|
|
|
+unsigned nErrLogThreadID[ERR_LOG_PROCESS_THREAD_NUM];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+BufferUtility* BufferUtility::instance = NULL;
|
|
|
+BufferUtility* BufferUtility::getInstance()
|
|
|
+{
|
|
|
+ if (instance == NULL)
|
|
|
+ {
|
|
|
+ if (instance == NULL)
|
|
|
+ {
|
|
|
+ instance = new BufferUtility();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+}
|
|
|
+
|
|
|
+int BufferUtility::init()
|
|
|
+{
|
|
|
+ gBuf = (ParseData *)malloc(sizeof(ParseData) * BUF_SIZE);
|
|
|
+ memset(gBuf, 0, sizeof(ParseData) * BUF_SIZE);
|
|
|
+ memset(gBufFlag, 0, sizeof(gBufFlag));
|
|
|
+ gBufMutex =CreateMutex(NULL, FALSE, NULL);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+ParseData* BufferUtility::getItem()
|
|
|
+{
|
|
|
+ int i = 0;
|
|
|
+ WaitForSingleObject(gBufMutex,INFINITE);
|
|
|
+ for(i=0; i<REAL_BUF; i++)
|
|
|
+ {
|
|
|
+ if(0 == gBufFlag[i])
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(i == REAL_BUF)
|
|
|
+ {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ gBufFlag[i] = 1;
|
|
|
+ ReleaseMutex(gBufMutex);
|
|
|
+ return &(gBuf[i]);
|
|
|
+}
|
|
|
+
|
|
|
+int BufferUtility::releaseItem(int i)
|
|
|
+{
|
|
|
+ WaitForSingleObject(gBufMutex,INFINITE);
|
|
|
+ gBufFlag[i] = 0;
|
|
|
+ memset(&(gBuf[i]), 0, sizeof(ParseData));
|
|
|
+ ReleaseMutex(gBufMutex);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int BufferUtility::releaseItem(ParseData* itemPtr)
|
|
|
+{
|
|
|
+ int i = 0;
|
|
|
+ WaitForSingleObject(gBufMutex,INFINITE);
|
|
|
+ for(i=0; i<REAL_BUF; i++)
|
|
|
+ {
|
|
|
+ if(itemPtr == (ParseData*)(&(gBuf[i])))
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ gBufFlag[i] = 0;
|
|
|
+ memset(&(gBuf[i]), 0, sizeof(ParseData));
|
|
|
+ ReleaseMutex(gBufMutex);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int message_process_thread_init()
|
|
|
+{
|
|
|
+ //登录时初始化缓冲区
|
|
|
+ BufferUtility* bufferInstance = BufferUtility::getInstance();
|
|
|
+ bufferInstance->init();
|
|
|
+ //初始化消息处理线程
|
|
|
+
|
|
|
+ int i = 0;
|
|
|
+ for(i=0; i<MSG_PROCESS_THREAD_NUM; i++)
|
|
|
+ {
|
|
|
+ hThread[i] = (HANDLE)_beginthreadex( NULL, 0, &message_process_entry, NULL, 0, &(nThreadID[i]) );
|
|
|
+ if(hThread[i] == 0)
|
|
|
+ {
|
|
|
+ printf("start thread failed,errno:%d\n",::GetLastError());
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+unsigned __stdcall message_process_entry(void *param)
|
|
|
+{
|
|
|
+ printf("thread message_process_entry start\n");
|
|
|
+
|
|
|
+ MSG msg;
|
|
|
+ //check for msg quene isExist or not
|
|
|
+ PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
|
|
+
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ if(GetMessage(&msg,0,0,0)) //get msg from message queue
|
|
|
+ {
|
|
|
+ switch(msg.message)
|
|
|
+ {
|
|
|
+ case TCP_MSG:
|
|
|
+ //add message process
|
|
|
+ ParseData* data = (ParseData*)(msg.wParam);
|
|
|
+ #ifndef UT_TEST
|
|
|
+ CYAServerDlg* dlg = reinterpret_cast<CYAServerDlg*>(data->handle);
|
|
|
+ //#ifdef TRACE_MEMORY_PARSE_PACKAGE
|
|
|
+ dlg->parse_package_data(data->buf, data->len, data->dwConnID);
|
|
|
+ //#endif // TRACE_MEMORY_PARSE_PACKAGE
|
|
|
+ BufferUtility::getInstance()->releaseItem(data);
|
|
|
+ break;
|
|
|
+ #else
|
|
|
+ //printf("current thread id: %d, receive tcp msg:%d\n", GetCurrentThreadId(), data->a);
|
|
|
+ BufferUtility::getInstance()->releaseItem(data);
|
|
|
+ #endif
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int send_tcp_thread_message(ParseData* data)
|
|
|
+{
|
|
|
+ //send thread message
|
|
|
+ unsigned thread_id = rand() % MSG_PROCESS_THREAD_NUM;
|
|
|
+ if(!PostThreadMessage(nThreadID[thread_id], TCP_MSG, (WPARAM)data, 0))//post thread msg
|
|
|
+ {
|
|
|
+ PostThreadMessage(nThreadID[thread_id], TCP_MSG, (WPARAM)data, 0);
|
|
|
+ printf("post message failed,errno:%d\n",::GetLastError());
|
|
|
+ BufferUtility::getInstance()->releaseItem(data);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int sql_process_thread_init()
|
|
|
+{
|
|
|
+ int i = 0;
|
|
|
+ for(i=0; i<SQL_PROCESS_THREAD_NUM; i++)
|
|
|
+ {
|
|
|
+ hSqlThread[i] = (HANDLE)_beginthreadex( NULL, 0, &sql_process_entry, NULL, 0, &(nSqlThreadID[i]) );
|
|
|
+ if(hSqlThread[i] == 0)
|
|
|
+ {
|
|
|
+ printf("start thread failed,errno:%d\n",::GetLastError());
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+// thread function
|
|
|
+unsigned __stdcall sql_process_entry(void *param)
|
|
|
+{
|
|
|
+ printf("thread sql_process_entry start\n");
|
|
|
+
|
|
|
+ MSG msg;
|
|
|
+ //check for msg quene isExist or not
|
|
|
+ PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ if(GetMessage(&msg,0,0,0)) //get msg from message queue
|
|
|
+ {
|
|
|
+ switch(msg.message)
|
|
|
+ {
|
|
|
+ case SQL_MSG:
|
|
|
+ char * pInfo = (char *)msg.wParam;
|
|
|
+ //printf("current thread id: %d,recv %s\n", GetCurrentThreadId(), pInfo);
|
|
|
+ //process sql command
|
|
|
+
|
|
|
+ #ifndef UT_TEST
|
|
|
+ _exec_sql(pInfo);
|
|
|
+ #endif
|
|
|
+ delete[] pInfo;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+int send_sql_message(char* pchr)
|
|
|
+{
|
|
|
+ //send thread message
|
|
|
+ unsigned thread_id = rand() % SQL_PROCESS_THREAD_NUM;
|
|
|
+ if(!PostThreadMessage(nSqlThreadID[thread_id], SQL_MSG, (WPARAM)pchr, 0))//post thread msg
|
|
|
+ {
|
|
|
+ PostThreadMessage(nSqlThreadID[thread_id], SQL_MSG, (WPARAM)pchr, 0);
|
|
|
+ printf("post message failed,errno:%d\n",::GetLastError());
|
|
|
+ delete[] pchr;
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int err_log_process_thread_init()
|
|
|
+{
|
|
|
+ int i = 0;
|
|
|
+ for(i=0; i<ERR_LOG_PROCESS_THREAD_NUM; i++)
|
|
|
+ {
|
|
|
+ hErrLogThread[i] = (HANDLE)_beginthreadex( NULL, 0, &err_log_process_entry, NULL, 0, &(nErrLogThreadID[i]) );
|
|
|
+ if(hErrLogThread[i] == 0)
|
|
|
+ {
|
|
|
+ printf("start thread failed,errno:%d\n",::GetLastError());
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+// thread function
|
|
|
+unsigned __stdcall err_log_process_entry(void *param)
|
|
|
+{
|
|
|
+ //printf("thread err log_process_entry start\n");
|
|
|
+
|
|
|
+ MSG msg;
|
|
|
+ //check for msg quene isExist or not
|
|
|
+ PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ if(GetMessage(&msg,0,0,0)) //get msg from message queue
|
|
|
+ {
|
|
|
+ switch(msg.message)
|
|
|
+ {
|
|
|
+ case ERR_LOG_MSG:
|
|
|
+ char * pInfo = (char *)msg.wParam;
|
|
|
+
|
|
|
+ #ifndef UT_TEST
|
|
|
+ _write_error_log(pInfo);
|
|
|
+ #else
|
|
|
+ delete[] pInfo;
|
|
|
+ #endif
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+int send_err_log_message(char* pchr)
|
|
|
+{
|
|
|
+ //send thread message
|
|
|
+ unsigned thread_id = rand() % ERR_LOG_PROCESS_THREAD_NUM;
|
|
|
+ if(!PostThreadMessage(nErrLogThreadID[thread_id], ERR_LOG_MSG, (WPARAM)pchr, 0))//post thread msg
|
|
|
+ {
|
|
|
+ PostThreadMessage(nErrLogThreadID[thread_id], ERR_LOG_MSG, (WPARAM)pchr, 0);
|
|
|
+ printf("post message failed,errno:%d\n",::GetLastError());
|
|
|
+ delete[] pchr;
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int service_task_init()
|
|
|
+{
|
|
|
+ BufferUtility* bufPtr = BufferUtility::getInstance();
|
|
|
+ bufPtr->init();
|
|
|
+ message_process_thread_init();
|
|
|
+ sql_process_thread_init();
|
|
|
+ err_log_process_thread_init();
|
|
|
+ ::Sleep(1000);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|