⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sgipapp.cpp

📁 联通的短信网关平台。 sp 使用。 如果想自己用vc 开发短信业务
💻 CPP
📖 第 1 页 / 共 3 页
字号:
							nTmpResult = SGIP_SUCCEED;
						}
						else
						{
							delete [] (char *)pBody;
							nTmpResult = SGIP_NODE_BUSY;
						}
						pthread_mutex_unlock(&m_ReportVec_mutex);

						SEQUENCE_NUM sSequence;
						memset(&sSequence, 0, sizeof(sSequence));
						memcpy(&sSequence, &pMsg->Head.sSeqNo, sizeof(sSequence));
						
						memset(cBuf, 0, sizeof(cBuf));
						SGIP_REPORTRESP_BODY stResp;
						int nBufLen = 0;
						m_pkg->DeComposePackage(SGIP_REPORT_RESP, (SGIP_MSG_BODY &)stResp, (char *)cBuf, &nBufLen);
						SGIP_REPORTRESP_MSG *pMsgResp = (SGIP_REPORTRESP_MSG *)cBuf;
						pMsgResp->Body.cResult = nTmpResult;
						memcpy(&pMsgResp->Head.sSeqNo, &sSequence, sizeof(sSequence));				

						SendToSMC(m_nSendSock, cBuf, nBufLen);
						break;
					}
					else
					{						
						printf("recv a msg, but no submit resp! %02x\n", iMsgType);
						break;
					}
				}
				else//recv error or time out---break;
				{
					printf("the %d times Submit recv response error!\n", nSubmitTimes);
					break;
				}
			}
		}
		m_pLog->add("...error!\n");
	}

	if (bSendFinish)
	{
		DisConnect(m_nSendSock);
		m_nSendSock = -1;
	}
	return END_FAILED;
}



bool CSGIPApp::HadTimeOut(time_t tBase, int nRequestTimeout)
{
	if ((time(NULL) - tBase) >= nRequestTimeout)
		return true;
	else
		return false;
}

void *GetAcceptProc(CSGIPApp *pSGIPApp)
{
	pthread_mutex_lock(&g_WGSrvCreate_mutex);
	if (g_bWingateServerCreate)
	{
		pthread_mutex_unlock(&g_WGSrvCreate_mutex);
		return NULL;
	}
	else
	{
		g_bWingateServerCreate = true;
		pthread_mutex_unlock(&g_WGSrvCreate_mutex);
	}
	
	while (!pSGIPApp->bTermitnate)
	{
		while (pSGIPApp->m_Listenfd == -1)
		{
			pSGIPApp->m_Listenfd = pSGIPApp->CreateServer(g_iLocalPort);
			usleep(10);
		}

		if (pSGIPApp->m_Listenfd != -1)
			printf("Create server LOCALHOST:%d sockfd=%d succeed!\n", \
					g_iLocalPort, pSGIPApp->m_Listenfd);

		while (true)
		{
			if (pSGIPApp->m_Listenfd == -1)
			{
				break;
			}

			pthread_mutex_lock(&pSGIPApp->m_AcceptSock_mutex);
			pSGIPApp->m_nAccSock = pSGIPApp->GetAccept(pSGIPApp->m_Listenfd);
			if (pSGIPApp->m_nAccSock == -1)
			{
				pthread_mutex_unlock(&pSGIPApp->m_AcceptSock_mutex);
				usleep(100);
				continue;
			}
			else
			{
				printf("accept a client %d\n", pSGIPApp->m_nAccSock);
				pthread_attr_t pAttr;
				pthread_attr_init(&pAttr);
				pthread_attr_setdetachstate(&pAttr,  PTHREAD_CREATE_DETACHED);
				pthread_t iThrRecv = 0;
				int ret = pthread_create(&iThrRecv, &pAttr, (void *(*) (void *))RecvThread, (void *)pSGIPApp);
				if(ret != 0)
				{
					pthread_mutex_unlock(&pSGIPApp->m_AcceptSock_mutex);
					printf("accept succeed but new thread error!\n");
					continue;
				}
				else
					printf("new Recv thread ID:%ld succeed!\n", iThrRecv);
			}
		}
	}

	g_bWingateServerCreate = false;
	printf("====Accept Proc exit!======\n");
	return NULL;
}

