Ejemplo n.º 1
0
UINT CSmsTraffic::SmThread(LPVOID lParam)
{

	CSmsTraffic* p=(CSmsTraffic *)lParam;	// this
    char *pPort = (char *)malloc(20);
    strcpy(pPort,p->m_pComport);
	int nMsg;				// 收到短消息条数
	int nDelete;			// 目前正在删除的短消息编号
	SM_BUFF buff;			// 接收短消息列表的缓冲区
	SM_PARAM param[256];	// 发送/接收短消息缓冲区
	CTime tmOrg, tmNow;		// 上次和现在的时间,计算超时用
	enum {
		stBeginRest,				// 开始休息/延时
		stContinueRest,				// 继续休息/延时
		stSendMessageRequest,		// 发送短消息
		stSendMessageResponse,		// 读取短消息列表到缓冲区
		stSendMessageWaitIdle,		// 发送不成功,等待GSM就绪
		stReadMessageRequest,		// 发送读取短消息列表的命令
		stReadMessageResponse,		// 读取短消息列表到缓冲区
		stDeleteMessageRequest,		// 删除短消息
		stDeleteMessageResponse,	// 删除短消息
		stDeleteMessageWaitIdle,	// 删除不成功,等待GSM就绪
		stExitThread				// 退出
	} nState;				// 处理过程的状态

	// 初始状态
	nState = stBeginRest;

	// 发送和接收处理的状态循环
	while (nState != stExitThread)
	{
		switch(nState)
		{
			case stBeginRest:
                //info(_TEXT("短信机:"),pPort,_TEXT(" 准备就绪..."));
				tmOrg = CTime::GetCurrentTime();
				nState = stContinueRest;
				break;

			case stContinueRest:
				Sleep(300);
				tmNow = CTime::GetCurrentTime();
				if (p->GetSendMessage(&param[0]))
				{
					nState = stSendMessageRequest;	// 有待发短消息,就不休息了
                    info(_TEXT("短信机:"),pPort,_TEXT(" 检测到有待发短信..."));
				}
				else if (tmNow - tmOrg >= 5)		// 待发短消息队列空,休息5毫秒
				{
					nState = stReadMessageRequest;	// 转到读取短消息状态
				}
				break;
                
			case stSendMessageRequest:
				gsmSendMessage(pPort,&param[0]);
				info(_TEXT("短信机:"),pPort,_TEXT(" 向号码:"),param[0].TPA,_TEXT(" 发送消息:"),param[0].TP_UD,"...");
				memset(&buff, 0, sizeof(buff));
				tmOrg = CTime::GetCurrentTime();
				nState = stSendMessageResponse;
                p->m_sendTimes++;
				break;

			case stSendMessageResponse:
				Sleep(10000);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(pPort,&buff))
				{
					case GSM_OK: 
                        setStatus(param[0].index,ssSendSuccess,true);
                        info(_TEXT("短信机:"),pPort,_TEXT(" 向号码:"),param[0].TPA,_TEXT(" 发送消息:"),param[0].TP_UD,"成功!");
                        p->m_sendTimes = 0;
                        p->m_sendFailedSms = 0;
						nState = stBeginRest;
						break;
					case GSM_ERR:
						nState = stSendMessageWaitIdle;
                        if(p->m_sendTimes >= SEND_TIMES_FAILED)
                        {
                            setStatus(param[0].index,ssWaitToSend,true);
                            info(_TEXT("短信机:"),pPort,_TEXT(" 向号码:"),param[0].TPA,_TEXT(" 发送消息:"),param[0].TP_UD,"失败!");
                            p->m_sendTimes = 0;
                            p->m_sendFailedSms++;
                            nState = stBeginRest;
                        }
						break;
					default:
						nState = stSendMessageWaitIdle;
						if(p->m_sendTimes >= SEND_TIMES_FAILED)
						{
							info(_TEXT("短信机:"),pPort,_TEXT(" 向号码:"),param[0].TPA,_TEXT(" 发送消息:"),param[0].TP_UD,"失败!");
							setStatus(param[0].index,ssWaitToSend,true);
							p->m_sendTimes = 0;
							p->m_sendFailedSms++;
							if(p->m_sendFailedSms >= SEND_SMS_FAILED)
							{
								info(_TEXT("短信机:"),pPort,"连续发送失败次数超标,疑似欠费,请确认!");
								
								
							}
							nState = stBeginRest;
						}

				}
				break;

			case stSendMessageWaitIdle:
				Sleep(500);
				nState = stSendMessageRequest;		
				break;

			case stReadMessageRequest:
				gsmReadMessageList(pPort);
				memset(&buff, 0, sizeof(buff));
				tmOrg = CTime::GetCurrentTime();
				nState = stReadMessageResponse;
				break;

			case stReadMessageResponse:
				Sleep(100);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(pPort,&buff))
				{
					case GSM_OK: 
						nMsg = gsmParseMessageList(param, &buff);
						if (nMsg > 0)
						{
							p->PutRecvMessage(param, nMsg);
                            for(int i=0;i<nMsg;i++)
                            {
                                insertRecvSms(&param[i]);
                                info(_TEXT("短信机:"),pPort,_TEXT(" 接收到消息:"),param[0].TP_UD);
                            }
							nDelete = 0;
							nState = stDeleteMessageRequest;
						}
						else
						{
							nState = stBeginRest;
						}
                        p->m_readFailedTimes = 0;
						break;
					case GSM_ERR:
                        p->m_readFailedTimes++;
                        if(p->m_readFailedTimes >= READ_TIMES_FAILED)
                        {
                            info(_TEXT("短信机:"),pPort,"连续收取短信失败次数超标,疑似欠费,请确认!");
                            
					    }
						nState = stBeginRest;
						break;
					default:
						if (tmNow - tmOrg >= 15)		// 15秒超时
						{
							nState = stBeginRest;
						}
				}
				break;

			case stDeleteMessageRequest:
				if (nDelete < nMsg)
				{
					gsmDeleteMessage(pPort,param[nDelete].index);
                    //info(_TEXT("短信机:"),pPort,_TEXT("请求清理收件箱"));
					memset(&buff, 0, sizeof(buff));
					tmOrg = CTime::GetCurrentTime();
					nState = stDeleteMessageResponse;
				}
				else
				{
					nState = stBeginRest;
				}
				break;

			case stDeleteMessageResponse:
				Sleep(100);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(pPort,&buff))
				{
					case GSM_OK: 
                        info(_TEXT("短信机:"),pPort,_TEXT("从收件箱删除1条短信"));
						nDelete++;
						nState = stDeleteMessageRequest;
						break;
					case GSM_ERR:
                        info(_TEXT("短信机:"),pPort,_TEXT("删除短信失败"));
						nState = stDeleteMessageWaitIdle;
						break;
					default:
						if (tmNow - tmOrg >= 5)		// 5秒超时
						{
//							TRACE("  Timeout!\n");
							nState = stBeginRest;
						}
				}
				break;

			case stDeleteMessageWaitIdle:
				Sleep(500);
				nState = stDeleteMessageRequest;		// 直到删除成功为止
				break;
		}

		// 检测是否有关闭本线程的信号
		DWORD dwEvent = WaitForSingleObject(p->m_hKillThreadEvent, 20);
		if (dwEvent == WAIT_OBJECT_0)  nState = stExitThread;
	}

	// 置该线程结束标志
	SetEvent(p->m_hThreadKilledEvent);
    free(pPort);
    pPort = NULL;
	return 9999;
}
Ejemplo n.º 2
0
UINT CSmsTraffic::SmThread(LPVOID lParam)
{
	CSmsTraffic* p=(CSmsTraffic *)lParam;	// this
	int nMsg;				// 收到短消息条数
	int nDelete;			// 目前正在删除的短消息编号
	SM_BUFF buff;			// 接收短消息列表的缓冲区
	SM_PARAM param[256];	// 发送/接收短消息缓冲区
	CTime tmOrg, tmNow;		// 上次和现在的时间,计算超时用
	enum {
		stBeginRest,				// 开始休息/延时
		stContinueRest,				// 继续休息/延时
		stSendMessageRequest,		// 发送短消息
		stSendMessageResponse,		// 读取短消息列表到缓冲区
		stSendMessageWaitIdle,		// 发送不成功,等待GSM就绪
		stReadMessageRequest,		// 发送读取短消息列表的命令
		stReadMessageResponse,		// 读取短消息列表到缓冲区
		stDeleteMessageRequest,		// 删除短消息
		stDeleteMessageResponse,	// 删除短消息
		stDeleteMessageWaitIdle,	// 删除不成功,等待GSM就绪
		stExitThread				// 退出
	} nState;				// 处理过程的状态

	// 初始状态
	nState = stBeginRest;

	// 发送和接收处理的状态循环
	while (nState != stExitThread)
	{
		switch(nState)
		{
			case stBeginRest:
//				TRACE("State=stBeginRest\n");
				tmOrg = CTime::GetCurrentTime();
				nState = stContinueRest;
				break;

			case stContinueRest:
//				TRACE("State=stContinueRest\n");
				Sleep(300);
				tmNow = CTime::GetCurrentTime();
				if (p->GetSendMessage(&param[0]))
				{
					nState = stSendMessageRequest;	// 有待发短消息,就不休息了
				}
				else if (tmNow - tmOrg >= 5)		// 待发短消息队列空,休息5秒
				{
					nState = stReadMessageRequest;	// 转到读取短消息状态
				}
				break;

			case stSendMessageRequest:
//				TRACE("State=stSendMessageRequest\n");
				gsmSendMessage(&param[0]);
				memset(&buff, 0x00, sizeof(buff));
				tmOrg = CTime::GetCurrentTime();
				nState = stSendMessageResponse;
				break;

			case stSendMessageResponse:
//				TRACE("State=stSendMessageResponse\n");
				Sleep(100);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(&buff))
				{
					case GSM_OK: 
//						TRACE("  GSM_OK %d\n", tmNow - tmOrg);
						nState = stBeginRest;
						break;
					case GSM_ERR:
//						TRACE("  GSM_ERR %d\n", tmNow - tmOrg);
						nState = stSendMessageWaitIdle;
						break;
					default:
//						TRACE("  GSM_WAIT %d\n", tmNow - tmOrg);
						if (tmNow - tmOrg >= 10)		// 10秒超时
						{
//							TRACE("  Timeout!\n");
							nState = stSendMessageWaitIdle;
						}
				}
				break;

			case stSendMessageWaitIdle:
				Sleep(500);
				nState = stSendMessageRequest;		// 直到发送成功为止
				break;

			case stReadMessageRequest:
//				TRACE("State=stReadMessageRequest\n");
				gsmReadMessageList(4);
				memset(&buff, 0, sizeof(buff));
				tmOrg = CTime::GetCurrentTime();
				nState = stReadMessageResponse;
				break;

			case stReadMessageResponse:
//				TRACE("State=stReadMessageResponse\n");
				Sleep(100);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(&buff))
				{
					case GSM_OK: 
//						TRACE("  GSM_OK %d\n", tmNow - tmOrg);
						nMsg = gsmParseMessageList(param, &buff);
						if (nMsg > 0)
						{
							p->PutRecvMessage(param, nMsg);
							nDelete = 0;
							nState = stDeleteMessageRequest;
						}
						else
						{
							nState = stBeginRest;
						}
						break;
					case GSM_ERR:
//						TRACE("  GSM_ERR %d\n", tmNow - tmOrg);
						nState = stBeginRest;
						break;
					default:
//						TRACE("  GSM_WAIT %d\n", tmNow - tmOrg);
						if (tmNow - tmOrg >= 15)		// 15秒超时
						{
//							TRACE("  Timeout!\n");
							nState = stBeginRest;
						}
				}
				break;

			case stDeleteMessageRequest:
//				TRACE("State=stDeleteMessageRequest\n");
				if (nDelete < nMsg)
				{
					gsmDeleteMessage(param[nDelete].index);
					memset(&buff, 0, sizeof(buff));
					tmOrg = CTime::GetCurrentTime();
					nState = stDeleteMessageResponse;
				}
				else
				{
					nState = stBeginRest;
				}
				break;

			case stDeleteMessageResponse:
//				TRACE("State=stDeleteMessageResponse\n");
				Sleep(100);
				tmNow = CTime::GetCurrentTime();
				switch (gsmGetResponse(&buff))
				{
					case GSM_OK: 
//						TRACE("  GSM_OK %d\n", tmNow - tmOrg);
						nDelete++;
						nState = stDeleteMessageRequest;
						break;
					case GSM_ERR:
//						TRACE("  GSM_ERR %d\n", tmNow - tmOrg);
						nState = stDeleteMessageWaitIdle;
						break;
					default:
//						TRACE("  GSM_WAIT %d\n", tmNow - tmOrg);
						if (tmNow - tmOrg >= 5)		// 5秒超时
						{
//							TRACE("  Timeout!\n");
							nState = stBeginRest;
						}
				}
				break;

			case stDeleteMessageWaitIdle:
//				TRACE("State=stDeleteMessageWaitIdle\n");
				Sleep(500);
				nState = stDeleteMessageRequest;		// 直到删除成功为止
				break;
		}

		// 检测是否有关闭本线程的信号
		DWORD dwEvent = WaitForSingleObject(p->m_hKillThreadEvent, 20);
		if (dwEvent == WAIT_OBJECT_0)  nState = stExitThread;
	}

	// 置该线程结束标志
	SetEvent(p->m_hThreadKilledEvent);

	return 9999;
}