CMPP网关协议核心代码 java版本
2018-01-24 16:18
567 查看
一:发起连接
public int connect() {
int stat = CMPPPara.CMPP_ERROR;
try {
CMPPConnectMessage cMsg = new CMPPConnectMessage();
String time = CTime.getTime(CTime.MMDDhhmmss);
byte[] user = SYS.Ps("cmpp_icp_id").getBytes();
byte[] auth = SYS.Ps("cmpp_icp_auth").getBytes();
byte[] timeStamp = time.getBytes();
byte abyte2[] = new byte[100];
System.arraycopy(user, 0, abyte2, 0, user.length);
int k = user.length + 9;
System.arraycopy(auth, 0, abyte2, k, auth.length);
k += auth.length;
System.arraycopy(timeStamp, 0, abyte2, k, 10);
k += 10;
byte auths[] = new byte[k];
System.arraycopy(abyte2, 0, auths, 0, k);
MD5 md5 = new MD5();
byte md5Msg[] = md5.getMD5ofBytes(auths, auths.length);
stat = login(cMsg, md5Msg, time);
if (stat != CMPPPara.CMPP_SUCCESS) {
log.warn("登录CMPP网关发生错误,错误码:" + stat);
close();
MonitorPara.isLogin = false;
} else {
log.warn("登录CMPP网关成功");
MonitorPara.isLogin = true;
}
} catch (Exception ex) {
ex.printStackTrace();
stat = CMPPPara.CONNECT_INIT_ERROR;
log.warn("CMPP:connect()-" + ex.toString());
close();
MonitorPara.isLogin = false;
}
return stat;
}
二:登录验证
private int login(CMPPConnectMessage cMsg, byte[] auth, String time)
throws IOException, Exception {
int stat = CMPPPara.CMPP_ERROR;
int length = 0;
byte[] inMsg = new byte[100];
System.arraycopy(Tools.int2byte(cMsg.commandID), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(cMsg.sequenceID), 0, inMsg, 8, 4);
length = CMPPPara.HEAD_LEN;
System.arraycopy(SYS.Ps("cmpp_icp_id").getBytes(), 0, inMsg, length,
SYS.Ps("cmpp_icp_id").getBytes().length);
length += SYS.Ps("cmpp_icp_id").getBytes().length;
System.arraycopy(auth, 0, inMsg, length, auth.length);
length += auth.length;
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
inMsg[length] = (byte) 0x20;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
inMsg[length] = (byte) 0x30;
}
length++;
System.arraycopy(Tools.int2byte(Integer.parseInt(time)), 0, inMsg,
length, 4);
length += 4;
cMsg.totalLength = length;
System.arraycopy(Tools.int2byte(cMsg.totalLength), 0, inMsg, 0, 4);
byte[] send = new byte[length];
System.arraycopy(inMsg, 0, send, 0, length);
socket = cmsm.getSocket();
socket.setSoTimeout(CMPPPara.READ_TIMEOUT);
log.warn("获取CMPP网关SOCKET连接成功");
send(send);
CMPPConnectMessageResp rMsg = result();
if (rMsg.commandID == CMPPCommand.COMMAND_CMPP_CONNECT_RESP)
stat = rMsg.nStatus;
return stat;
}
三:发送心跳包(长链接需要保持在线)
public void activeTest() {
CMPPActiveTestMessage test = new CMPPActiveTestMessage();
try {
byte[] send = new byte[12];
System.arraycopy(Tools.int2byte(test.totalLength), 0, send, 0, 4);
System.arraycopy(Tools.int2byte(test.commandID), 0, send, 4, 4);
System.arraycopy(Tools.int2byte(test.sequenceID), 0, send, 8, 4);
send(send);
} catch (IOException e) {
log.warn("与CMPP网关握手异常:" + test.sequenceID);
}
}
四:发送下行短信
public void submit(CMPPSubmitMessage msg) {
int maxlonglen = 140;
byte[] tp_udhiHead = new byte[6];
byte[] msgUCS2 = null;
try {
msgUCS2 = msg.sMsgContent.getBytes("UTF-16BE");
//System.out.println("msgUCS2:"+msgUCS2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int msgUCS2Len = msgUCS2.length;// 长短信长度
int msgUCS2Count;
int sign = 1;
if (msgUCS2Len <= maxlonglen) {
msgUCS2Count = 1;
sign = 0;
} else {
msgUCS2Count = returnSmsSendNum(msg.sMsgContent);
tp_udhiHead[0] = 0x05;
tp_udhiHead[1] = 0x00;
tp_udhiHead[2] = 0x03;
tp_udhiHead[3] = 0x0A;
tp_udhiHead[4] = (byte) msgUCS2Count;
tp_udhiHead[5] = 0x01;
}
for (int i = 0; i < msgUCS2Count; i++) {
msg.sequenceID = SEQ.getSequenceID();
byte[] msgContent = null;
if (sign == 0) {
try {
msg.nMsgFormat = 8;
msgContent = msg.sMsgContent.getBytes("UTF-16BE");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
tp_udhiHead[5] = (byte) (i + 1);
msg.nMsgFormat = 8;
msg.nTPudhi = 1;
if (i != (msgUCS2Count - 1)) {// 不为最后一条
msgContent = Tools.byteAdd(tp_udhiHead, msgUCS2, i
* (maxlonglen - 6), (i + 1) * (maxlonglen - 6));
} else {
msgContent = Tools.byteAdd(tp_udhiHead, msgUCS2, i
* (maxlonglen - 6), msgUCS2Len);
}
if (SYS.Pi("cmpp_sign_stat") == 1)
msg.nPkTotal = msgUCS2Count + 1;
else
msg.nPkTotal = msgUCS2Count;
msg.nPkNumber = i + 1;
}
byte[] sMsgContent = msgContent; // 信息内容
byte send[] = new byte[1024 * 4];
int size = 0;
size += 8;
send[size] = (byte) msg.nPkTotal;
size++;
send[size] = (byte) msg.nPkNumber;
size++;
send[size] = (byte) msg.nNeedReply;
size++;
send[size] = (byte) msg.nMsgLevel;
size++;
System.arraycopy(msg.sServiceId.getBytes(), 0, send, size,
msg.sServiceId.getBytes().length);
size += 10;
send[size] = (byte) msg.nFeeUserType;
size++;
if (null != msg.sFeeMobile && !msg.sFeeMobile.equals("")) {
System.arraycopy(msg.sFeeMobile.getBytes(), 0, send, size,
msg.sFeeType.getBytes().length);
}
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 21;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
size += 32;
send[size] = (byte) msg.nFeeTerminalType;
size++;
}
send[size] = (byte) msg.nTPpId;
size++;
send[size] = (byte) msg.nTPudhi;
size++;
send[size] = (byte) msg.nMsgFormat;
size++;
System.arraycopy(msg.sMsgSrc.getBytes(), 0, send, size,
msg.sMsgSrc.getBytes().length);
size += 6;
System.arraycopy(msg.sFeeType.getBytes(), 0, send, size,
msg.sFeeType.getBytes().length);
size += 2;
System.arraycopy(msg.sFeeCode.getBytes(), 0, send, size,
msg.sFeeCode.getBytes().length);
size += 6;
if (msg.sValidTime != null)
System.arraycopy(msg.sValidTime.getBytes(), 0, send, size,
msg.sValidTime.getBytes().length);
size += 17;
if (msg.sAtTime != null)
System.arraycopy(msg.sAtTime.getBytes(), 0, send, size,
msg.sAtTime.getBytes().length);
size += 17;
System.arraycopy(msg.sSrcId.getBytes(), 0, send, size,
msg.sSrcId.getBytes().length);
size += 21;
send[size] = (byte) msg.nDestUsrTl;
size++;
System.arraycopy(msg.sDestTerminalId.getBytes(), 0, send, size,
msg.sDestTerminalId.length());
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 21;
e
94a1
lse if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
size += 32;
send[size] = (byte) msg.nDestTerminalType;
size++;
}
send[size] = (byte) sMsgContent.length;
size++;
System.arraycopy(sMsgContent, 0, send, size, sMsgContent.length);
size += sMsgContent.length;
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 8;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
System.arraycopy(msg.linkId.getBytes(), 0, send, size,
msg.linkId.getBytes().length);
size += 20;
}
byte[] msgBytes = new byte[size];
System.arraycopy(send, 0, msgBytes, 0, size);
byte[] submit = new byte[CMPPPara.HEAD_LEN + size];
msg.totalLength = CMPPPara.HEAD_LEN + size;
System.arraycopy(Tools.int2byte(msg.totalLength), 0, submit, 0, 4);
System.arraycopy(Tools.int2byte(msg.commandID), 0, submit, 4, 4);
System.arraycopy(Tools.int2byte(msg.sequenceID), 0, submit, 8, 4);
System.arraycopy(msgBytes, 0, submit, CMPPPara.HEAD_LEN, size);
send(submit);
try {
Thread.sleep(1000 / SYS.Pi("smsPackageLimit"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
****************************************************************************************
以上为CMPP协议的核心源码内容,如果需要可以联系QQ:63573260,加我时注明来源。
public int connect() {
int stat = CMPPPara.CMPP_ERROR;
try {
CMPPConnectMessage cMsg = new CMPPConnectMessage();
String time = CTime.getTime(CTime.MMDDhhmmss);
byte[] user = SYS.Ps("cmpp_icp_id").getBytes();
byte[] auth = SYS.Ps("cmpp_icp_auth").getBytes();
byte[] timeStamp = time.getBytes();
byte abyte2[] = new byte[100];
System.arraycopy(user, 0, abyte2, 0, user.length);
int k = user.length + 9;
System.arraycopy(auth, 0, abyte2, k, auth.length);
k += auth.length;
System.arraycopy(timeStamp, 0, abyte2, k, 10);
k += 10;
byte auths[] = new byte[k];
System.arraycopy(abyte2, 0, auths, 0, k);
MD5 md5 = new MD5();
byte md5Msg[] = md5.getMD5ofBytes(auths, auths.length);
stat = login(cMsg, md5Msg, time);
if (stat != CMPPPara.CMPP_SUCCESS) {
log.warn("登录CMPP网关发生错误,错误码:" + stat);
close();
MonitorPara.isLogin = false;
} else {
log.warn("登录CMPP网关成功");
MonitorPara.isLogin = true;
}
} catch (Exception ex) {
ex.printStackTrace();
stat = CMPPPara.CONNECT_INIT_ERROR;
log.warn("CMPP:connect()-" + ex.toString());
close();
MonitorPara.isLogin = false;
}
return stat;
}
二:登录验证
private int login(CMPPConnectMessage cMsg, byte[] auth, String time)
throws IOException, Exception {
int stat = CMPPPara.CMPP_ERROR;
int length = 0;
byte[] inMsg = new byte[100];
System.arraycopy(Tools.int2byte(cMsg.commandID), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(cMsg.sequenceID), 0, inMsg, 8, 4);
length = CMPPPara.HEAD_LEN;
System.arraycopy(SYS.Ps("cmpp_icp_id").getBytes(), 0, inMsg, length,
SYS.Ps("cmpp_icp_id").getBytes().length);
length += SYS.Ps("cmpp_icp_id").getBytes().length;
System.arraycopy(auth, 0, inMsg, length, auth.length);
length += auth.length;
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
inMsg[length] = (byte) 0x20;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
inMsg[length] = (byte) 0x30;
}
length++;
System.arraycopy(Tools.int2byte(Integer.parseInt(time)), 0, inMsg,
length, 4);
length += 4;
cMsg.totalLength = length;
System.arraycopy(Tools.int2byte(cMsg.totalLength), 0, inMsg, 0, 4);
byte[] send = new byte[length];
System.arraycopy(inMsg, 0, send, 0, length);
socket = cmsm.getSocket();
socket.setSoTimeout(CMPPPara.READ_TIMEOUT);
log.warn("获取CMPP网关SOCKET连接成功");
send(send);
CMPPConnectMessageResp rMsg = result();
if (rMsg.commandID == CMPPCommand.COMMAND_CMPP_CONNECT_RESP)
stat = rMsg.nStatus;
return stat;
}
三:发送心跳包(长链接需要保持在线)
public void activeTest() {
CMPPActiveTestMessage test = new CMPPActiveTestMessage();
try {
byte[] send = new byte[12];
System.arraycopy(Tools.int2byte(test.totalLength), 0, send, 0, 4);
System.arraycopy(Tools.int2byte(test.commandID), 0, send, 4, 4);
System.arraycopy(Tools.int2byte(test.sequenceID), 0, send, 8, 4);
send(send);
} catch (IOException e) {
log.warn("与CMPP网关握手异常:" + test.sequenceID);
}
}
四:发送下行短信
public void submit(CMPPSubmitMessage msg) {
int maxlonglen = 140;
byte[] tp_udhiHead = new byte[6];
byte[] msgUCS2 = null;
try {
msgUCS2 = msg.sMsgContent.getBytes("UTF-16BE");
//System.out.println("msgUCS2:"+msgUCS2);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int msgUCS2Len = msgUCS2.length;// 长短信长度
int msgUCS2Count;
int sign = 1;
if (msgUCS2Len <= maxlonglen) {
msgUCS2Count = 1;
sign = 0;
} else {
msgUCS2Count = returnSmsSendNum(msg.sMsgContent);
tp_udhiHead[0] = 0x05;
tp_udhiHead[1] = 0x00;
tp_udhiHead[2] = 0x03;
tp_udhiHead[3] = 0x0A;
tp_udhiHead[4] = (byte) msgUCS2Count;
tp_udhiHead[5] = 0x01;
}
for (int i = 0; i < msgUCS2Count; i++) {
msg.sequenceID = SEQ.getSequenceID();
byte[] msgContent = null;
if (sign == 0) {
try {
msg.nMsgFormat = 8;
msgContent = msg.sMsgContent.getBytes("UTF-16BE");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
tp_udhiHead[5] = (byte) (i + 1);
msg.nMsgFormat = 8;
msg.nTPudhi = 1;
if (i != (msgUCS2Count - 1)) {// 不为最后一条
msgContent = Tools.byteAdd(tp_udhiHead, msgUCS2, i
* (maxlonglen - 6), (i + 1) * (maxlonglen - 6));
} else {
msgContent = Tools.byteAdd(tp_udhiHead, msgUCS2, i
* (maxlonglen - 6), msgUCS2Len);
}
if (SYS.Pi("cmpp_sign_stat") == 1)
msg.nPkTotal = msgUCS2Count + 1;
else
msg.nPkTotal = msgUCS2Count;
msg.nPkNumber = i + 1;
}
byte[] sMsgContent = msgContent; // 信息内容
byte send[] = new byte[1024 * 4];
int size = 0;
size += 8;
send[size] = (byte) msg.nPkTotal;
size++;
send[size] = (byte) msg.nPkNumber;
size++;
send[size] = (byte) msg.nNeedReply;
size++;
send[size] = (byte) msg.nMsgLevel;
size++;
System.arraycopy(msg.sServiceId.getBytes(), 0, send, size,
msg.sServiceId.getBytes().length);
size += 10;
send[size] = (byte) msg.nFeeUserType;
size++;
if (null != msg.sFeeMobile && !msg.sFeeMobile.equals("")) {
System.arraycopy(msg.sFeeMobile.getBytes(), 0, send, size,
msg.sFeeType.getBytes().length);
}
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 21;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
size += 32;
send[size] = (byte) msg.nFeeTerminalType;
size++;
}
send[size] = (byte) msg.nTPpId;
size++;
send[size] = (byte) msg.nTPudhi;
size++;
send[size] = (byte) msg.nMsgFormat;
size++;
System.arraycopy(msg.sMsgSrc.getBytes(), 0, send, size,
msg.sMsgSrc.getBytes().length);
size += 6;
System.arraycopy(msg.sFeeType.getBytes(), 0, send, size,
msg.sFeeType.getBytes().length);
size += 2;
System.arraycopy(msg.sFeeCode.getBytes(), 0, send, size,
msg.sFeeCode.getBytes().length);
size += 6;
if (msg.sValidTime != null)
System.arraycopy(msg.sValidTime.getBytes(), 0, send, size,
msg.sValidTime.getBytes().length);
size += 17;
if (msg.sAtTime != null)
System.arraycopy(msg.sAtTime.getBytes(), 0, send, size,
msg.sAtTime.getBytes().length);
size += 17;
System.arraycopy(msg.sSrcId.getBytes(), 0, send, size,
msg.sSrcId.getBytes().length);
size += 21;
send[size] = (byte) msg.nDestUsrTl;
size++;
System.arraycopy(msg.sDestTerminalId.getBytes(), 0, send, size,
msg.sDestTerminalId.length());
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 21;
e
94a1
lse if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
size += 32;
send[size] = (byte) msg.nDestTerminalType;
size++;
}
send[size] = (byte) sMsgContent.length;
size++;
System.arraycopy(sMsgContent, 0, send, size, sMsgContent.length);
size += sMsgContent.length;
if (SYS.Pi("cmpp_sp_ver") == 2)// cmpp2.0
size += 8;
else if (SYS.Pi("cmpp_sp_ver") == 3) {// cmpp3.0
System.arraycopy(msg.linkId.getBytes(), 0, send, size,
msg.linkId.getBytes().length);
size += 20;
}
byte[] msgBytes = new byte[size];
System.arraycopy(send, 0, msgBytes, 0, size);
byte[] submit = new byte[CMPPPara.HEAD_LEN + size];
msg.totalLength = CMPPPara.HEAD_LEN + size;
System.arraycopy(Tools.int2byte(msg.totalLength), 0, submit, 0, 4);
System.arraycopy(Tools.int2byte(msg.commandID), 0, submit, 4, 4);
System.arraycopy(Tools.int2byte(msg.sequenceID), 0, submit, 8, 4);
System.arraycopy(msgBytes, 0, submit, CMPPPara.HEAD_LEN, size);
send(submit);
try {
Thread.sleep(1000 / SYS.Pi("smsPackageLimit"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
****************************************************************************************
以上为CMPP协议的核心源码内容,如果需要可以联系QQ:63573260,加我时注明来源。
相关文章推荐
- SMGP网关协议核心代码 java版本
- java在线支付---09,10,11,12_在线支付_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- java实现网上在线支付--09,10,11,12_分析易宝支付网关的应答协议与处理代码,完成用于处理支付响应的Servlet的初步编写和调试,完成处理支付网关响应结果的Servlet,支付实现
- Java核心代码(四)native方法
- Java核心代码(五) 实用代码块
- 移动网关CMPP 2.0-3.0协议归档
- 用maven来创建scala和java项目代码环境(图文详解)(Intellij IDEA(Ultimate版本)、Intellij IDEA(Community版本)和Scala IDEA for Eclipse皆适用)(博主推荐)
- MongoDB-JAVA-Driver 3.2版本常用代码全整理(4) - 地理空间索引
- Java代码检查工具PMD发布 4.2.6 版本
- Java 线程池框架核心代码分析
- 关于Java核心技术书中的代码点和代码单元是怎么理解的呢?
- 《Java高手真经_应用框架卷_Java_Web核心框架》所用软件、使用版本、下载地址列表:
- Java核心代码例程
- 想找一个java版本的twitter的IdWorker的代码
- Java线程池框架核心代码解析
- 微信开发从入门到精通教程大全 资料大全 java和php版本;教程文档、代码、视频 微信商城实例
- java学习之旅02--计算机语言发展史,Java历史,Java核心优势,Java三大版本的区别
- Java核心代码(二)Class loader
- java与 mysql6.0版本连接代码
- 【Java TCP/IP Socket】构建和解析自定义协议消息(含代码)