您的位置:首页 > 编程语言 > C#

轻松学习C#开发CMPP2.0协议网关-2

2012-04-17 16:28 162 查看
登录协议包定义

消息体包括:

字段名

字节数
属性
描述
Source_Addr

6

Octet String

源地址,此处为SP_Id,即SP的企业代码。

AuthenticatorSource

16

Octet String

用于鉴别源地址。其值通过单向MD5 hash计算得出,表示如下:

AuthenticatorSource =

MD5(Source_Addr+9字节的0 +shared secret+timestamp)

Shared secret
由中国移动与源地址实体事先商定,timestamp格式为:MMDDHHMMSS,即月日时分秒,10位。

Version

1

Unsigned Integer

双方协商的版本号(高位4bit表示主版本号,低位4bit表示次版本号)

Timestamp

4

Unsigned Integer

时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐。

消息体长度为6+16+1+4=27

MobileConnect.cs

/// <summary>
/// 消息流水号,顺序累加,步长为1,循环使用(一对请求和应答消息的流水号必须相同) 消息头使用
/// </summary>
uint sequenceID;

public MobileConnect(uint sequenceid)
{
this.sequenceID = sequenceid;
}

/// <summary>
/// 企业代码
/// </summary>
string sourceAddr;
/// <summary>
/// 企业代码
/// </summary>
public string SourceAddr
{
get { return sourceAddr; }
set { sourceAddr = value; }
}

/// <summary>
/// 双方商定的验证码,类同与密码
/// </summary>
string sharedSecret;

/// <summary>
/// 双方商定的验证码,类同与密码
/// </summary>
public string SharedSecret
{
get
{
return this.sharedSecret;
}
set
{
this.sharedSecret = value;
}
}
/// <summary>
/// 服务器支持的最高版本号,对于3.0的版本,高4bit为3,低4位为0
/// </summary>
uint version;
/// <summary>
/// 服务器支持的最高版本号,对于3.0的版本,高4bit为3,低4位为0
/// </summary>
public uint Version
{
get
{
return this.version;
}
set
{
this.version = value;
}
}
/// <summary>
/// 时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐
/// </summary>
uint timestamp;
/// <summary>
/// 时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐
/// </summary>
public uint Timestamp
{
get
{
return this.timestamp;
}
set
{
this.timestamp = value;
}
}
/// <summary>
/// 消息处理  组合消息
/// </summary>
/// <returns></returns>
public byte[] ToBytes()
{
MobileMessageTitle header = new MobileMessageTitle();
//消息长度
header.MessageContentLength = this.ConnectLength;
header.CommandID = (uint)MobileShareEnum.CommandID.Connect;
header.SequenceID = this.sequenceID;
byte[] rb = new byte[header.MessageLength];//消息头+消息的总长度
byte[] Source_Addr = Tools.StringToByte(this.SourceAddr, "ASCII");//登录消息体参数1
byte[] sharedSecret = Tools.StringToByte(this.SharedSecret, "ASCII");
string st = this.Timestamp.ToString();
if (st.Length == 9) st = "0" + st;
byte[] timestampmd5 = Tools.StringToByte(st, "ASCII");//登录消息体参数4
byte[] timestamp = Tools.UintToUintArray(this.Timestamp);
byte[] zero = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] AuthenticatorSource = new byte[Source_Addr.Length + zero.Length + sharedSecret.Length + timestampmd5.Length];//登录消息体参数2
Array.Copy(Source_Addr, 0, AuthenticatorSource, 0, Source_Addr.Length);
Array.Copy(zero, 0, AuthenticatorSource, Source_Addr.Length, zero.Length);
Array.Copy(sharedSecret, 0, AuthenticatorSource, Source_Addr.Length + zero.Length, sharedSecret.Length);
Array.Copy(timestampmd5, 0, AuthenticatorSource, Source_Addr.Length + zero.Length + sharedSecret.Length, timestampmd5.Length);
int index = 0;
Array.Copy(header.ToBytes(), 0, rb, index, header.MessageTitleLength);//添加消息头
index += (int)header.MessageTitleLength;
Array.Copy(Source_Addr, 0, rb, index, Source_Addr.Length);//添加登录消息体参数1
index += 6;
Array.Copy(Tools.MD5(AuthenticatorSource), 0, rb, index, 16);//登录消息体参数2  经MD5加密
index += 16;
rb[index] = (byte)this.Version;
index += 1;
Array.Copy(Tools.IntChangeNetByte(timestamp), 0, rb, index, 4);
return rb;
}

/// <summary>
/// 登录验证消息体的长度
/// </summary>
public uint ConnectLength
{
get
{
return (6 + 16 + 1 + 4);
}
}


消息头协议包MobileMessageTitle.cs已经定义,登录协议包MobileConnect.cs如何使用如下:

MobileConnect mcont = new MobileConnect(Tools.SeqID);//Tools.SeqID为消息流水号(唯一)

mcont.SharedSecret = 密码;//this.ydpas;

mcont.SourceAddr = 企业编号;//this.ydcode;

mcont.Timestamp = uint.Parse(DateTime.Now.ToString("MMddHHmmss"));

mcont.Version = 0x30;

mcont.ToBytes();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: