Bläddra i källkod

_exec_sql with delete

lihz 8 år sedan
förälder
incheckning
90cde8c087
4 ändrade filer med 191 tillägg och 203 borttagningar
  1. 184 187
      ProcessRemodule.cpp
  2. 1 1
      QueueStrManager.h
  3. 4 14
      YAServerDlg.cpp
  4. 2 1
      YAServerDlg.h

+ 184 - 187
ProcessRemodule.cpp

@@ -40,13 +40,13 @@ BufferUtility* BufferUtility::instance = NULL;
 BufferUtility* BufferUtility::getInstance()
 {
 	if (instance == NULL)
-    {
-    	if (instance == NULL)
-    	{
-       		instance = new BufferUtility;
-    	}
-    }
-    return instance;
+	{
+		if (instance == NULL)
+		{
+			instance = new BufferUtility;
+		}
+	}
+	return instance;
 }
 
 int BufferUtility::init()
@@ -74,7 +74,7 @@ ParseData* BufferUtility::getItem()
 		return NULL;
 	}
 	gBufFlag[i] = 1;
-    ReleaseMutex(gBufMutex);
+	ReleaseMutex(gBufMutex);
 	return &(gBuf[i]);
 }
 
@@ -83,7 +83,7 @@ int BufferUtility::releaseItem(int i)
 	WaitForSingleObject(gBufMutex,INFINITE);
 	gBufFlag[i] = 0;
 	memset(&(gBuf[i]), 0, sizeof(ParseData));
-    ReleaseMutex(gBufMutex);
+	ReleaseMutex(gBufMutex);
 	return 0;
 }
 
@@ -100,14 +100,14 @@ int BufferUtility::releaseItem(ParseData* itemPtr)
 	}
 	gBufFlag[i] = 0;
 	memset(&(gBuf[i]), 0, sizeof(ParseData));
-    ReleaseMutex(gBufMutex);
+	ReleaseMutex(gBufMutex);
 	return 0;
 }
 
 
 int message_process_thread_init()
 {
-    //登录时初始化缓冲区
+	//登录时初始化缓冲区
 	BufferUtility* bufferInstance = BufferUtility::getInstance();
 	bufferInstance->init();
 	//初始化消息处理线程
@@ -115,71 +115,71 @@ int message_process_thread_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;
-        }
+		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)
 {
-	 MSG msg;
-	 //check for msg quene isExist or not
-	 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
-	
-	 while(true)
-	 {
+	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
-                     try
-                     {
-                         dlg->parse_package_data(data->buf, data->len, data->dwConnID);
-                     }
-					 catch(exception e)
-					 {
-					     //printf(e.what());
-					 }
-                     //#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
-					 
-             }
-		 }
-	 }
+		{
+			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
+				try
+				{
+					dlg->parse_package_data(data->buf, data->len, data->dwConnID);
+				}
+				catch(exception e)
+				{
+					//printf(e.what());
+				}
+				//#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
-    LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_91);
+	//send thread message
+	LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_91);
 	unsigned thread_id = rand() % MSG_PROCESS_THREAD_NUM;
 	if(!PostThreadMessage(nThreadID[thread_id], TCP_MSG, (WPARAM)data, 0))//post thread msg
-    {
-        LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_31);
-        debug_print_syslog(0, "post tcp message failed,errno:%d\n",::GetLastError());
-        BufferUtility::getInstance()->releaseItem(data);
+	{
+		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_31);
+		debug_print_syslog(0, "post tcp message failed,errno:%d\n",::GetLastError());
+		BufferUtility::getInstance()->releaseItem(data);
 		return 1;
-    }
+	}
 	return 0;
 }
 
@@ -189,12 +189,12 @@ 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;
-        }
+		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;
 }
