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

📄 pushsms.cpp

📁 短信猫发送短信,使用PDU编码格式
💻 CPP
字号:
// PushSms.cpp: implementation of the CPushSms class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "modem_SMS.h"
#include "PushSms.h"

#include "Modem_SMSDlg.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPushSms::CPushSms()
{
	memset(PushMsg,0x00,sizeof(PushMsg));
	_bFileOpend=false;
	bRunning=false;
	_TotalSucc=0;
}

CPushSms::~CPushSms()
{

	Stop();
	Quit();

}


CString CPushSms::GetOneUserPhone()
{
	CString strUserPhone="";
	locker_Users.Lock();
	strUserPhone=RemoveLastLine();
	locker_Users.Unlock();

	strUserPhone.Replace("\r","");
	strUserPhone.Replace("\n","");
	strUserPhone.Replace(" ","");
	return strUserPhone;
}



CString CPushSms::RemoveLastLine()
{

	if(_bFileOpend==false) return "";

	char OneLine[300];
	memset(OneLine,0x00,sizeof(OneLine));	
	CString strOneLine="";
	
	int iMaxReadLen=150;
	if(_file_users.GetLength()<150) iMaxReadLen=_file_users.GetLength();
	
	_file_users.Seek((-1)*iMaxReadLen,CFile::current);
	int iReadLen=_file_users.Read(OneLine,iMaxReadLen);
	if(iReadLen>10)
	{
		char* pos= strrchr(OneLine,'\n');
		if(pos)
		{
			int iLen=strlen(pos);
			if(iLen<10)
			{
				unsigned long newLen=_file_users.GetLength()-iLen;
				_file_users.SetLength(newLen);
			}
			else
			{
				strOneLine=pos;
				strOneLine.Replace("\r","");
				strOneLine.Replace("\n","");
				
				unsigned long newLen=_file_users.GetLength()-iLen;
				_file_users.SetLength(newLen);
				
			}
		}
		else
		{
			if(iReadLen>=11)
			{
				strOneLine=OneLine;
				strOneLine.Replace("\r","");
				strOneLine.Replace("\n","");
				unsigned long newLen=_file_users.GetLength()-iReadLen;
				_file_users.SetLength(newLen);
			}
		}
	}
	return strOneLine;
}

void CPushSms::Update_SetState()
{

	STATTEXT stat;
	if(list_StateText.Get(stat)==false)
	{
		return;
	}

	if(_pDlg!=NULL)
	{
		_pDlg->SetDlgItemInt(IDC_STATIC_STATE,_TotalSucc,FALSE);
		if(_pDlg->m_List_State.GetSafeHwnd()!=NULL)
		{
			CListCtrl* pList=(CListCtrl*)_pDlg->GetDlgItem(IDC_LIST_STATE);
			if(pList)
			{
				CString strTem=pList->GetItemText(stat.ItemIndex,stat.fieldIndex);
				if(strTem.CompareNoCase(stat.szText)!=0)
				{
					pList->SetItemText(stat.ItemIndex,stat.fieldIndex,stat.szText);
				}				
			}
		}			
	}

}

