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

📄 servicebroker.cpp

📁 基于dialogic语音卡的IVR系统源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	int iMyServiceID, iMyHostSN;
	iMyServiceID = atoi(aszMyServiceID);
	iMyHostSN = atoi(aszMyHostSN);
	sprintf(aszMyHostSN, "%04d%04d", iMyServiceID, iMyHostSN);
	memcpy(pHeader_Route->aszSource, aszMyHostSN, 8);
//	memcpy(pHeader_Route->aszSource + 4, aszMyHostSN, 4);
	////生成随机的包ID(暂时用DWORD累加实现)
	uiRandomID ++;
/*	因为在"050d"中没法使用LEN_STRUCT_HEADER_ROUTE_PACKETID,所以这句不能用
	sprintf(pHeader_Route->aszPacketID, "%050d", uiRandomID);*/
	sprintf(pHeader_Route->aszPacketID, "%d", uiRandomID);
	int iTempLen = LEN_STRUCT_HEADER_ROUTE_PACKETID - strlen(pHeader_Route->aszPacketID) - 1;	//所需0字符的个数
	memset(pHeader_Route->aszPacketID, '0', iTempLen);
	sprintf(pHeader_Route->aszPacketID + iTempLen, "%d", uiRandomID);
	//如果是ANSWERREQUEST,就使用WTREQUEST保存在CURRENTSTATE中的PACKETID()
	if(prequestParamList->iServiceType == iServiceType_TSD && prequestParamList->iServiceNo == iFuncNo_AnswerRequest)
	{
		memcpy(pHeader_Route->aszPacketID, prequestParamList->aszPacketID, LEN_PACKETID);
	}

	//形成通讯包头
	struct Header_Comm *pHeader_Comm = (struct Header_Comm *)malloc(sizeof(struct Header_Comm));
	pHeader_Comm->byteFlag = '1';	//'1':BIN包 '2':XML包 '3':WML
	pHeader_Comm->dwVerify = 0;
	pHeader_Comm->uiLength = sizeof(struct Header_Route) + iTableLen;

	//生成BUFFER
	len = sizeof(struct Header_Comm) + sizeof(struct Header_Route) + pHeader_Route->uiLength;
	*ppPacket = (char *)malloc(len);
	memcpy(*ppPacket, pHeader_Comm, sizeof(Header_Comm));
	memcpy(*ppPacket + sizeof(struct Header_Comm), pHeader_Route, sizeof(struct Header_Route));
	memcpy(*ppPacket + sizeof(struct Header_Comm) + sizeof(struct Header_Route), pTableData, pHeader_Route->uiLength);

	//添加请求的记录和设置超时时间(现被放到TCPTHREAD中)
//	iRequestWaitElapse = 1000 * GetPrivateProfileInt("ServiceInterface", "TimeOut", 0, CONFIGFILE);
//	int uiTimerID = SetTimer(NULL, TIMER_EVENT_TIMEOUT, iRequestWaitElapse, NULL);
//	m_requestQueue_App.SaveRequestInfo(prequestParamList, pHeader_Route->aszPacketID, uiTimerID);

	free(pHeader_Comm);
	free(pHeader_Route);
	free(pTableData);

	return len;
}

int PacketTable(struct ParamList *prequestParamList, char **ppTableData)
{
int len;

	char *pBuffer;
	CServiceDataQueue::DumpToBuffer(prequestParamList, &pBuffer, &len);

	*ppTableData = pBuffer;

	return len;
}

int TcpSend(struct ParamList *prequestParamList, char *pData, int iLength)
{
	struct TCPSendParam *ptcpSendParam = (struct TCPSendParam *)malloc(sizeof(struct TCPSendParam));
	ptcpSendParam->pData = pData;
	ptcpSendParam->iLen = iLength;
	ptcpSendParam->requestParamList = *prequestParamList;
	ptcpSendParam->iTimeOut = prequestParamList->iTimeOut;
	struct Header_Route *pheader_Route = (struct Header_Route *)(pData + sizeof(struct Header_Comm));
	memcpy(ptcpSendParam->aszPacketID, pheader_Route->aszPacketID, sizeof(pheader_Route->aszPacketID));

	PostThreadMessage(dwTCPThreadID, WM_SOCK_SENDREQUEST, (WPARAM)iLength, (LPARAM)ptcpSendParam);

	return 0;
}