@@ -202,47 +202,48 @@ int sql_process_thread_init()
 // thread function
 unsigned __stdcall sql_process_entry(void *param)
 {
-     //printf("thread sql_process_entry start\n");
-	 LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_99);
-
-     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:
-				 	LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_84);
-                 char * pInfo = (char *)msg.wParam;
-                 //printf("current thread id: %d,recv %s\n", GetCurrentThreadId(), pInfo);
-                 //process sql command
-				 
-				 #ifndef  UT_TEST
-                 _exec_sql(pInfo);
-                 #else
-                 delete[] pInfo;
-				 #endif
-                 break;
-             }
-         }
-     };
-    return 0;
+	//printf("thread sql_process_entry start\n");
+	LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_99);
+
+	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:
+				LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_84);
+				char * pInfo = (char *)msg.wParam;
+				//printf("current thread id: %d,recv %s\n", GetCurrentThreadId(), pInfo);
+				//process sql command
+
+#ifndef  UT_TEST
+				_exec_sql(pInfo);
+				delete[] pInfo;
+#else
+				delete[] pInfo;
+#endif
+				break;
+			}
+		}
+	};
+	return 0;
 }
 int send_sql_message(char* pchr)
 {
-    LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_102);
-    //send thread message
+	LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_102);
+	//send thread message
 	unsigned thread_id = rand() % SQL_PROCESS_THREAD_NUM;
 	if(!PostThreadMessage(nSqlThreadID[thread_id], SQL_MSG, (WPARAM)pchr, 0))//post thread msg
-    {
-        LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_32);
-        debug_print_syslog(0, "post sql message failed,errno:%d\n", ::GetLastError());
-        delete[] pchr;
+	{
+		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_32);
+		debug_print_syslog(0, "post sql message failed,errno:%d\n", ::GetLastError());
+		delete[] pchr;
 		return 1;
-    }
+	}
 	return 0;
 }
 
@@ -251,12 +252,12 @@ 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;
-        }
+		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;
 }
@@ -264,42 +265,42 @@ int err_log_process_thread_init()
 // 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;
+	//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
+	//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
-    {
-        LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_33);
-        debug_print_syslog(0, "post err log message failed,errno:%d\n",::GetLastError());
-        delete[] pchr;
+	{
+		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_33);
+		debug_print_syslog(0, "post err log message failed,errno:%d\n",::GetLastError());
+		delete[] pchr;
 		return 1;
-    }
+	}
 	return 0;
 }
 
@@ -309,12 +310,12 @@ int sys_log_process_thread_init()
 	log_module_init();
 	for(i=0; i<SYS_LOG_PROCESS_THREAD_NUM; i++)
 	{
-	    hSysLogThread[i] = (HANDLE)_beginthreadex( NULL, 0, &sys_log_process_entry, NULL, 0, &(nSysLogThreadID[i]) );
-        if(hSysLogThread[i] == 0)
-        {
-            printf("start thread failed,errno:%d\n",::GetLastError());
-            return 1;
-        }
+		hSysLogThread[i] = (HANDLE)_beginthreadex( NULL, 0, &sys_log_process_entry, NULL, 0, &(nSysLogThreadID[i]) );
+		if(hSysLogThread[i] == 0)
+		{
+			printf("start thread failed,errno:%d\n",::GetLastError());
+			return 1;
+		}
 	}
 	return 0;
 }
@@ -322,31 +323,31 @@ int sys_log_process_thread_init()
 // thread function
 unsigned __stdcall sys_log_process_entry(void *param)
 {
-     //printf("thread sys 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 SYS_LOG_MSG:
-                     char * pInfo = (char *)msg.wParam;
-				 
-				     #ifndef  UT_TEST
-                     debug_print(0, pInfo);
-					 delete[] pInfo;
-                     #else
-                     delete[] pInfo;
-					 #endif
-                     break;
-             }
-         }
-     };
-    return 0;
+	//printf("thread sys 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 SYS_LOG_MSG:
+				char * pInfo = (char *)msg.wParam;
+
+#ifndef  UT_TEST
+				debug_print(0, pInfo);
+				delete[] pInfo;
+#else
+				delete[] pInfo;
+#endif
+				break;
+			}
+		}
+	};
+	return 0;
 }
 
 int service_task_init()
