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

📄 messagehead.cs

📁 移动MISC1.6正向、反向定制、取消接口
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Cryptography;

    public enum CMPP_Command_Id : uint
        {
            CMPP_CONNECT = 0x00000001,//请求连接
            CMPP_CONNECT_RESP = 0x80000001,//请求连接应答

            CMPP_TERMINATE = 0x00000002,//终止连接
            CMPP_TERMINATE_RESP = 0x80000002,//终止连接应答

            CMPP_SUBMIT = 0x00000004,//提交短信
            CMPP_SUBMIT_RESP = 0x80000004,//提交短信应答

            CMPP_DELIVER = 0x00000005,//短信下发
            CMPP_DELIVER_RESP = 0x80000005,//下发短信应答

            CMPP_QUERY = 0x00000006,//发送短信状态查询
            CMPP_QUERY_RESP = 0x80000006,//发送短信状态查询应答

            CMPP_CANCEL = 0x00000007,//删除短信
            CMPP_CANCEL_RESP = 0x80000007,//删除短信应答

            CMPP_ACTIVE_TEST = 0x00000008,//激活测试
            CMPP_ACTIVE_TEST_RESP = 0x80000008,//激活测试应答

            CMPP_FWD = 0x00000009,//消息前转
            CMPP_FWD_RESP = 0x80000009,//消息前转应答

            CMPP_MT_ROUTE = 0x00000010,//MT路由请求
            CMPP_MT_ROUTE_RESP = 0x80000010,//MT路由请求应答

            CMPP_MO_ROUTE = 0x00000011,//MO路由请求
            CMPP_MO_ROUTE_RESP = 0x80000011,//MO路由请求应答

            CMPP_GET_MT_ROUTE = 0x00000012,//获取MT路由请求
            CMPP_GET_MT_ROUTE_RESP = 0x80000012,//获取MT路由请求应答

            CMPP_MT_ROUTE_UPDATE = 0x00000013,//MT路由更新
            CMPP_MT_ROUTE_UPDATE_RESP = 0x80000013,//MT路由更新应答

            CMPP_MO_ROUTE_UPDATE = 0x00000014,//MO路由更新
            CMPP_MO_ROUTE_UPDATE_RESP = 0x80000014,//MO路由更新应答

            CMPP_PUSH_MT_ROUTE_UPDATE = 0x00000015,//MT路由更新
            CMPP_PUSH_MT_ROUTE_UPDATE_RESP = 0x80000015,//MT路由更新应答

            CMPP_PUSH_MO_ROUTE_UPDATE = 0x00000016,//MO路由更新
            CMPP_PUSH_M0_ROUTE_UPDATE_RESP = 0x80000016,//MO路由更新应答

            CMPP_GET_MO_ROUTE = 0x00000017,//获取MO路由请求
            CMPP_GET_MO_ROUTE_RESP = 0x00000017,//获取MO路由请求应答
        }
        public class Message_Header //消息头
        {
            public const int Length = 4 + 4 + 4;

            public CMPP_Command_Id Command_Id
            {
                get
                {
                    return this._Command_Id;
                }
            }
            public uint Sequence_Id
            {
                get
                {
                    return this._Sequence_Id;
                }
            }
            public uint Total_Length
            {
                get
                {
                    return this._Total_Length;
                }
            }

            private uint _Total_Length;//4 Unsigned Integer 消息总长度(含消息头及消息体)
            private CMPP_Command_Id _Command_Id;//4 Unsigned Integer 命令或响应类型
            private uint _Sequence_Id;//4 Unsigned Integer 消息流水号,顺序累加,步长为1,循环使用(一对请求和应答消息的流水号必须相同)

            public Message_Header(uint Total_Length, CMPP_Command_Id Command_Id, uint Sequence_Id)//发送前的构造函数
            {
                this._Total_Length = Total_Length;
                this._Command_Id = Command_Id;
                this._Sequence_Id = Sequence_Id;
            }
            public Message_Header(byte[] bytes)//参数是数组的构造函数
            {
                byte[] buffer = new byte[4];
                Buffer.BlockCopy(bytes, 0, buffer, 0, buffer.Length);
                Array.Reverse(buffer);
                this._Total_Length = BitConverter.ToUInt32(buffer, 0);

                Buffer.BlockCopy(bytes, 4, buffer, 0, buffer.Length);
                Array.Reverse(buffer);
                this._Command_Id = (CMPP_Command_Id)BitConverter.ToUInt32(buffer, 0);

                Buffer.BlockCopy(bytes, 8, buffer, 0, buffer.Length);
                Array.Reverse(buffer);
                this._Sequence_Id = BitConverter.ToUInt32(buffer, 0);
            }
            public byte[] ToBytes()
            {
                byte[] bytes = new byte[Message_Header.Length];

                byte[] buffer = BitConverter.GetBytes(this._Total_Length);
                Array.Reverse(buffer);
                Buffer.BlockCopy(buffer, 0, bytes, 0, 4);

                buffer = BitConverter.GetBytes((uint)this._Command_Id);
                Array.Reverse(buffer);
                Buffer.BlockCopy(buffer, 0, bytes, 4, 4);

                buffer = BitConverter.GetBytes(this._Sequence_Id);
                Array.Reverse(buffer);
                Buffer.BlockCopy(buffer, 0, bytes, 8, 4);

                return bytes;
            }
            public override string ToString()
            {
                
                return string.Format
                (
                    "\tMessage_Header:\r\n\t\tCommand_Id: {0}\r\n\t\tSequence_Id: {1}\r\n\r\rTotal_Length: {2}"
                    ,this._Command_Id
                    ,this._Sequence_Id
                    ,this._Total_Length
                );
            }
        }
        public class CMPP_CONNECT //:CMPP_Request
        {
            public const int BodyLength = 6 + 16 + 1 + 4;

            private string _Source_Addr;//6 Octet String 源地址,此处为SP_Id,即短信特服号码
            private string _Password;
            private byte[] _AuthenticatorSource;//16 Octet String 用于鉴别地址。其值通过单向MD5 hash计算的出,表示如下:
            //    AuthenticatorSource = 
            //    MD5(Source_Addr + 9个字节的0 + shared secre + timestamp)
            //    Shared secret 由中国移动与公司事先商定,timestamp格式为:MMDDHHMMSS,即月日时分秒,10位。
            private uint _Version;//1 Unsigned Integer 高4bit为3,低4位为0
            private uint _Timestamp;//4 Unsigned Integer 时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐。

            private Message_Header _Header;

            public Message_Header Header
            {
                get
                {
                    return this._Header;
                }
            }

            public byte[] AuthenticatorSource
            {
                get
                {
                    return this._AuthenticatorSource;
                }
            }
            public CMPP_CONNECT
            (string Source_Addr
                , string Password
                , DateTime Timestamp
                , uint Version
                , uint Sequence_Id
            )
            {
                this._Header = new Message_Header(Message_Header.Length + BodyLength, CMPP_Command_Id.CMPP_CONNECT, Sequence_Id);

                this._Source_Addr = Source_Addr;
                this._Password = Password;

                string s = Util.Get_MMDDHHMMSS_String(Timestamp);
                this._Timestamp = UInt32.Parse(s);

                byte[] buffer = new byte[6 + 9 + this._Password.Length + 10];
                Encoding.ASCII.GetBytes(this._Source_Addr).CopyTo(buffer, 0);
                Encoding.ASCII.GetBytes(this._Password).CopyTo(buffer, 6 + 9);
                Encoding.ASCII.GetBytes(s).CopyTo(buffer, 6 + 9 + this._Password.Length);
                
                this._AuthenticatorSource = new MD5CryptoServiceProvider().ComputeHash(buffer, 0, buffer.Length);
                this._Version = Version;
            }
            public byte[] ToBytes()
            {
                int i = 0;
                byte[] bytes = new byte[Message_Header.Length + BodyLength];

                //header 12
                byte[] buffer = this._Header.ToBytes();
                Buffer.BlockCopy(buffer, 0, bytes, i, buffer.Length);

                //Source_Addr 6
                i += Message_Header.Length;
                buffer = Encoding.ASCII.GetBytes(this._Source_Addr);
                Buffer.BlockCopy(buffer, 0, bytes, i, 6);

                //AuthenticatorSource 16
                i += 6;
                buffer = this._AuthenticatorSource;
                Buffer.BlockCopy(buffer, 0, bytes, i, buffer.Length);//16

                //version 1
                i += 16;
                bytes[i++] = (byte)this._Version;

                //Timestamp
                buffer = BitConverter.GetBytes(this._Timestamp);
                Array.Reverse(buffer);
                buffer.CopyTo(bytes, i);

                return (bytes);
            }
            public override string ToString()
            {
                return "[\r\n"
                + this._Header.ToString() + "\r\n"
                + string.Format
                (
                "\tMessageBody:"
                + "\r\n\t\tAuthenticatorSource:{0}"
                + "\r\n\t\tPassword:{1}"
                + "\r\n\t\tSource_Addr:{3}"
                + "\r\n\t\tVersion:{4}"
                , this._AuthenticatorSource
                , this._Password
                , this._Source_Addr
                , this._Timestamp
                , this._Version
                )
                + "\r\n";
            }
        }
    public class CMPP_CONNECT_RESP //: CMPP_Response 
    {
        private Message_Header _Header;
        public const int BodyLength = 4 + 16 + 1;

        private uint _Status;    // 4 Unsigned Integer 状态 
           //   0:正确 
           //   1:消息结构错 
           //   2:非法源地址 
           //   3:认证错 
           //   4:版本太高 
           //   5~:其他错误 
        private byte[] _AuthenticatorISMG; // 16 Octet String ISMG认证码,用于鉴别ISMG。 
           //   其值通过单向MD5 hash计算得出,表示如下: 
           //   AuthenticatorISMG =MD5(Status+AuthenticatorSource+shared secret),Shared secret 由中国移动与源地址
           //   实体事先商定,AuthenticatorSource为源地址实体发送给ISMG的对应消息CMPP_Connect中的值。 
           //   认证出错时,此项为空。 
        private uint _Version;    // 1 Unsigned Integer 服务器支持的最高版本号,对于3.0的版本,高4bit为3,低4位为0 

        public byte[] AuthenticatorISMG
        {
            get
            {
                return this._AuthenticatorISMG;
            }
        }

        public uint Status
        {
            get
            {
                return this._Status;
            }
        }

        public uint Version
        {
            get
            {
                return this._Version;
            }
        }

        public Message_Header Header
        {
            get
            {
                return this._Header;
            }
        }

        public CMPP_CONNECT_RESP(byte[] bytes)
        {
            //header 12 
            int i = 0;
            byte[] buffer = new byte[Message_Header.Length];
            Buffer.BlockCopy(bytes, 0, buffer, 0, buffer.Length);
            this._Header = new Message_Header(buffer);

            //status 4 
            i += Message_Header.Length;
            buffer = new byte[4];
            Buffer.BlockCopy(bytes, i, buffer, 0, buffer.Length);
            Array.Reverse(buffer);
            this._Status = BitConverter.ToUInt32(buffer, 0);

            //AuthenticatorISMG 16 
            i += 4;
            this._AuthenticatorISMG = new byte[16];
            Buffer.BlockCopy(bytes, Message_Header.Length + 4, this._AuthenticatorISMG, 0, this._AuthenticatorISMG.Length);

            //version 
            i += 16;
            this._Version = bytes[i];
        }

        public override string ToString()
        {
            return "[\r\n" 
            + this._Header.ToString() + "\r\n" 
            + string.Format
            (
                "\tMessageBody:" 
                + "\r\n\t\tAuthenticatorISMG: {0}"
                + "\r\n\t\tBodyLength: {1}"
                + "\r\n\t\tStatus: {2}"
                + "\r\n\t\tVersion: {3}"
                ,this._AuthenticatorISMG
                ,CMPP_CONNECT_RESP.BodyLength
                ,this._Status
                ,this._Version
            ) + "\r\n]";
        }
    }
        public class CMPP_SUBMIT
        {
            private int _BodyLength;
            //without _Dest_terminal_Id Msg_Content
            public const int FixedBodyLength = 8
                +1
                +1
                +1
                +1
                +10
                +1
                +32
                +1
                +1
                +1

⌨️ 快捷键说明

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