📄 cmpp3smg.cpp
字号:
// CMPP3SMG.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "CMPP3SMG.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
MyCMPPClient::MyCMPPClient()
{
}
MyCMPPClient::~MyCMPPClient()
{
}
void MyCMPPClient::OnConnectionLogWrite(CMPPConnection & connection, const char * str)
{
printf("%s\n", str);
}
void MyCMPPClient::DumpStatus(CMPPConnection & connection)
{
printf("INFO|TOKEN=%s|MT_OK=%u|MT_FAIL=%u|RESP_OK=%u|RESP_FAIL=%u|MO=%u|SR=%u;\n",
connection.GetToken(),
connection.GetSubmitSucceedCount(),
connection.GetSubmitFailedCount(),
connection.GetRespSucceedCount(),
connection.GetRespFailedCount(),
connection.GetDeliverCount(),
connection.GetStatusReportCount());
}
void MyCMPPClient::Release()
{
CMPPClient::Release();
}
BOOL MyCMPPClient::OnConnectionSubmiting(
CMPPConnection & connection,
cmpp_submit _message
)
{
return TRUE;
}
void MyCMPPClient::OnConnectionSubmited(
CMPPConnection & connection,
cmpp_submit _message
)
{
printf("SUBMIT|SERVICEID:%s|FEETYPE:%s|FEECODE:%s|SRCID:%s|DESTID:%s|MSG:%s;\n",
GetSubStr(_message.sServiceId, 10),
GetSubStr(_message.sFeeType, 2),
GetSubStr(_message.sFeeCode, 6),
GetSubStr(_message.sSrcId, 21),
GetSubStr(_message.sDstTerminalId, /*32*/LENGTH_OF_MSISDN_IN_MOMT),
_message.sMsgContent);
return CMPPClient::OnConnectionSubmited(connection, _message);
}
void MyCMPPClient::OnConnectionSubmitFailed(
CMPPConnection & connection,
cmpp_submit _message
)
{
return CMPPClient::OnConnectionSubmitFailed(connection, _message);
}
void MyCMPPClient::OnReceivedConnectionSubmitResponse(
CMPPConnection & connection,
cmpp_submit_deliver_resp resp
)
{
CString sMsgId = _T("");
sMsgId.Format("%I64u", resp.nMsgid);
printf("SUBMITRESP|SEQID=%u|RESULT=%u|MSGID=%s;\n",
resp.nSeqId,
resp.nResult,
sMsgId);
return CMPPClient::OnReceivedConnectionSubmitResponse(connection, resp);
}
void MyCMPPClient::OnConnectionCleared(CMPPConnection & connection)
{
//DumpStatus(connection);
return;
}
void MyCMPPClient::OnReceivedConnectionDeliver(
CMPPConnection & connection,
cmpp_deliver deliver
)
{
CString szMsgId = _T("");
szMsgId.Format("%I64u", hton64(deliver.nMsgid));
CString szDstId = GetSubStr(deliver.sDestid, 21);
unsigned bMsgFmt = deliver.uchMsgfmt;
CString szSrcTerminalId = GetSubStr(deliver.sSrcterminalid, /*32*/LENGTH_OF_MSISDN_IN_MOMT);
CString szMsg = GetSubStr(deliver.MO_Msg_Content.sMsgcontent, deliver.uchMsglength);
if (bMsgFmt == 8) {
// 转换UCS2
char sMultiByte[141] = "";
unsigned char uchTmp = 0;
unsigned char pchTmp[1024] = "";
memcpy(pchTmp, deliver.MO_Msg_Content.sMsgcontent, deliver.uchMsglength);
for (int ix=0; ix<deliver.uchMsglength/2; ix++) {
uchTmp = pchTmp[ix*2];
pchTmp[ix*2] = pchTmp[ix*2 + 1];
pchTmp[ix*2 + 1] = uchTmp;
}
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, (LPCWSTR)pchTmp, deliver.uchMsglength/2, sMultiByte, 140, NULL, NULL);
szMsg = sMultiByte;
}
CString szLinkId = GetSubStr(deliver.sLinkId, 20);
printf("DELIVER|SRCMOBILE:%s|SPNUMBER:%s|MSGFMT:%u|LENGTH:%u|MSG:%s|LINKID:%s;\n",
szSrcTerminalId,
szDstId,
bMsgFmt,
szMsg.GetLength(),
szMsg,
szLinkId
);
return CMPPClient::OnReceivedConnectionDeliver(connection, deliver);
}
void MyCMPPClient::OnReceivedConnectionStatusReport(
CMPPConnection & connection,
cmpp_deliver sr
)
{
CString state = GetSubStr(sr.MO_Msg_Content.csr.sStat, 7);
CString destid = GetSubStr(sr.MO_Msg_Content.csr.sDestTerminalId, /*32*/LENGTH_OF_MSISDN_IN_MOMT);
CString donetime = GetSubStr(sr.MO_Msg_Content.csr.sDoneTime, 10);
CString submittime = GetSubStr(sr.MO_Msg_Content.csr.sSubmitTime, 10);
unsigned smscid = sr.MO_Msg_Content.csr.nSmscSeq;
CString msgid = _T("");
msgid.Format("%I64u", sr.MO_Msg_Content.csr.nMsgid);
printf(
"REPORT|STATE:%s|DESTTERMINALID:%s|DONETIME:%s|SUBMITTIME:%s|SMSCSEQID:%u|MSGID:%s;\n",
state,
destid,
donetime,
submittime,
smscid,
msgid
);
return;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("致命错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
}
MyCMPPClient * client = new MyCMPPClient();
client->Initialize();
CMPPConnection * connection = client->MakeConnection(
"127.0.0.1",
7890,
CMPP3_VERSION, "901234", "123456", "901234", e_SendAndRecv, FALSE, 25, TRUE
);
if (connection == NULL || !connection->IsOpened() ) {
printf("建立收发连接失败.\n");
return -1;
}
else {
printf("建立收发连接成功\n");
}
int ix = 0;
for (ix = 0; ix < 10; ix++) {
Sleep(1500);
CMPP_SUBMIT msg;
ZeroMemory(&msg, sizeof(CMPP_SUBMIT));
msg.nMsgid = 0;
msg.uchDstUsrTl = 1;
strncpy(msg.sDstTerminalId, "+8613901168061", LENGTH_OF_MSISDN_IN_MOMT);
strncpy(msg.sFeeCode, "000000", 6);
strncpy(msg.sFeeTerminalId, "+8613901168061",LENGTH_OF_MSISDN_IN_MOMT);
strncpy(msg.sFeeType, "01", 2);
strncpy(msg.sLinkId, "01010101010101010101", 20);
strncpy(msg.sMsgContent, "TEST消息", 8);
strncpy(msg.sMsgSrc, "901234", 6);
strncpy(msg.sServiceId, "HELP", 10);
strncpy(msg.sSrcId, "01850", 21);
msg.uchDestTerminalType = 0;
msg.uchFeeUserType = 0;
msg.uchFeeTerminalType = 0;
msg.uchMsgFmt = 15;
msg.uchMsgLength = 8;
msg.uchMsgLevel = 3;
msg.uchPkNumber = 1;
msg.uchPkTotal = 1;
msg.uchRegisteredDelivery = 1;
msg.uchTpPid = 0;
msg.uchTpUdhi = 0;
// 开始发送
client->Submit(msg);
}
client->Release();
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -