您的位置:首页 > 编程语言 > Java开发

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,加我时注明来源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CMPP2.0
相关文章推荐