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

📄 sgippacket.cpp

📁 一个SGIP网关的程序源码.vc代码
💻 CPP
字号:
// SGIPPacket.cpp: implementation of the CSGIPPacket class.
//
//////////////////////////////////////////////////////////////////////

//#include "atlconv.h"
#include "stdafx.h"
#include "SGIPGateWay.h"
#include "SGIPPacket.h"
#include "systemprofile.h"
#include "helpfunc.h"
#include "winsock2.h"
#include "atlconv.h"

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

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

CSGIPPacket::CSGIPPacket()
{
	
}

CSGIPPacket::~CSGIPPacket()
{

}
unsigned long CSGIPPacket::GetSGIPTime()
{
	CTime current_time=CTime::GetCurrentTime();
	char mytime[30];
    
	int current_month=current_time.GetMonth();
	int current_day=current_time.GetDay();
	int current_hour=current_time.GetHour();
	int current_minute=current_time.GetMinute();
	int current_second=current_time.GetSecond();
	sprintf(mytime,"%02d%02d%02d%02d%02d",current_month,current_day,current_hour,current_minute,current_second);
	return atol(mytime);
}
int CSGIPPacket::PacketBind(BYTE *lpBuffer)
{
	mnSN = 1;

	SBIND ini_bind;
	memset(&ini_bind, 0, sizeof(ini_bind));
	// 信息长度
	ini_bind.Msglen=htonl(sizeof(ini_bind));
	// 命令
	ini_bind.Cmd_id=htonl(SGIP_BIND);
	// 序列号
	ini_bind.Seq_Number1=htonl(gProfile.mlngSpnumber);
	ini_bind.Seq_Number2=htonl(GetSGIPTime());
	ini_bind.Seq_Number3=htonl(mnSN);
	// 连接类型
	ini_bind.Login_Type=1; // SP向SMG连接
	// 登录用户
	strcpy( ini_bind.Login_Name,gProfile.mstrLoginName);
	// 登录密码
	strcpy(  ini_bind.Login_Passowrd,gProfile.mstrLoginPassowrd);
	// 保留字
	strcpy(  ini_bind.Reserve,"");

	memcpy(lpBuffer, &ini_bind, sizeof(ini_bind));
	return ntohl(ini_bind.Msglen);
}
int CSGIPPacket::PacketBindResp(BYTE *lpBuffer, SBIND *pBindPacket)
{
	SBIND_RESP bind_resp;
 	memset(&bind_resp,0,sizeof(bind_resp));
	bind_resp.Msglen=htonl(sizeof(bind_resp));
	bind_resp.Cmd_id=htonl(SGIP_BIND_RESP);

    bind_resp.Seq_Number1 = pBindPacket->Seq_Number1;
    bind_resp.Seq_Number2 = pBindPacket->Seq_Number2;
    bind_resp.Seq_Number3 = pBindPacket->Seq_Number3;
	bind_resp.Result=0;//set

	memcpy(lpBuffer, &bind_resp, sizeof(bind_resp));
	return ntohl(bind_resp.Msglen);
}
int CSGIPPacket::PacketUnbindResp(BYTE *lpBuffer, SSGIP_MESSAGE_HEAD *pUnbindPacket)
{
	SSGIP_MESSAGE_HEAD unbind_resp;
	memset(&unbind_resp, 0, sizeof(unbind_resp));
	unbind_resp.Msglen = htonl(sizeof(unbind_resp));
	unbind_resp.Cmd_id = htonl(SGIP_UNBIND_RESP);

	unbind_resp.Seq_Number1 = pUnbindPacket->Seq_Number1;
	unbind_resp.Seq_Number2 = pUnbindPacket->Seq_Number2;
	unbind_resp.Seq_Number3 = pUnbindPacket->Seq_Number3;

	memcpy(lpBuffer, &unbind_resp, sizeof(unbind_resp));
	return ntohl(unbind_resp.Msglen);
}
int CSGIPPacket::GetBindRespResult(SBIND_RESP *pBindRespPacket)
{
	return ntohl(pBindRespPacket->Result);
}
// 关于提交
int CSGIPPacket::PacketSubmit(BYTE *lpBuffer, int nFeeType, char *strServiceType, char *strFeeValue, LPCTSTR strSender, LPCTSTR strSendTo,LPCTSTR strMsg)
{
	SSUBMIT  sumbit_info;
	memset(&sumbit_info, 0, sizeof(sumbit_info));

	memcpy(sumbit_info.SPNumber, strSender, strlen(strSender));
	sumbit_info.ChargeNumber[0] = 0;
//	memcpy(sumbit_info.ChargeNumber, "000000000000000000000", 21);
	sumbit_info.UserCount = 1;
	//memcpy(sumbit_info.UserNumber, "86", 2);
	//CString str = CHelpFunc::GetRealNumber(&Packet.strSendTo[1]);
	CString str;
	str.Format("86%s", CHelpFunc::GetRealNumber(strSendTo));
	memcpy(sumbit_info.UserNumber, str, str.GetLength());
	memcpy(sumbit_info.CorpId, gProfile.mstrCorpId, gProfile.mstrCorpId.GetLength());
	strcpy(sumbit_info.ServiceType, strServiceType);
		
	sumbit_info.FeeType = nFeeType;
	strcpy(sumbit_info.FeeValue, strFeeValue);
	//sumbit_info.FeeValue[0] = 0;
	sumbit_info.GivenValue[0] = 0;
	 
	sumbit_info.AgentFlag = 0;
	sumbit_info.MorelatetoMTFlag = 2;
	if( nFeeType >= 3) 
	  sumbit_info.MorelatetoMTFlag = 3;   //接收方免费或计费鉴权
	sumbit_info.Priority = 0;
	sumbit_info.ExpireTime[0] = 0;
	sumbit_info.ScheduleTime[0] = 0;
	sumbit_info.ReportFlag = 2;           //不返回状态报告
	if( nFeeType == 5)
      sumbit_info.ReportFlag = 3;         //包月鉴权状态报告
	sumbit_info.TP_pid = 0;
	sumbit_info.TP_udhi = 0;
	sumbit_info.MessageCoding = 15; //GBK编码
	sumbit_info.MessageType = 0;
	sumbit_info.MessageLength = htonl((strlen(strMsg)&0xff));
 	memcpy(sumbit_info.MessageContent, strMsg, strlen(strMsg)&0xff);
 
 	sumbit_info.Msglen = htonl(sizeof(sumbit_info)-sizeof(sumbit_info.MessageContent)+strlen(strMsg));
	sumbit_info.Cmd_id  =htonl(SGIP_SUBMIT);
	sumbit_info.Seq_Number1 =htonl(gProfile.mlngSpnumber);
	sumbit_info.Seq_Number2 = htonl(GetSGIPTime());
	mnSN++;
	sumbit_info.Seq_Number3 =htonl(mnSN);

	memcpy(lpBuffer, &sumbit_info, sizeof(sumbit_info));
	return ntohl(sumbit_info.Msglen);
}
int CSGIPPacket::GetSubmitRespResult(SSUBMIT_RESP *pSubmitRespPacket)
{
	return ntohl(pSubmitRespPacket->Result);
}
// 关于deliver
void CSGIPPacket::ParseDeliverPacket(BYTE *lpBuffer, LPTSTR strSender, LPTSTR strSendTo, LPTSTR strMsg)
{
	SDELIVER  deliver_info;
	memset(&deliver_info, 0, sizeof(deliver_info));
	memcpy(&deliver_info, lpBuffer, sizeof(deliver_info));

	if( strSender )
	{
		CString str = CHelpFunc::GetRealNumber(deliver_info.UserNumber);
		strcpy(strSender, str);
	}
	if( strSendTo )
	{
		strcpy(strSendTo, deliver_info.SPNumber);
	}
	if( strMsg )
	{
		if( deliver_info.MessageCoding == 8 )
		{
			USES_CONVERSION;
			WORD *pWord = (WORD*)deliver_info.MessageContent;
			for( int i = 0; i < ntohl(deliver_info.MessageLength)/2; i++ )
			{
				pWord[i] = ((pWord[i]&0xFF)<<8) + ((pWord[i]&0xFF00)>>8);
			}
			lstrcpy((char*)strMsg, W2A((unsigned short*)deliver_info.MessageContent));			
		}
		else
			strcpy(strMsg, deliver_info.MessageContent);
	}
}
int CSGIPPacket::PacketDeliverResp(BYTE *lpBuffer, SDELIVER *pDeliverPacket)
{
	SDELIVER_RESP resp_info;

	memset(&resp_info, 0, sizeof(resp_info));

	resp_info.Cmd_id = htonl(SGIP_DELIVER_RESP);
	resp_info.Seq_Number1 = pDeliverPacket->Seq_Number1;
	resp_info.Seq_Number2 = pDeliverPacket->Seq_Number2;
	resp_info.Seq_Number3 = pDeliverPacket->Seq_Number3;
	resp_info.Msglen = htonl(sizeof(resp_info));
	resp_info.Result = 0;

	memcpy(lpBuffer, &resp_info, sizeof(resp_info));
	return ntohl(resp_info.Msglen);
}

⌨️ 快捷键说明

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