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

📄 func.cpp

📁 (R)-VC++-Code-cmpp企业短信助理-源代码全
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		GetLastday(buffer+i);
		PutNByte(buffer+i,4,atoi(arg.ESMEID));//station_id
		PutNByte(buffer+i+4,4,id_time);//msg_id
		i+=8;
		buffer[i]=2;
		i++;
		Jstrncpy(buffer+i,sm,10);
		i+=10;
		i+=8;//8 bytes to reserve
		break;
	case ALIVE://over
		break;
	case ALIVERESP://over
		buffer[i]=0;
		i++;
		break;
	}
	PutNByte(buffer,4,i);
	if(arg.isDebug)
	AddBinLog(buffer,i);
	return i;
}

DWORD WINAPI ReceiverMsg(LPVOID pp)
{
	int i,j,AA;
	WORD wAlive=0;
	fd_set rfds,afds;
	int binderror=0;
	DWORD dwRet=0;
	char strSQL[400];
	char msg[256];
	char buffer[SMPPMAXLENGTH];
	_ConnectionPtr pCnnGSM=NULL;
	timeval tv;
	tv.tv_sec=20;
	tv.tv_usec=0;
	FD_ZERO(&afds);
	FD_SET(arg.s,&afds);
	try
	{
		::CoInitialize(NULL);
		TESTHR(pCnnGSM.CreateInstance(__uuidof(Connection)));
		pCnnGSM->ConnectionTimeout = 120;
		pCnnGSM->CommandTimeout=300;
		TESTHR(pCnnGSM->Open(arg.strCnn,"","",adConnectUnspecified));
	}//try
	catch (_com_error &e)
	{
		AddLog("数据库连接失败。\n");			
		AddLog("失败原因: '%s'\n", (char*) e.Description());			
		if(pCnnGSM->GetState())pCnnGSM->Close();
		::CoUninitialize();
		SleepEx(1000,1);
		return 1;
	}
	i=InitMsg(BIND,buffer,0,0);
	if(send(arg.s,buffer,i,0)<i)
	{
		return ExitFromNetClose;
	}
	try{
	while(1)
	{
		memcpy(&rfds,&afds,sizeof(afds));
		i=select(FD_SETSIZE,&rfds,(fd_set*)0,(fd_set*)0,
			(struct timeval*)&tv);
		switch(i)
		{
			case SOCKET_ERROR:	//error:
				AddLog("select error for %d\n",GetLastError());
				ExitThread(ExitFromNetClose);
				break;
			case 0:	//timeout:connection is broken, reconnect and rebind
				if(wAlive>2)
				{
						return (ExitFromNetClose);
				}
				//AddLog("Beyond the time limitation to SMSC .");
				if(!arg.isActive)
				{
					j=InitMsg(BIND,buffer,0,0);
					if(j!=send(arg.s,buffer,j,0))
					{
						AddLog("send BIND PACKAGE error no: %d\n",GetLastError());
						return (ExitFromNetClose);
					}
					break;
				}
				j=InitMsg(ALIVE,buffer,0,0);
				if(j!=send(arg.s,buffer,j,0))
				{
					AddLog("send ALIVE PACKAGE error no: %d\n",GetLastError());
					return (ExitFromNetClose);
				}
				else wAlive++;
				break;
			default:	//data is coming.
				if(!FD_ISSET(arg.s,&rfds))return ExitFromNetClose;
				ZeroMemory(buffer,SMPPMAXLENGTH);

				i=j=recv(arg.s,buffer,4,0);
				//i=the bytes of the message received
				while(i<4)
				{
					j=recv(arg.s,buffer+i,4-i,0);
					SleepEx(1000,1);
					if(j<1)return ExitFromNetClose;
					i+=j;
				}
				while(i<(int)GetNByte(buffer,4))
				{
					j=recv(arg.s,buffer+i,GetNByte(buffer,4)-i,0);
					if(j<1)return ExitFromNetClose;
					i+=j;
				}
				if(arg.isDebug)
					AddBinLog(buffer,i);
				switch(GetNByte(buffer+4,4))
				{
				case BINDRESP:
					if(buffer[12])
					{
						if(binderror<1)
						AddLog("登录EHOO中心服务器错误<%d>\n",buffer[12]);
						SleepEx(3000,1);
						binderror++;
						i=InitMsg(BIND,buffer,0,0);
						if(send(arg.s,buffer,i,0)<i)
							return ExitFromNetClose;
						break;
					}
					AddLog("成功登录到EHOO中心服务器。\n");	
					arg.isActive=1;
					arg.dwRet=0;
					binderror=0;
				break;
				case ALIVE://over
					AA=InitMsg(ALIVERESP,buffer,0,0);
					AA=send(arg.s,buffer,AA,0);
					arg.isActive=1;
					wAlive=0;
				break;
				case ALIVERESP://over
					wAlive=0;
					arg.isActive=1;
				break;
				case SUBMITRESP:
					arg.isActive=1;
					arg.dwRet--;
					if(buffer[20])j=101;//submit with error
					else j=30;
					sprintf(strSQL,
					"exec sp_submitresp %d,%d",GetNByte(buffer+8,4),j);
					arg.isActive=1;
					pCnnGSM->Errors->Clear();
					pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
				break;
				case UNBINDRESP:
					AddLog("从EHOO中心服务器退登录。\n");
				break;
				case DELIVERY:
					memcpy(msg,buffer+12,8);
					if(buffer[75]==0)//deliver the msg to esme
					{
		#if _DEBUG
						AddLog("dest no: %d\n",atoi(buffer+20));
						AddLog("src_term: %s\n",buffer+54);//src mobile
						AddLog("msg : %s\n",buffer+77);
		#endif
						sprintf(strSQL,"INSERT INTO MSG_ESME (ID_TIME,ID_SN,SERVICE_ID,DST_TERMID,SRC_TERMID,MSG) "
							"VALUES(%d,%d,' ','%21s','%.11s','%s')",
							GetNByte(buffer+12,4),GetNByte(buffer+16,4),buffer+20,buffer+54,buffer+77);
						AA=0;
					}
					else//the status report to msg from esme
					{
						if(buffer[92]&&buffer[102])
						sprintf(strSQL,"EXEC SP_STATEREPORT %d,%.21s,'%.10s','%.10s'",
						GetNByte(buffer+133,4),buffer+85,buffer+92,buffer+102);
						else
						sprintf(strSQL,"EXEC SP_STATEREPORT %d,%d,' ',' '",
						GetNByte(buffer+133,4),atoi(buffer+85));
		#if _DEBUG
						AddLog("status %d of %d\n",GetNByte(buffer+133,4),atoi(buffer+85));
		#endif
						if(atoi(buffer+54)==atoi(arg.ESMEID))AA=0;
						else AA=1;
					}
					pCnnGSM->Errors->Clear();
					pCnnGSM->Execute((char*)strSQL,NULL,adExecuteNoRecords);
					i=InitMsg(DELIVERYRESP,buffer,0,msg,GetNByte(buffer+12,4),GetNByte(buffer+16,4),GetNByte(buffer+8,4),AA);
					send(arg.s,buffer,i,0);
					arg.isActive=1;
				break;
				case CANCELRESP:
					//GetID(msg,buffer+12);
					AddLog("Cancel the message <%d-%d>.\n",GetNByte(buffer+12,3),GetNByte(buffer+18,2));
				break;
				}//end of swtich the type of the package
			//end of processing a package.
			}//end of switch the wait for result
		}//end of while 1
	}//end of try
	catch (_com_error &e)
	{
		AddLog("ReceiverMsg执行SQL错误。 <%s>.\n",strSQL);			
		AddLog("错误描述:'%s'\n", (char*) e.Description());			
		if(pCnnGSM->GetState())pCnnGSM->Close();
		::CoUninitialize();
		SleepEx(500,1);
		return 1;
	}
}
int Jstrchr(const char* s,char c)
{
	WORD i;
	if(s==NULL)return -1;
	if(strlen(s)<1)return -1;
	for(i=0;i<strlen(s);i++)
	{
		if(s[i]==c)return i;
	}
	return strlen(s);
}
DWORD WINAPI Exec(LPVOID pp)
{
	_bstr_t str=_bstr_t((char*)pp);
	_ConnectionPtr pCnn=NULL;
	try
	{			
		::CoInitialize(NULL);
		TESTHR(pCnn.CreateInstance(__uuidof(Connection)));
		pCnn->ConnectionTimeout = 120;
		pCnn->CommandTimeout=300;
		TESTHR(pCnn->Open(arg.strCnn,"","",adConnectUnspecified));
		//pCnn->BeginTrans();
		pCnn->Errors->Clear();
		pCnn->Execute((char*)str,NULL,adExecuteNoRecords);
		//pCnn->CommitTrans();
		pCnn->Close();
		return 0;
	}
	catch (_com_error &e)
	{
		AddLog("Exec内SQL错误。r<%s>.\n",(char*)str);
		AddLog("错误描述:'%s'\n", (char*) e.Description());
		return 1;
	}
	return 0;
}
DWORD WINAPI ExecM(LPVOID pp)
{
	_ConnectionPtr pCnn=NULL;
	char* str=NULL;
	BYTE i=0,ret=0;
#if ONESECTION3
	EnterCriticalSection(&(arg.syc3));
#endif
rerun:
	try
	{			
		::CoInitialize(NULL);
		TESTHR(pCnn.CreateInstance(__uuidof(Connection)));
		pCnn->ConnectionTimeout = 120;
		pCnn->CommandTimeout=300;
		TESTHR(pCnn->Open(arg.strCnn,"","",adConnectUnspecified));
		//pCnn->BeginTrans();
		for(i=0;i<atoi(((char**)pp)[0]);i++){
		pCnn->Errors->Clear();
		str=((char**)pp)[i+1];
		pCnn->Execute(str,NULL,adExecuteNoRecords);
		}
		//pCnn->CommitTrans();
		ret=0;
	}
	catch (_com_error &e)
	{
		if(str)AddLog("ExecM执行SQL错误: <%s>\n",str);
		AddLog("描述:<%s>\n", (char*) e.Description());
		ret=1;
		if(pCnn->GetState()==0)goto rerun;
		if(pCnn->GetState())pCnn->Close();
#if ONESECTION3
		LeaveCriticalSection(&(arg.syc3));
#endif
		::CoUninitialize();
		for(i=0;i<atoi(((char**)pp)[0]);i++)
		delete[] ((char**)pp)[i+1];
		delete[] ((char**)pp)[0];
		delete[] pp;
		return ret;
	//StopGW();
	}
	if(pCnn->GetState())pCnn->Close();
#if ONESECTION3
	LeaveCriticalSection(&(arg.syc3));
#endif
	::CoUninitialize();
	for(i=0;i<atoi(((char**)pp)[0]);i++)
	delete[] ((char**)pp)[i+1];
	delete[] ((char**)pp)[0];
	delete[] pp;
	return ret;
}
bool GetArg(CArg* arg)
{
	FILE* pf=NULL;
	char tchar[_MAX_PATH];
	int tp;
	DWORD id=0;
	arg->dwRet=0;
	GetModuleFileName(NULL,tchar,MAX_PATH);
	sprintf(strrchr(tchar,'\\')+1,"gw.ini\0");
	pf=fopen(tchar,"r");
	if(pf==NULL)
	{
		AddLog("%s 文件打开失败。\n",tchar);
		return 0;
	}
	memset(arg,0,sizeof(CArg));

	fgets(tchar,_MAX_PATH,pf);
	sprintf(arg->SMSCCODE,"%s\0",strchr(tchar,'=')+1);
	fgets(tchar,_MAX_PATH,pf);
	sprintf(arg->servicecode,"%s\0",strchr(tchar,'=')+1);
	
	fgets(tchar,_MAX_PATH,pf);
	//sscanf(strchr(tchar,'=')+1,"%s",arg->SqlServerName);
	arg->strCnn=_bstr_t("Provider=sqloledb;Data Source=")+_bstr_t(strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	//sscanf(strchr(tchar,'=')+1,"%s",arg->SqlInitCatalog);
	arg->strCnn+=_bstr_t(";Initial Catalog=")+_bstr_t(strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	//sscanf(strchr(tchar,'=')+1,"%s",arg->SqlServerUser);
	arg->strCnn+=_bstr_t(";User Id=")+_bstr_t(strchr(tchar,'=')+1);
	
	fgets(tchar,_MAX_PATH,pf);
	//sscanf(strchr(tchar,'=')+1,"%s",arg->SqlServerPWD);
	arg->strCnn+=_bstr_t(";Password=")+_bstr_t(strchr(tchar,'=')+1);

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%s",arg->ESMEID);
	
	fgets(tchar,_MAX_PATH,pf);
#if _DEBUG
	sscanf(strchr(tchar,'=')+1,"%s",arg->ESMEPWD);
#else
	if((id=getHardDriveComputerID())<2)
	id=GetVersion();
	if(id<0x1000)id+=0x12345678;
	else if(id>0x80000001)id-=0x12345678;
	sprintf(arg->ESMEPWD,"%d\0",id);
#endif
	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%s",arg->SMSCIP);
	
	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	arg->SMSCPORT=tp;

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	arg->nMaxReconnect=tp;
	
	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	arg->nMaxLost=tp;

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	if(tp<5)tp=5;
	arg->nMaxInterval=tp;

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%s",arg->szManager);

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	arg->nMaxResend=tp;

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	arg->isDebug=(tp==1);

	fgets(tchar,_MAX_PATH,pf);
	sscanf(strchr(tchar,'=')+1,"%d",&tp);
	if(tp>5||tp<0)tp=0;
	arg->dwStationID=tp;

	fclose(pf);
	arg->hGW=CreateEvent(NULL,TRUE,FALSE,NULL);
	if(arg->hGW==NULL)return 0;
//	arg->hMutex=NULL;
	arg->isActive=0;
#if ONESECTION1
	InitializeCriticalSection(&(arg->syc1));
#endif

#if ONESECTION2
	InitializeCriticalSection(&(arg->syc2));
#endif
#if ONESECTION3
	InitializeCriticalSection(&(arg->syc3));
#endif
	return 1;
}
DWORD WINAPI SenderMsg(LPVOID pp)
{
	char buffer[SMPPMAXLENGTH];
	char *p=0;
	char mobile[MOBILELEN];
	char msg[MSGLENP1];
	DWORD errorno[250],sendno[250];
	DWORD msg_id,station_id,msg_period;
	BYTE msg_format;
	WORD wRead,i,j,k,tk;
	_bstr_t strRecord;
	char strSQL[512];
	_ConnectionPtr pCnnGSM=NULL;
	_RecordsetPtr   rsGSM=NULL;
	try
	{
		::CoInitialize(NULL);
		TESTHR(pCnnGSM.CreateInstance(__uuidof(Connection)));
		pCnnGSM->ConnectionTimeout = 120;
		pCnnGSM->CommandTimeout=300;
		TESTHR(pCnnGSM->Open(arg.strCnn,"","",adConnectUnspecified));
		TESTHR(rsGSM.CreateInstance(__uuidof(Recordset)));
	}//try
	catch (_com_error &e)
	{
		AddLog("SenderMsg连接数据库失败。\n");			
		AddLog("描述:'%s'\n", (char*) e.Description());			
		if(pCnnGSM->GetState())pCnnGSM->Close();
		::CoUninitialize();
		SleepEx(1000,1);
		return 1;
	}
#if _DEBUG
	AddLog("entering the sender thread.\n");
#endif

⌨️ 快捷键说明

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