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

📄 scmpp30operator.cpp

📁 一个支持EMPP协议的企信通程序,很完整可以马上用,完全socket底层协议编写,不用移动提供的API
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SCmpp30Operator.cpp: implementation of the CSCmpp30Operator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "SCmpp30Operator.h"
#include "yes.h"
#include "md5.h"
#include "data.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CCriticalSection m_molock;
extern CCriticalSection m_reportlock;
extern CPtrList m_molist;
extern CPtrList m_reportlist;


#define CMPP30_CONNECT	0x00000001
#define CMPP30_CONNECT_RESP	0x80000001
#define CMPP30_TERMINATE	0x00000002
#define CMPP30_TERMINATE_RESP	0x80000002
#define CMPP30_SUBMIT		0x00000004
#define CMPP30_SUBMIT_RESP	0x80000004
#define CMPP30_DELIVER	0x00000005
#define CMPP30_DELIVER_RESP	0x80000005
#define CMPP30_QUERY	0x00000006
#define CMPP30_QUERY_RESP	0x80000006
#define CMPP30_CANCEL		0x00000007
#define CMPP30_CANCEL_RESP	0x80000007
#define CMPP30_ACTIVE_TEST	0x00000008
#define CMPP30_ACTIVE_TEST_RESP	0x80000008


//len
#define LEN_CMPP30_HEADER			       12
#define LEN_CMPP30_LOGIN			       42
#define LEN_CMPP30_LOGIN_RESP		       25
#define LEN_CMPP30_SUBMIT			       203
#define LEN_CMPP30_SUBMIT_RESP	       14
#define LEN_CMPP30_DELIVER		       99
#define LEN_CMPP30_DELIVER_RESP	       14

#define T_TIME 20
#define C_TIME 30
#define N_COUNT 3
char * ConvertToSingle(char * strDest, CString strSource, int length);
CString ConvertToString(BYTE * strSource, int length);

#define MAX_RETRY_CNT 3
void ShowMsg(CString msg);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void MD5_string (unsigned char *string, int len,unsigned char * dest);

CSCmpp30Operator::CSCmpp30Operator(BYTE _telcomid):COperator(_telcomid,5)
{
	for(int i=0;i<m_mtthread+m_mothread;i++)
	{
		m_ConnList[i]=NULL;
		m_toevent[i]=CreateEvent(NULL,FALSE,FALSE,NULL);
		ResetEvent(m_toevent[i]);
		
	}
	m_runrecv=false;
	
	m_sequence=0;
}

CSCmpp30Operator::~CSCmpp30Operator()
{
	for(int i=0;i<m_mtthread+m_mothread;i++)
	{
		if(m_ConnList[i])
		{
			CloseSocket(m_ConnList[i]->sock);
			delete m_ConnList[i];
		}
		CloseHandle(m_toevent[i]);		
	}
	RemoveAllMtStr();
}

void CSCmpp30Operator::LoadSetting()
{
	COperator::LoadSetting();	
}

void CSCmpp30Operator::InitOperator()
{
	for(int i=0;i<m_mtthread+m_mothread;i++)
	{
		m_ConnList[i]=new ConnElement;
		m_ConnList[i]->sock=NULL;
		m_ConnList[i]->state=0;
	}
}
struct cmpp30result
{
	char msgId[21];
	unsigned long state;
	HANDLE hevent;
};
int CSCmpp30Operator::Submit(CString content, CString mobile,CString srcid,CString svrid,char* gmsgid,unsigned long* gstate)
{
	
	int sindex=-1;
	while(1)
	{
		sindex=GetFreeMtConn();
		if(sindex>=0) break;
		Sleep(1);
	}

	
	int count=1;
	
	char submit[LEN_CMPP30_HEADER+LEN_CMPP30_SUBMIT+254];
	memset(submit,0,LEN_CMPP30_HEADER+LEN_CMPP30_SUBMIT+254);
	
	unsigned long templong=0;
	templong=LEN_CMPP30_HEADER+LEN_CMPP30_SUBMIT+content.GetLength();
	templong=htonl(templong);
	
	int offset=0;
	
	memcpy(submit+offset,&templong,4);
	offset+=4;
	
	templong=htonl(CMPP30_SUBMIT);
	memcpy(submit+offset,&templong,4);
	offset+=4;
	
	offset+=4;

	offset+=10;

	*(submit+offset)=1;
	offset+=1;

	*(submit+offset)=1;
	offset+=1;
	
	*(submit+offset)=1;
	offset+=1;

	*(submit+offset)=15;
	offset+=1;

	offset+=17;

	offset+=17;

	unsigned long temp=0;
	temp=1;
	temp=htonl(temp);
	memcpy(submit+offset,&temp,4);
	offset+=4;

	offset+=32;

	*(submit+offset)=content.GetLength();
	offset+=1;

	memcpy(submit+offset,(char*)(LPCTSTR)content,content.GetLength());
	offset+=content.GetLength();

	offset+=21;

	memcpy(submit+offset,(char*)(LPCTSTR)srcid,srcid.GetLength());
	offset+=21;

	

	memcpy(submit+offset,(char*)(LPCTSTR)svrid,svrid.GetLength());
	offset+=10;

	offset+=66;
	
	
	int index=0;
	for(index=0;index<count;index++)
	{
		
		char msgId[21];
		unsigned long state;
		state=20000;
		strcpy(msgId,"-1");	
		
		for(int i=0;i<MAX_RETRY_CNT;i++)
		{
			if(m_ConnList[sindex]->sock==NULL)
			{
				Sleep(100);
				continue;
			}
			SOCKET client=m_ConnList[sindex]->sock;
			state=20000;
			strcpy(msgId,"-1");			
			unsigned long mt_seq=GenSequence();
			HANDLE hevent=CreateEvent(NULL,FALSE,FALSE,NULL);
			AddMtResultStr(mt_seq,hevent);
			
			templong=htonl(mt_seq);
			memcpy(submit+8,&templong,4);

			memcpy(submit+12+52,(char*)(LPCTSTR)mobile,mobile.GetLength());	

			if(SendSocket(client,submit,offset,MAX_SENDTIME)<=0)
			{
				RemoveMtResult(mt_seq);
				Sleep(100);
				continue;								
			}						

			WaitForSingleObject(hevent,MAX_RECVTIME*1000);
			GetMtResult(mt_seq,msgId,state);
			if(gmsgid)
				strcpy(gmsgid,msgId);
			if(gstate)
				*gstate=state;
			RemoveMtResult(mt_seq);
			break;
		}

	
		
	}
	m_ConnList[sindex]->state=0;
	return index;
}	
unsigned long CSCmpp30Operator::GenSequence()
{
	CSingleLock lock(&m_lock);
	lock.Lock();
	m_sequence++;
	if(m_sequence>=0x7FFFFFFF) m_sequence=1;	
	return m_sequence;
}

SOCKET CSCmpp30Operator::Login( BYTE mode)
{
	char logbody[LEN_CMPP30_HEADER+LEN_CMPP30_LOGIN];
	memset(logbody,0,LEN_CMPP30_HEADER+LEN_CMPP30_LOGIN);
	
	unsigned long templong=0;
	templong=LEN_CMPP30_HEADER+LEN_CMPP30_LOGIN;
	templong=htonl(templong);
	
	int offset=0;
	
	memcpy(logbody+offset,&templong,4);
	offset+=4;
	
	templong=htonl(CMPP30_CONNECT);
	memcpy(logbody+offset,&templong,4);
	offset+=4;
	
	templong=htonl(GenSequence());
	memcpy(logbody+offset,&templong,4);
	offset+=4;
	
	memcpy(logbody+offset,uid,min(strlen(uid),21));
	offset+=21;
	
	char AuthenticatorClient[55];
	memset(AuthenticatorClient,0,55);
	int k=0;	
	memcpy(AuthenticatorClient,uid,min(strlen(uid),21));
	k=min(strlen(uid),21);
	
	k+=9;
	
	memcpy(AuthenticatorClient+k,pwd,min(strlen(pwd),15));
	k+=min(strlen(pwd),15);
	
	CTime t=CTime::GetCurrentTime();
	memcpy(AuthenticatorClient+k,(char*)(LPCTSTR)t.Format("%m%d%H%M%S"),10);
	//memset(AuthenticatorClient+k,'0',10);	
	k+=10;
	
	MD5_string((unsigned char *)AuthenticatorClient,k,(unsigned char *)AuthenticatorClient);
	
	memcpy(logbody+offset,AuthenticatorClient,16);
	offset+=16;
	
	*(logbody+offset)=0x20;
	offset+=1;
	
	long intstmp=atoi((char*)(LPCTSTR)t.Format("%m%d%H%M%S"));	
	templong=htonl(intstmp);
	memcpy(logbody+offset,&templong,4);
	offset+=4;
	
	SOCKET client=ConnectSocket(host,port);
	if(client==NULL) 
	{
		return NULL;	
	}
	
	if(SendSocket(client,logbody,offset,MAX_SENDTIME)<=0)
	{
		CloseSocket(client);
		client=NULL;
		return NULL;
	}
	
	return client;
}