void CPushSms::Thread_doWork(HANDLE& quitEvent,CString WorkName,int WorkIndex,void* pParam)
{
	CPushSms* pPushSms=(CPushSms*)pParam;
	CComPort* pCurrComPort=NULL;
	DataBase::CAdoConnection dbConn;
	CString appDir=Tools::CMyTools::GetAppDir();
	CString strDB_ConnStr="";
	strDB_ConnStr.Format("file name=%s\\db.udl",appDir);
	CString SQL="";

	DWORD SendInterrup_Second=atol(Tools::CMyTools::Config_Read("SendInterrup_Second"));
	

	CString strLog;
	CString strUserPhone="";
	bool bAddList=false;

	while (WaitForSingleObject(quitEvent,0)==WAIT_TIMEOUT)
	{
		Sleep(100);		

		if(bAddList)
		{
			bAddList=false;
			pPushSms->list_ComPorts.Add(pCurrComPort);//重新添加入队列
		}

		if(!pPushSms->bRunning)
		{
			break;
		}

		//打开数据库
		if(dbConn.IsOpen()==FALSE)
		{
			dbConn.Connect(strDB_ConnStr);
			if(dbConn.IsOpen()==FALSE)
			{
				Sleep(500);
				continue;
			}
		}

		if(pPushSms->list_ComPorts.Get(pCurrComPort)==false)
		{
			Sleep(300);
			continue;
		}

		
		bAddList=true;//已经取出端口,标记为需要重新添加回队列


		//处理该端口 
		if(pCurrComPort==NULL) 
		{
			bAddList=false;
			continue;
		}

		//更新端口状态
		//if(1==0)
		{
			CString tem;
			tem.Format("%d",pCurrComPort->_Counter_CanSend);
			pPushSms->SetState(pCurrComPort->iListIndex,2,tem);
			tem.Format("%d",pCurrComPort->_Counter_Succ);
			pPushSms->SetState(pCurrComPort->iListIndex,3,tem);
			tem.Format("%d",pCurrComPort->_Counter_Fail);
			pPushSms->SetState(pCurrComPort->iListIndex,4,tem);		
			
		}


		if(pCurrComPort->GetCanUseCount()==0 || pCurrComPort->_bError==true)
		{//没有可用条数

			bool bError=pCurrComPort->_bError;
			pCurrComPort->_bError=false;

			//判断是否插入了卡	
			CString SN;
			if(pCurrComPort->modem_GetSimSN(SN)==false)//读取当前端口Sim卡序列号
			{
				pPushSms->SetState(pCurrComPort->iListIndex,1,"读卡序列号失败");
				continue;//读卡序列号失败
			}
			if(SN.GetLength()==0)
			{
				//尚未插入Sim卡
				pPushSms->SetState(pCurrComPort->iListIndex,1,"尚未插入SIM卡");				
				continue;
			}

			//已经插入sim卡
			//判断是否需要换卡
			if(pCurrComPort->_SN.CompareNoCase(SN)==0) //两张卡是一样的,用户还没换卡,继续提示用户换卡
			{
				if(bError==false)
				{
					pPushSms->SetState(pCurrComPort->iListIndex,1,"本卡已经发完,请更换SIM卡");					
				}
				else
				{
					//已经产生了错误,但是检查到卡的序列号没变,不是用户换卡
				}
				continue;
			}
			else
			{
				//用户已经更换了SIM卡,或者产生了错误,请求重新检查卡的可用数
				//进行换卡操作				
				int iCanSendCount=0;
				SQL.Format("exec simCard_GetTodyCanSendCount '%s','sms'",SN);
				modioLib::DataBase::CAdoRecordSet rec(&dbConn);
				rec.Open(SQL);
				if(rec.IsOpen())
				{
					CString CanSendCount="0";
					rec.GetValueString(CanSendCount,"CanSendCount");				
					iCanSendCount=atoi(CanSendCount);
				}
				//更换sim卡
				pCurrComPort->ChangeSimCard(SN,iCanSendCount);
				pPushSms->SetState(pCurrComPort->iListIndex,1,"SIM卡已经更换成功");
				continue;
			}			
			pPushSms->SetState(pCurrComPort->iListIndex,1,"没可用数,请更换sim卡");
			continue;//没有可用数

		}


		////////////////////////////当前卡有可用数//////////////////////////////////////////////
		 //判断此端口的发送间隔是否已经到
		 if(GetTickCount()- pCurrComPort->tLastSend<SendInterrup_Second*1000)
		 {
			 continue;
		 }

		//判断误码率
		 /*pCurrComPort->modem_GetSimErrorE();
		 if(pCurrComPort->iSinal<=15 || pCurrComPort->iErrorE>10)
		 {
			 strLog.Format("信号[%d]太低或者误码率[%d]太高,暂停发送",pCurrComPort->iSinal,pCurrComPort->iErrorE);
			 pPushSms->SetState(pCurrComPort->iListIndex,1,strLog);
			 continue;
		 }
*/


		 pCurrComPort->tLastSend=GetTickCount();//更新最后发送时间

		 //判断是否有用户可发
		 if(strUserPhone.GetLength()==0)
		 {
			 //取一个用户号码
			 strUserPhone=pPushSms->GetOneUserPhone();
		}

		 if(strUserPhone.GetLength()==0)
		 {
			 pPushSms->SetState(pCurrComPort->iListIndex,1,"没取到用户");
			 continue;
		 }

		char szPhone[128];
		strcpy(szPhone,strUserPhone);
		pPushSms->SetState(pCurrComPort->iListIndex,1,"正在发送用户["+strUserPhone+"]");

		//pPushSms->locker_Device.Lock();

		if(pCurrComPort->modem_SendOneSms(szPhone,pPushSms->PushMsg)==true)
		{
			//更新数据库的可用数量
			CString SQL;
			SQL.Format("exec simCard_Update '%s','sms','1'",pCurrComPort->_SN);
			if(dbConn.ExecSql(SQL)<0)
			{
				dbConn.Disconnect();
			}

			pPushSms->_TotalSucc++;
			strUserPhone="";//发送已成功,需要重新获取号码
		}
		else
		{
			pCurrComPort->_bError=true;//发送失败,标志为产生了错误,请求重新检查卡的状态
		}
		//pPushSms->locker_Device.Unlock();
	}

	TRACE("Thread_doWork 线程退出\n");
}

bool CPushSms::Start(CModem_SMSDlg* pDlg)
{

	if(bRunning) return true;
	bRunning=true;

	list_StateText.Clear();

	//判断用户号码
	pDlg->SetDlgItemText(IDC_STATIC_STATE,"");
	CString UserFilePath=Tools::CMyTools::GetAppDir()+"\\群发号码.txt";
	CFileException fEx;
	_file_users.Open(UserFilePath,CFile::modeReadWrite|CFile::shareDenyWrite,&fEx);
	if(fEx.m_cause!=0)
	{
		AfxMessageBox("打开文件[群发号码.txt]失败",MB_OK|MB_ICONINFORMATION);
		return false;
	}
	
	_file_users.SeekToEnd();
	
	_bFileOpend=true;
	
	_TotalSucc=atol(Tools::CMyTools::Config_Read("TotalSucc"));

	list_ComPorts.Clear();

	POSITION pos=list_ComPorts_ForDelete.dataList.GetHeadPosition();
	while(pos)
	{
		CComPort* pComPort=list_ComPorts_ForDelete.dataList.GetNext(pos);
		list_ComPorts.Add(pComPort);
	}
	

	CString Msg;
	_pDlg->GetDlgItemText(IDC_EDIT_MSG,Msg);
	strcpy(PushMsg,Msg);
	
	int Thread_Work_Count=atoi(Tools::CMyTools::Config_Read("Thread_Work_Count"));
	if(Thread_Work_Count==0)Thread_Work_Count=1;

	_workers.AddWorker(Thread_Work_Count,Thread_doWork,"群发线程",this);
//	_workers.AddWorker(1,Thread_SetState,"状态更新线程",this);


	return true;
}

void CPushSms::Stop()
{

	if(bRunning==false) return;
	bRunning=false;
	Sleep(300);
	_workers.Stop();

	CString strTotalSuccCount="";
	strTotalSuccCount.Format("%d",_TotalSucc);	
	Tools::CMyTools::Config_Write("TotalSucc",strTotalSuccCount);

	
	if(_bFileOpend)
	{
		_file_users.Close();
		_bFileOpend=false;
	}


}

void CPushSms::SetState(int ItemIndex, int fieldIndex,CString strText)
{

	if(bRunning==false) return ;

	STATTEXT stat;
	stat.ItemIndex=ItemIndex;
	stat.fieldIndex=fieldIndex;
	strcpy(stat.szText,strText);
	list_StateText.Add(stat);
}

void CPushSms::Quit()
{
	
	CComPort* pComPort=NULL;
	while(list_ComPorts_ForDelete.Get(pComPort)==true)
	{
		if(pComPort)
		{
			TRACE("删除端口[%d]\n",pComPort->_port);
			delete pComPort;
			pComPort=NULL;
			
		}
	}
}

⌨️ 快捷键说明

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