int TcpSend2(char *pData, int iLength)
{
/******************************************************************
给SIMUIVR用于发送XML包
******************************************************************/

	struct TCPSendParam *ptcpSendParam = (struct TCPSendParam *)malloc(sizeof(struct TCPSendParam));
	ptcpSendParam->pData = pData;
	ptcpSendParam->iLen = iLength;
	memset(&(ptcpSendParam->requestParamList), 0, sizeof(ptcpSendParam->requestParamList));
	ptcpSendParam->iTimeOut = 0;
	memset(ptcpSendParam->aszPacketID, 0, sizeof(ptcpSendParam->aszPacketID));

	PostThreadMessage(dwTCPThreadID, WM_SOCK_SENDREQUEST, (WPARAM)iLength, (LPARAM)ptcpSendParam);

	return 0;
}

int ProcessRcvData(char *pRcvData, int len, void *pclientSock)
{
/**************************************************
功能:	处理TCP收到的数据(不包括9字节的包头)
**************************************************/
	if(bLog)
	{
		LogBuffer(aszPacketLogFile_Recv, pRcvData, len);
	}
	return UnpacketReply(pRcvData, len);
}

int UnpacketReply(char *pRcvData, int len)
{
/**************************************************
功能:	将TCP接收到的 应答包 解包为ReplyParamList
		pRcvData在SOCKET中释放,这里不管
**************************************************/
int iOffset_Table = sizeof(Header_Comm) + sizeof(Header_Route);
struct ParamList *preplyParamList;
struct Header_Route *pheader_Route;

	if(memcmp(pRcvData, "<?", 2) == 0)
	{//为SIMUIVR增加的代码,因为SIMUIVR会直接发送XML包给VRG
		struct ParamList *preplyParamList;
		CServiceDataQueue::CreateParamList(&preplyParamList, 0, 0, 0, 0);
		char *pRcvData_Copy;
		pRcvData_Copy = (char *)malloc(len);
		memcpy(pRcvData_Copy, pRcvData, len);
		CServiceDataQueue::AddParamListMember_Bin(preplyParamList, pRcvData_Copy, len);
		m_replyQueue.AddParamList(preplyParamList);
		return 0;
	}
	//生成REPLYPARAMLIST
	if(CServiceDataQueue::BirthFromBuffer(pRcvData + sizeof(struct Header_Route), len - sizeof(struct Header_Route), &preplyParamList) != 0)
	{
		LogBuffer2(pRcvData, len, "Birth Failed");
#ifdef IVR	//(在工程文件中预定义)是为了SIMUIVR的工程不会调用这句
		wprintf_err("Error:\tServiceBroker BirthFromBuffer() failed");
#endif
		return -1;
	}
	pheader_Route = (struct Header_Route *)pRcvData;

	//找到应答包中PACKETID对应的请求参数(channelno等)
	struct ParamList requestParamList;
	UINT uiTimerID;
	if(m_requestQueue_App.FindRequestInfo(pheader_Route->aszPacketID, requestParamList, uiTimerID) != 0)
	{
		LogBuffer2(pRcvData, len, "FindRequestInfo Failed");
#ifdef IVR	//(在工程文件中预定义)是为了SIMUIVR的工程不会调用这句
		wprintf_err("Error:\tServiceBroker FindRequestInfo() failed");
#endif
		return -1;
	}

	//填写应答参数的通道信息
	preplyParamList->bIsIVR = requestParamList.bIsIVR;
	preplyParamList->iChannelNo = requestParamList.iChannelNo;
	preplyParamList->iServiceNo = requestParamList.iServiceNo;
	preplyParamList->iServiceType = requestParamList.iServiceType;
	preplyParamList->ISN = requestParamList.ISN;
	//设置请求/应答类型
	preplyParamList->bIsReply = TRUE;
	//保存包ID
	memcpy(preplyParamList->aszPacketID, pheader_Route->aszPacketID, sizeof(preplyParamList->aszPacketID));

	//释放TIMER
	KillTimer(NULL, uiTimerID);

	//将应答放到应答队列
	m_replyQueue.AddParamList(preplyParamList);

	return 0;
}

UINT TCPThread(LPVOID pParam)
{
struct TcpThreadParam *ptcpThreadParam = (struct TcpThreadParam *)pParam;

	dwTCPThreadID = GetCurrentThreadId();

	//是否写发送/接收包的日志(包括9字节的包头)
	bLog = (BOOL)GetPrivateProfileInt("ServiceInterface", "Log", 0, CONFIGFILE);

	if(AfxSocketInit() == FALSE)
	{
		AfxMessageBox("Socket initialize failed");
		return 100;
	}

HERE:
	CClientSock clientSock(ProcessRcvData);
	clientSock.Create();

	//读取代理服务器的信息
	char aszProxyAddr[100] = "\0";
	GetPrivateProfileString(ptcpThreadParam->aszServerDescription, "Address", "abc", aszProxyAddr, sizeof(aszProxyAddr), CONFIGFILE);
	if(aszProxyAddr[0] == 0)
	{
		AfxMessageBox("read setup.ini error");
		return 1;
	}
	int iProxyPort = GetPrivateProfileInt("ServiceProxy", "Port", 0, ".\\Setup.ini");
	if(iProxyPort == 0)
	{
		AfxMessageBox("read setup.ini error");
		return 2;
	}

	//连接代理服务器
	int iConnectErrorCount = 0;
	while(1)
	{
		if(clientSock.Connect(aszProxyAddr, iProxyPort) == FALSE)
		{
			iConnectErrorCount ++;
			if(iConnectErrorCount < 0)//xyw ask me to do so
			{
				AfxMessageBox("can't connect to service proxy");
				return 3;
			}
			isConnect = 0;
			Sleep(500);
		}
		else
		{
			isConnect = 1;
			break;
		}
	}

	//消息循环
    MSG msg;      
    while (GetMessage(&msg, NULL, 0, 0))
	{
		if(msg.hwnd != NULL)
		{
			TranslateMessage(&msg);    /* Translates virtual key codes           */         
			DispatchMessage(&msg);     /* Dispatches message to window           */    
		}
		else
		{
			switch(msg.message)
			{
				case WM_SOCK_SENDREQUEST:
					struct TCPSendParam *ptcpSendParam;
					ptcpSendParam = (struct TCPSendParam *)(msg.lParam);
					char *pSendData;
					int len;
					pSendData = (char *)(ptcpSendParam->pData);
					len = (int)(ptcpSendParam->iLen);
					int iSentDataLen;
					iSentDataLen = clientSock.Send(pSendData, len);
					if(iSentDataLen == SOCKET_ERROR || iSentDataLen != len)
					{
//						AfxMessageBox("iSentDataLen!=len");
					}
					//写发送包日志
					if(bLog)
					{
						LogBuffer(aszPacketLogFile_Send, pSendData, len);
					}
					free(pSendData);

					//添加请求的记录和设置超时时间
					if(ptcpThreadParam->bSetTimer)
					{
						int uiTimerID2;
						uiTimerID2 = 0;
						if(iRequestWaitElapse != 0)
						{
							if(ptcpSendParam->iTimeOut == 0)
							{
								iRequestWaitElapse = 1000 * GetPrivateProfileInt("ServiceInterface", "TimeOut", 0, CONFIGFILE);
							}
							else
							{
								iRequestWaitElapse = 1000 * ptcpSendParam->iTimeOut;
							}
							uiTimerID2 = SetTimer(NULL, TIMER_EVENT_TIMEOUT, iRequestWaitElapse, NULL);
						}
						else
						{
							uiTimerID2 = 0;
						}
						m_requestQueue_App.SaveRequestInfo(&(ptcpSendParam->requestParamList), ptcpSendParam->aszPacketID, uiTimerID2);
					}

					//释放参数
					free(ptcpSendParam);
					break;

				case WM_SOCK_CLOSED:
					goto HERE;
					break;

				case WM_TIMER:	//某个请求超时,填写系统繁忙的应答
					//找到应答包中PACKETID对应的请求参数(channelno等)
					struct ParamList requestParamList;
					UINT uiTimerID;
					uiTimerID = msg.wParam;
					if(m_requestQueue_App.FindRequestInfo_ByTimerID(uiTimerID, requestParamList) != 0)
					{
						break;
					}

					//填写应答参数的通道信息
					struct ParamList *preplyParamList;
					CServiceDataQueue::CreateParamList(&preplyParamList, 0, 0, 0, 0);
					preplyParamList->bIsIVR = requestParamList.bIsIVR;
					preplyParamList->iChannelNo = requestParamList.iChannelNo;
					preplyParamList->iServiceNo = requestParamList.iServiceNo;
					preplyParamList->iServiceType = requestParamList.iServiceType;
					preplyParamList->ISN = requestParamList.ISN;

					//释放TIMER
					if(uiTimerID != 0)
					{
						KillTimer(NULL, uiTimerID);
					}

					//将“系统繁忙”应答放到应答队列
					CServiceDataQueue::AddParamListMember_Integer(preplyParamList, -1);
					m_replyQueue.AddParamList(preplyParamList);

					break;

				default:
					break;
			}
		}
	}//end of while(getmessage())

	//释放线程参数
	free(ptcpThreadParam);

	return 0;
}
/*
UINT FaxRequestThread(LPVOID pParam)
{
	dwTCPThreadID = GetCurrentThreadId();

	if(AfxSocketInit() == FALSE)
	{
		AfxMessageBox("Socket initialize failed");
		return 100;
	}

	CServerSock serverSock;
	serverSock.Create(4000);
	serverSock.Listen();

	//消息循环
    MSG msg;      
    while (GetMessage(&msg, NULL, 0, 0))
	{
		if(msg.hwnd != NULL)
		{
			TranslateMessage(&msg);    // Translates virtual key codes
			DispatchMessage(&msg);     // Dispatches message to window    
		}
		else
		{
			switch(msg.message)
			{
				case WM_SOCK_SENDREQUEST:
					struct TCPSendParam *ptcpSendParam;
					ptcpSendParam = (struct TCPSendParam *)(msg.lParam);
					char *pSendData;
					int len;
					pSendData = (char *)(ptcpSendParam->pData);
					len = (int)(ptcpSendParam->iLen);
					int iSentDataLen;
					iSentDataLen = clientSock.Send(pSendData, len);
					if(iSentDataLen == SOCKET_ERROR || iSentDataLen != len)
					{
						AfxMessageBox("iSentDataLen!=len");
					}
					free(pSendData);

					//添加请求的记录和设置超时时间
					int uiTimerID2;
					iRequestWaitElapse = 1000 * GetPrivateProfileInt("ServiceInterface", "TimeOut", 0, CONFIGFILE);
					if(iRequestWaitElapse != 0)
					{
						uiTimerID2 = SetTimer(NULL, TIMER_EVENT_TIMEOUT, iRequestWaitElapse, NULL);
					}
					else
					{
						uiTimerID2 = 0;
					}
					m_requestQueue_App.SaveRequestInfo(ptcpSendParam->prequestParamList, ptcpSendParam->aszPacketID, uiTimerID2);

					//释放参数
					free(ptcpSendParam);

					break;

				default:
					break;
			}//end of switch
		}
	}//end of while

	return 0;
}
*/

⌨️ 快捷键说明

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