struct cmpp30tstr
{
	CSCmpp30Operator* p;
	int			index;
	BYTE		mode;
};

DWORD CSCmpp30Operator::RecvThread(LPVOID lp)
{
	SOCKET client=NULL;
	cmpp30tstr* t=(cmpp30tstr*)lp;
	CSCmpp30Operator* p=t->p;
	int index=t->index;
	BYTE mode=t->mode;
	delete t;
	time_t t_flag=0;
	int  ActiveCnt=0;
	while(p->m_runrecv)
	{
		if(client==NULL)
		{
			while(p->m_runrecv)
			{
				client=p->Login(mode);
				if(client) break;
				Sleep(1);
			}
			if(!p->m_runrecv) break;
			ActiveCnt=0;
			t_flag=0;
		}
		if(p->m_ConnList[index]->sock!=NULL)
		{
			time_t t_now;
			time(&t_now);
			if((t_now-t_flag>=C_TIME)||(ActiveCnt>0&&t_now-t_flag>=T_TIME))
			{
				if(!p->SendActiveTest(client,index)) continue;
				t_flag=t_now;
				ActiveCnt++;
				if(ActiveCnt>3)
				{
					CloseSocket(client);
					p->m_ConnList[index]->sock=client=NULL;
					continue;
				}
			}
		}
	
		FD_SET fd = {1,client};
		TIMEVAL tv = {0, 0};
		int ret=select(0, &fd, NULL, NULL, &tv);
		if(ret==1)
		{
			unsigned long cmd;
			bool rret=p->ReceivBody(client,index,cmd);
			time(&t_flag);
			if(rret)
			{
				if(cmd==CMPP30_ACTIVE_TEST_RESP)
				{
					ActiveCnt=0;
					time(&t_flag);
				}
			}else
				continue;
		}else if(ret==SOCKET_ERROR)
		{
			CloseSocket(client);
			p->m_ConnList[index]->sock=client=NULL;
		}
		Sleep(1);
	}	
	if(client)
	{
		CloseSocket(client);
		p->m_ConnList[index]->sock=client=NULL;		
	}
	SetEvent(p->m_toevent[index]);
	return 0;
}

int CSCmpp30Operator::GetFreeMtConn()
{
	int index=-1;
	m_connlock.Lock();
	for(int i=0;i<m_mtthread;i++)
	{
		if(m_ConnList[i]->state==0)
		{
			index=i;
			m_ConnList[i]->state=1;
			break;
		}
	}
	m_connlock.Unlock();
	return index;
}

bool CSCmpp30Operator::ReceivBody(SOCKET &client, int index,unsigned long& cmdstore)
{
	
	CString msg;
	char head[LEN_CMPP30_HEADER];	
	char content[LEN_CMPP30_DELIVER+254+1];
	char logresp[LEN_CMPP30_LOGIN_RESP];
	memset(content,0,LEN_CMPP30_DELIVER+254+1);
	char submitresp[LEN_CMPP30_SUBMIT_RESP];
	unsigned long templong=0;
	
	char SequenceID[4];
	unsigned long packetlen;

⌨️ 快捷键说明

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