// 线程回调
int CDealCMDThread::Run()
{
	int		nByteRecv	= 0;
	int		nSelectRes  = 0;
	fd_set	readfds;		
	struct	timeval tv;
	NM_CMD_S emCMD;
	
	while(!m_bSafeExit && m_hSocketClient != INVALID_SOCKET)
	{
		try
		{
			m_bThreadRolling = TRUE;
			tv.tv_sec  = 3;
			tv.tv_usec = 0;
			
			FD_ZERO(&readfds);
			FD_SET(m_hSocketClient, &readfds);
			
			nSelectRes = select(m_hSocketClient + 1, &readfds, NULL, NULL, &tv);
			if(nSelectRes == -1)
			{
				m_bThreadRolling = FALSE;
//				DW("Receive CMD Select Failed.nSelectRes = -1");
				::PostMessage(m_hWndMsg,WM_CONNECT_CLIENT,(WPARAM)FALSE,0);				
				break;
			}
			else if (nSelectRes == 0)
			{
				m_bThreadRolling = FALSE;
				continue;
			}
			else
			{
				if(FD_ISSET(m_hSocketClient, &readfds))
				{
					ZeroMemory(&emCMD,sizeof(NM_CMD_S));
					// TCP方式接收数据,由于TCP粘包,所以对接收到的数据要进行分析,然后进行投递给解码显示模块
					nByteRecv = recv(m_hSocketClient, (char *)&emCMD, sizeof(NM_CMD_S), 0);
					if(nByteRecv <= 0)
					{
						m_bThreadRolling = FALSE;
//						DW("Receive CMD Select Failed. nByteRecv = %d",nByteRecv);
						::PostMessage(m_hWndMsg,WM_CONNECT_CLIENT,(WPARAM)FALSE,0);	
						break;
					}

					ParseCMD(emCMD);
				}

				m_bThreadRolling = FALSE;
			} 
		}
		catch(...)
		{
			m_bThreadRolling = FALSE;
//			DW("Catch Receive CMD Select Failed.");
			::PostMessage(m_hWndMsg,WM_CONNECT_CLIENT,(WPARAM)FALSE,0);	
		}
	}

	m_bThreadRolling = FALSE;

	return 0;
}
Exemple #2
0
/*
*****************************************************************************
** FUNCTION NAME: main
**
** FUNCTION INPUTS:
**  @int argc:
**  @char *argv[]:
**
** FUNCTION DESCRIPTION
**   I have nothing to say, just enjoy yourself.
**
** FUNCTION OUTPUTS:
**   Resturns XXXXX
**
** HISTORY:
** 2008-7-3	Steven Leong	Created
*****************************************************************************
*/
int main(int argc, char *argv[])
{
	char *ptrXml = NULL;
	int nRet, i;
	char *ptrObjID = NULL;
	char *ptrCdid = NULL;
	char *ptrCategory = NULL;
	char *ptrAlbumTitle = NULL;
	TOC_INFO tocInfo;
	CDDB_INFO cddbInfo;

	memset(&tocInfo, 0, sizeof(TOC_INFO));
	memset(&cddbInfo, 0, sizeof(CDDB_INFO));

	/* Register signals */
#ifndef __ARM__
	signal(SIGINT, catchKill); /* ctrl+c */
#endif
	signal(SIGHUP, catchKill); /* zqdevice do it */
	signal(SIGKILL, catchKill);
	signal(SIGTERM, catchKill);
	signal(SIGQUIT, catchKill);

	/* Read command from stdin */
	if(ZRipGetCMD(&ptrXml) != ZAPP_SUCCESS){
		dprintf("Cannot get command.\n");
		return -1;
	}

	/* parse command */
	nRet = ParseCMD(ptrXml, &ptrObjID, &ptrCdid, &ptrCategory);
	if(nRet != ZAPP_SUCCESS){
		dprintf("Parse command failed.\n");
		/* return invalid xml */
		ZRipResponseError(ZRIP, 1);
		//NOTE:Free ptrXml ASAP
		ZFREE(ptrXml);
		goto appExit;
	}
	//NOTE:Free ptrXml ASAP
	ZFREE(ptrXml);

	/* The policy like GetCDDB. We will get CDDB from LocalDB or FreeDB first */
	
	if(ptrObjID == NULL || ptrCdid == NULL || ptrCategory == NULL){
		/* No enough parameters. */
		ZRipResponseError(ZRIP, 4);
		goto appExit;
	}

	/* Now, let's get the cdid from zDB */
	/* Read TOC from zDB */
	if(ZDBGetTOCInfoByObjID(ptrObjID, &tocInfo)!=ZAPP_SUCCESS){
		ZRipResponseError(UPDATECDDB, 1);
		goto appExit;
	}

	/* Everything is readly now, Do it!!! */

	/* NOTE: we should use the cdid from CMD. Not in the zDB.
	Because we may update inexact CDDB. Not the exact one. */

	cddbInfo.uCDID = strtoul(ptrCdid, NULL, 10);
	cddbInfo.category = strdup(ptrCategory);
	cddbInfo.uCount = 0;
	cddbInfo.trackMeta = NULL;

	dprintf("********* Useful Info *********\n");
	dprintf("objID -> %s\n", ptrObjID);
	dprintf("CDID -> %u\n", cddbInfo.uCDID);
	dprintf("category -> %s\n", ptrCategory);
	dprintf("*******************************\n");

	/* If the category matched the localDB flag format "LOCALDB#albumTitle",
	We will get CDDB from LocalCDDB, else from FreeDB. then Update metadata
	to zDB */
	if(!strncmp(ptrCategory, LOCALDB_FLAG"#", strlen(LOCALDB_FLAG)+1)){
		/* Matched the localDB flag, get CDDB from localDB */
		dprintf("LocalDB...\n");
		ptrAlbumTitle = strdup(&ptrCategory[strlen(LOCALDB_FLAG)+1]);
		nRet = LocalDBGetCDDB(&cddbInfo, ptrAlbumTitle);
		if(nRet != ZAPP_SUCCESS){
			ZRipResponseError(UPDATECDDB, 4);
			goto appExit;
		}
	}
	else {
		/* Get CDDB from freeDB */
		nRet = FreeDBGetCDDB(&cddbInfo);
		if(nRet!= ZAPP_SUCCESS){
			/* get CDDB from freeDB failed. */
			switch(nRet){
				case ZAPP_NETWORK:
					ZRipResponseError(UPDATECDDB, 2);
					goto appExit;
				case ZAPP_TIMEOUT:
					ZRipResponseError(UPDATECDDB, 3);
					goto appExit;
			}
		}
	}

	/* Update the metadata and get new objID */
	ZFREE(ptrObjID);
	nRet = UpdateCDDB(&tocInfo, &cddbInfo, &ptrObjID);
	if(nRet != ZAPP_SUCCESS){
		ZRipResponseError(UPDATECDDB, 5);
		goto appExit;
	}

	dprintf("Response now...\n");
	ResponseCMD(ptrObjID);

appExit:
	ZFREE(tocInfo.ptrFrames);
	ZFREE(ptrObjID);
	ZFREE(ptrCategory);
	ZFREE(ptrAlbumTitle);
	/* free cddbInfo */
	ZFREE(cddbInfo.category);
	for(i=0;i<cddbInfo.uCount;i++){
		freeMetaTrack(cddbInfo.trackMeta[i]);
		ZFREE(cddbInfo.trackMeta[i]);
	}
	ZFREE(cddbInfo.trackMeta);
	return 0;
}