@@ -363,34 +364,30 @@ int service_task_init()
 
 int send_sys_log_message(char* pchr)
 {
-    //send thread message
-    if(!m_log_sys_status)
-    {
-        return 0;
-    }
+	//send thread message
+	if(!m_log_sys_status)
+	{
+		return 0;
+	}
 	unsigned thread_id = rand() % SYS_LOG_PROCESS_THREAD_NUM;
 	if(!PostThreadMessage(nSysLogThreadID[thread_id], SYS_LOG_MSG, (WPARAM)pchr, 0))//post thread msg
-    {
-        LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_34);
-        delete[] pchr;
+	{
+		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_34);
+		delete[] pchr;
 		return 1;
-    }
+	}
 	return 0;
 }
 
 int debug_print_syslog(UINT debugLevel, const char* format, ...)
 {
-    char *buffer = new char[LOG_BUF_SIZE];
+	char *buffer = new char[LOG_BUF_SIZE];
 	memset(buffer, 0, LOG_BUF_SIZE);
-    va_list args;
-    va_start (args, format);
-    vsnprintf_s(buffer, LOG_BUF_SIZE, LOG_BUF_SIZE-1, format, args);
-    va_end (args);
-	
+	va_list args;
+	va_start (args, format);
+	vsnprintf_s(buffer, LOG_BUF_SIZE, LOG_BUF_SIZE-1, format, args);
+	va_end (args);
+
 	send_sys_log_message(buffer);
 	return 0;
-}
-
-
-
-
+}

+ 1 - 1
QueueStrManager.h

@@ -4,7 +4,7 @@
 
 class QueueStrManager
 {
-	typedef DWORD (WINAPI *PFCALLBACK)(LPVOID lparam);
+	typedef DWORD (WINAPI *PFCALLBACK)(const char* lparam);
 private:
 	char* m_chr;
 	int m_couter;

+ 4 - 14
YAServerDlg.cpp

@@ -215,7 +215,8 @@ long giAllSqlCount=0;
 long giAllThreadCount=0;
 HANDLE hSemaHandle=NULL;
 
-DWORD WINAPI _exec_sql(LPVOID lparam)
+//DWORD WINAPI _exec_sql(LPVOID lparam)
+DWORD WINAPI _exec_sql(const char * lparam)
 {
 	LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_85);
 	::InterlockedIncrement(&g_QueueItemCount);
@@ -224,33 +225,22 @@ DWORD WINAPI _exec_sql(LPVOID lparam)
 		debug_print_syslog(0, "_exec_sql error: lparam is NULL");
 		return 1;
 	}
-	char *sql = (char*)lparam ;
 	CMysqlConn* pConn = NULL;
 	CDBConnGuard pDbGuard(pConn);
 	if(pConn == NULL)
 	{
 		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_86);
-		if(sql){
-			//delete[] lparam;
-			sql = NULL;
-		}
 		::InterlockedDecrement(&g_QueueItemCount);
 		return 1;
 	}
 	int err = 0;
 
-	pConn->MultiExecute(sql,err);
+	pConn->MultiExecute(lparam, err);
 
 	if(err > 0){
 		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_87);
 		debug_print_syslog(0, "sql error: %d", err);
-		debug_print_syslog(0, "sql: %s", sql);
-	}
-
-	if(sql){
-		LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_88);
-		//delete[] lparam;
-		sql = NULL;
+		debug_print_syslog(0, "sql: %s", lparam);
 	}
 
 	::InterlockedDecrement(&g_QueueItemCount);

+ 2 - 1
YAServerDlg.h

@@ -70,7 +70,8 @@ struct _SOCKET_BUFFER
 	}
 };
 
-extern DWORD WINAPI _exec_sql(LPVOID lparam);
+//extern DWORD WINAPI _exec_sql(LPVOID lparam);
+extern DWORD WINAPI _exec_sql(const char* lparam);
 extern DWORD WINAPI _write_error_log(LPVOID lparam);
 
 // CYAServerDlg ¶Ô»°¿ò