void *RecvThread(CSGIPApp *pSGIPApp)
{
	int nSockfd = -1;
	nSockfd = pSGIPApp->m_nAccSock;
	pthread_mutex_unlock(&pSGIPApp->m_AcceptSock_mutex);

	char cBuf[5120] = "";
	char cSendBuf[1024] = "";
	CSGIPPkg mPackage;


	memset(&cBuf, 0, sizeof(cBuf));
	int nRecvLen = sizeof(cBuf);

	int nReturn = -1;

	if ((nReturn = pSGIPApp->RecvFromSMC(nSockfd, (char *)cBuf, &nRecvLen, 60)) != END_FAILED)
	{
		
		//added by liaomch 2003-10-16
		//加入超时不发数据的处理
		if (nReturn == -2) //time out
		{
			printf("====invalid accept sockfd=%d[time out no recv], close!====\n", nSockfd);
			close(nSockfd);
			printf("Recv thread id:%ld exit!\n", pthread_self());
			return NULL;
		}
		//end added

		int iMsgType = -1;
		iMsgType = mPackage.GetPackageType((const char *)cBuf);
		if (iMsgType != SGIP_BIND) 
		{
			printf("recv thread get a pakcage: %02x\n", iMsgType);
			pSGIPApp->DisConnect(nSockfd);
			//close(nSockfd);
			printf("Recv thread id:%ld exit!\n", pthread_self());
			return NULL;
		}
		else
		{
			SGIP_BIND_MSG *pMsg = (SGIP_BIND_MSG *)cBuf;			
			
			if ((memcmp((const char *)&pMsg->Body.cLoginName, (const char *)g_cLUserName, strlen(pMsg->Body.cLoginName)) == 0) &&
				(memcmp((const char *)&pMsg->Body.cLoginPwd, (const char *)g_cLUserName, strlen(pMsg->Body.cLoginPwd)) == 0))
			{
				memset(cSendBuf, 0, sizeof(cSendBuf)); 
				SGIP_BINDRESP_BODY stResp;
				int nBufLen = 0;
				mPackage.DeComposePackage(SGIP_BIND_RESP, (SGIP_MSG_BODY &)stResp, (char *)cSendBuf, &nBufLen);
				SGIP_BINDRESP_MSG *pRespMsg = (SGIP_BINDRESP_MSG *)cSendBuf;
				memcpy(&pRespMsg->Head.sSeqNo, &pMsg->Head.sSeqNo, sizeof(pMsg->Head.sSeqNo));

				pRespMsg->Body.cResult = SGIP_SUCCEED;
				if ((pMsg->Body.cLoginType != 2) && (pMsg->Body.cLoginType != 1))
					pRespMsg->Body.cResult = SGIP_LOGIN_TYPE_ERR;

				if (!pSGIPApp->SendToSMC(nSockfd, cSendBuf, nBufLen))
				{
					close(nSockfd);
					printf("Send bind Response failed!\n"
						"Recv thread id:%ld exit!\n", pthread_self());
					return NULL;
				}
			}
			else
			{
				memset(cSendBuf, 0, sizeof(cSendBuf)); 
				SGIP_BINDRESP_BODY stResp;
				int nBufLen = 0;
				mPackage.DeComposePackage(SGIP_BIND_RESP, (SGIP_MSG_BODY &)stResp, (char *)cSendBuf, &nBufLen);
				SGIP_BINDRESP_MSG *pRespMsg = (SGIP_BINDRESP_MSG *)cSendBuf;
				memcpy(&pRespMsg->Head.sSeqNo, &pMsg->Head.sSeqNo, sizeof(pMsg->Head.sSeqNo));
				
				pRespMsg->Body.cResult = SGIP_LOGIN_INVALID;
				
				if (!pSGIPApp->SendToSMC(nSockfd, cSendBuf, nBufLen))
				{
					printf("Send bind Response failed!\n");
				}
				close(nSockfd);
				printf("Bind message validate failed!\n"
					"Recv thread id:%ld exit!\n", pthread_self());
				return NULL;
			}
		}
	}
	else//accept, recv a unknow package or timeout:
	{
		printf("====invalid accept sockfd=%d, close!====\n", nSockfd);
		close(nSockfd);
		printf("Recv thread id:%ld exit!\n", pthread_self());
		return NULL;		
	}

	while (true)
	{
		memset(&cBuf, 0, sizeof(cBuf));

		nRecvLen = sizeof(cBuf);

		if ((nReturn = pSGIPApp->RecvFromSMC(nSockfd, (char *)cBuf, &nRecvLen, 60)) != END_FAILED)
		{
			//added by liaomch 2003-10-16
			//加入超时不发数据的处理
			if (nReturn == -2) //time out
			{
				printf("====time out no recv package[sock=%d], close!====\n", nSockfd);
				pSGIPApp->DisConnect(nSockfd);
				printf("Recv thread id:%ld exit!\n", pthread_self());
				return NULL;
			}
			//end added

			int nMsgType = 0;
			nMsgType = mPackage.GetPackageType((const char *)cBuf);
			printf("Get a Package: msgtype = %02x\n", nMsgType);

			switch (nMsgType)
			{
			case SGIP_DELIVER:
				{
					printf("recv a deliver package!\n");

					SGIP_DELIVER_MSG sDeliverMsg;
					memset(&sDeliverMsg, 0, sizeof(sDeliverMsg));
					mPackage.ComposePackage(nMsgType, (SGIP_MSG &)sDeliverMsg, (const char *)cBuf);

					SGIP_DELIVER_PACKAGE *pDeliverBody = NULL;
					bool bFirst = true;
					while (pDeliverBody == NULL)
					{
						if (!bFirst)
						{
							printf("ATTENTION: Apply Memory error!\n");							
							usleep(10);
						}
						
						pDeliverBody = (SGIP_DELIVER_PACKAGE *)new char[sizeof(SGIP_DELIVER_PACKAGE)];
						memset(pDeliverBody, 0, sizeof(SGIP_DELIVER_PACKAGE));
						bFirst = false;
					}
					memcpy(&pDeliverBody->Body, &sDeliverMsg.Body, sizeof(SGIP_DELIVER_BODY));
					sprintf(pDeliverBody->cMsgid, "%u%010d%d",
							ntohl(sDeliverMsg.Head.sSeqNo.iSrcID),
							ntohl(sDeliverMsg.Head.sSeqNo.iDate),
							ntohl(sDeliverMsg.Head.sSeqNo.iSeqno)
						);

#ifdef __DEBUG_API__
					SGIP_DELIVER_MSG *pMsg = &sDeliverMsg;					
					printf("recv a ==DELIVER== package!\n");
					printf("\tiMsgLen=%d\n"
						"\tiMsgType=%02x\n"
						"\tiMsgid=%u%010d%d\n"
						"\tcMsgCoding=%d\n"
						"\tcUserNumber=%s\n"
						"\tcContent=%s\n",
						ntohl(pMsg->Head.iMsgLen),
						ntohl(pMsg->Head.iCmdID),
						ntohl(pMsg->Head.sSeqNo.iSrcID),
						ntohl(pMsg->Head.sSeqNo.iDate),
						ntohl(pMsg->Head.sSeqNo.iSeqno),
						pMsg->Body.cMessageCoding,
						pMsg->Body.cUserNumber,
						pMsg->Body.cMessageContent);
#endif //
					
					pthread_mutex_lock(&pSGIPApp->m_DeliverVec_mutex);
					int nTmpResult = -1;
					if ((pSGIPApp->m_pDeliver.size() < MAX_QUEUE_LEN) &&
						(pSGIPApp->m_pDeliver.size() <pSGIPApp->m_pDeliver.max_size()))
					{						
						pSGIPApp->m_pDeliver.push_back(pDeliverBody);
						nTmpResult = SGIP_SUCCEED;
					}
					else
					{
						delete [] (char *)pDeliverBody;
						nTmpResult = SGIP_NODE_BUSY;
					}
					
					pthread_mutex_unlock(&pSGIPApp->m_DeliverVec_mutex);

					SEQUENCE_NUM sSequence;
					memset(&sSequence, 0, sizeof(sSequence));
					memcpy(&sSequence, &sDeliverMsg.Head.sSeqNo, sizeof(sSequence));
					
					memset(cSendBuf, 0, sizeof(cSendBuf));
					SGIP_DELIVERRESP_BODY stResp;
					int nBufLen = 0;
					mPackage.DeComposePackage(SGIP_DELIVER_RESP, (SGIP_MSG_BODY &)stResp, (char *)cSendBuf, &nBufLen);
					
					SGIP_DELIVERRESP_MSG *pMsgResp = (SGIP_DELIVERRESP_MSG *)cSendBuf;
					
					*(cSendBuf+sizeof(SGIP_HEAD)) = nTmpResult;

					memcpy(&pMsgResp->Head.sSeqNo, &sSequence, sizeof(sSequence));
	
					if (!pSGIPApp->SendToSMC(nSockfd, cSendBuf, nBufLen))
					{
						printf("Send Deliver Response error!\n");
					}
					else
						printf("Send Deliver Response succeed!\n");
					
					break;
				}

			case SGIP_REPORT:
				{
					printf("recv a report package!\n");

					SGIP_REPORT_MSG sReportMsg;
					memset(&sReportMsg, 0, sizeof(sReportMsg));
					mPackage.ComposePackage(nMsgType, (SGIP_MSG &)sReportMsg, (const char *)cBuf);

					SGIP_REPORT_PACKAGE *pReportBody = NULL;	
					bool bFirst = true;
					while (pReportBody == NULL)
					{
						if (!bFirst)
						{
							printf("ATTENTION: Apply Memory error!\n");
							usleep(10);
						}
						
						pReportBody = (SGIP_REPORT_PACKAGE *)new char[sizeof(SGIP_REPORT_PACKAGE)];
						bFirst = false;		
					}					
					memcpy(&pReportBody->Body, &sReportMsg.Body, sizeof(SGIP_REPORT_BODY));
					sprintf(pReportBody->cMsgid, "%u%010d%d",
							ntohl(sReportMsg.Head.sSeqNo.iSrcID),
							ntohl(sReportMsg.Head.sSeqNo.iDate),
							ntohl(sReportMsg.Head.sSeqNo.iSeqno));

#ifdef __DEBUG_API__
					SGIP_REPORT_MSG *pMsg = &sReportMsg;					
					printf("recv a ==REPORT== package!\n");
					printf("\tiMsgLen=%d\n"
						"\tiMsgType=%02x\n"
						"\tiMsgid=%u%010d%d\n"
						"\tcState=%d\n"
						"\tcUserNumber=%s\n"
						"\tcErrorCode=%d\n",
						ntohl(pMsg->Head.iMsgLen),
						ntohl(pMsg->Head.iCmdID),
						ntohl(pMsg->Head.sSeqNo.iSrcID),
						ntohl(pMsg->Head.sSeqNo.iDate),
						ntohl(pMsg->Head.sSeqNo.iSeqno),
						pMsg->Body.cState,
						pMsg->Body.cUserNumber,
						pMsg->Body.ErrorCode);
#endif //

					pthread_mutex_lock(&pSGIPApp->m_ReportVec_mutex);
					int nTmpResult = -1;
					if ((pSGIPApp->m_pReport.size() < MAX_QUEUE_LEN) &&
						(pSGIPApp->m_pReport.size() <pSGIPApp->m_pReport.max_size()))
					{
						pSGIPApp->m_pReport.push_back(pReportBody);
						nTmpResult = SGIP_SUCCEED;
					}
					else
					{
						delete [] (char *)pReportBody;
						nTmpResult = SGIP_NODE_BUSY;
					}
					pthread_mutex_unlock(&pSGIPApp->m_ReportVec_mutex);

					SEQUENCE_NUM sSequence;
					memset(&sSequence, 0, sizeof(sSequence));
					memcpy(&sSequence, &sReportMsg.Head.sSeqNo, sizeof(sSequence));
															
					memset(cSendBuf, 0, sizeof(cSendBuf));
					SGIP_REPORTRESP_BODY stResp;
					int nBufLen = 0;
					mPackage.DeComposePackage(SGIP_REPORT_RESP, (SGIP_MSG_BODY &)stResp, (char *)cSendBuf, &nBufLen);
							
					SGIP_REPORTRESP_MSG *pMsgResp = (SGIP_REPORTRESP_MSG *)cSendBuf;
					//pMsgResp->Body.cResult = 0;
					*(cSendBuf + sizeof(SGIP_HEAD)) = nTmpResult;
					
					memcpy(&pMsgResp->Head.sSeqNo, &sSequence, sizeof(sSequence));
										
					if (!pSGIPApp->SendToSMC(nSockfd, cSendBuf, nBufLen))
						printf("Send Report Response Error!\n");
					else
						printf("Send Report Response succeed!\n");

					break;
				}

			case SGIP_SUBMIT:
				{
					//SGIP_SUBMIT_MSG *pMsg = (SGIP_SUBMIT_MSG *)cBuf;
					SGIP_SUBMIT_MSG sSubmitMsg;
					memset(&sSubmitMsg, 0, sizeof(sSubmitMsg));
					mPackage.ComposePackage(nMsgType,\
							(SGIP_MSG &)sSubmitMsg, (const char *)cBuf);
					SGIP_SUBMIT_MSG *pMsg = &sSubmitMsg;

#ifdef __DEBUG_API__
					printf("recv a ==SUBMIT== package!\n");
					printf("\tiMsgLen=%d\n"
						"\tiMsgType=%02x\n"
						"\tiMsgid=%u%010d%d\n"
						"\tcMsgType=%d\n"
						"\tcMsgLen=%d\n"
						"\tcMsgContent=%s\n"
						"\tcSpNumber=%s\n"
						"\tcUserNumber=%s\n",
						ntohl(pMsg->Head.iMsgLen),
						ntohl(pMsg->Head.iCmdID),
						ntohl(pMsg->Head.sSeqNo.iSrcID),
						ntohl(pMsg->Head.sSeqNo.iDate),
						ntohl(pMsg->Head.sSeqNo.iSeqno),
						pMsg->Body.cMessageType,
						ntohl(pMsg->Body.iMessageLength),
						pMsg->Body.cMessageContent,
						pMsg->Body.cSPNumber,
						pMsg->Body.cUserNumber
						);
#endif
					
					SEQUENCE_NUM sSequence;
					memset(&sSequence, 0, sizeof(sSequence));
					memcpy(&sSequence, &pMsg->Head.sSeqNo, sizeof(sSequence));
															
					memset(cSendBuf, 0, sizeof(cSendBuf));
					SGIP_SUBMITRESP_BODY stResp;
					int nBufLen = 0;
					mPackage.DeComposePackage(SGIP_SUBMIT_RESP, (SGIP_MSG_BODY &)stResp, (char *)cSendBuf, &nBufLen);
					SGIP_SUBMITRESP_MSG *pMsgResp = (SGIP_SUBMITRESP_MSG *)cSendBuf;
					*(cSendBuf + sizeof(SGIP_HEAD)) = SGIP_SUCCEED;
					memcpy(&pMsgResp->Head.sSeqNo, &sSequence, sizeof(sSequence));
					
					if (!pSGIPApp->SendToSMC(nSockfd, cSendBuf, nBufLen))
						printf("Send Submit Response Error!\n");

					break;
				}

			case SGIP_BIND:
				{
					//SGIP_BIND_MSG *pMsg = (SGIP_BIND_MSG *)cBuf;
					SGIP_BIND_MSG sBindMsg;
					memset(&sBindMsg, 0, sizeof(sBindMsg));
					mPackage.ComposePackage(nMsgType,\
						(SGIP_MSG &)sBindMsg, (const char *)cBuf);
					SGIP_BIND_MSG *pMsg = &sBindMsg;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -