SGIP协议之Submit

    /// <summary>
    /// 功能:消息打包
    /// 时间:2010年12月28日
    /// </summary>
    public class Submit
    {
        private int bodyLength;

        public int BodyLength
        {
            get { return bodyLength; }
            set { bodyLength = value; }
        }

        private readonly int Length = 21
                                        + 21
                                        + 1
                                       // + 21
                                        + 5
                                        + 10
                                        + 1
                                        + 6
                                        + 6
                                        + 1
                                        + 1
                                        + 1
                                        + 16
                                        + 16
                                        + 1
                                        + 1
                                        + 1
                                        + 1
                                        + 1
                                        + 4
                                        + 8;
                                      
                                       
        private MessageHeader msgHeader;

        /// <summary>
        /// 消息头
        /// </summary>
        public MessageHeader MsgHeader
        {
            get { return msgHeader; }
            set { msgHeader = value; }
        }

        private string sPNumber;

        /// <summary>
        /// SP的接入号码
        /// </summary>
        public string SPNumber
        {
            get { return sPNumber; }
            set { sPNumber = value; }
        }

        private string chargeNumber;

        /// <summary>
        /// 付费号码,手机号码前加“86”国别标志;
        /// 当且仅当群发且对用户收费时为空;如果为空,
        /// 则该条短消息产生的费用由UserNumber代表的用户支付;
        /// 如果为全零字符串“000000000000000000000”,
        /// 表示该条短消息产生的费用由SP支付。
        /// </summary>
        public string ChargeNumber
        {
            get { return chargeNumber; }
            set { chargeNumber = value; }
        }
        private uint userCount;

        /// <summary>
        /// 接收短消息的手机数量,取值范围1至100
        /// </summary>
        public uint UserCount
        {
            get { return userCount; }
            set { userCount = value; }
        }

        private string[] userNumber;

        /// <summary>
        /// 接收该短消息的手机号,
        /// 该字段重复UserCount指定的次数,
        /// 手机号码前加“86”国别标志
        /// </summary>
        public string[] UserNumber
        {
            get { return userNumber; }
            set { userNumber = value; }
        }
        private string corpId;

        /// <summary>
        /// 企业代码,取值范围0-99999
        /// </summary>
        public string CorpId
        {
            get { return corpId; }
            set { corpId = value; }
        }
        private string serviceType;

        /// <summary>
        /// 业务代码,由SP定义
        /// </summary>
        public string ServiceType
        {
            get { return serviceType; }
            set { serviceType = value; }
        }
        private uint feeType;

        /// <summary>
        /// 计费类型
        /// </summary>
        public uint FeeType
        {
            get { return feeType; }
            set { feeType = value; }
        }
        private string feeValue;

        /// <summary>
        /// 取值范围0-99999,
        /// 该条短消息的收费值,
        /// 单位为分,
        /// 由SP定义对于包月制收费的用户,
        /// 该值为月租费的值
        /// </summary>
        public string FeeValue
        {
            get { return feeValue; }
            set { feeValue = value; }
        }
        private string givenValue;

        /// <summary>
        /// 取值范围0-99999,
        /// 赠送用户的话费,
        /// 单位为分,由SP定义,
        /// 特指由SP向用户发送广告时的赠送话费
        /// </summary>
        public string GivenValue
        {
            get { return givenValue; }
            set { givenValue = value; }
        }
        private uint agentFlag;

        /// <summary>
        /// 代收费标志,0:应收;1:实收
        /// </summary>
        public uint AgentFlag
        {
            get { return agentFlag; }
            set { agentFlag = value; }
        }
        private uint morelatetoMTFlag;

        /// <summary>
        /// 引起MT消息的原因0-MO点播引起的第一条MT消息;
        /// 1-MO点播引起的非第一条MT消息;
        /// 2-非MO点播引起的MT消息;
        /// 3-系统反馈引起的MT消息。
        /// </summary>
        public uint MorelatetoMTFlag
        {
            get { return morelatetoMTFlag; }
            set { morelatetoMTFlag = value; }
        }
        private uint priority;

        /// <summary>
        /// 优先级0-9从低到高,默认为0
        /// </summary>
        public uint Priority
        {
            get { return priority; }
            set { priority = value; }
        }
        private string expireTime;

        /// <summary>
        /// 短消息寿命的终止时间,
        /// 如果为空,表示使用短消息中心的缺省值。
        /// 时间内容为16个字符,格式为”yymmddhhmmsstnnp” ,
        /// 其中“tnnp”取固定值“032+”,
        /// 即默认系统为北京时间
        /// </summary>
        public string ExpireTime
        {
            get { return expireTime; }
            set { expireTime = value; }
        }
        private string scheduleTime;

        /// <summary>
        /// 短消息定时发送的时间,
        /// 如果为空,表示立刻发送该短消息。
        /// 时间内容为16个字符,格式为“yymmddhhmmsstnnp” ,
        /// 其中“tnnp”取固定值“032+”,
        /// 即默认系统为北京时间
        /// </summary>
        public string ScheduleTime
        {
            get { return scheduleTime; }
            set { scheduleTime = value; }
        }
        private uint reportFlag;

        /// <summary>
        /// 状态报告标记
        /// 0-该条消息只有最后出错时要返回状态报告
        /// 1-该条消息无论最后是否成功都要返回状态报告
        /// 2-该条消息不需要返回状态报告
        /// 3-该条消息仅携带包月计费信息,不下发给用户,要返回状态报告
        /// 其它-保留
        /// 缺省设置为0
        /// </summary>
        public uint ReportFlag
        {
            get { return reportFlag; }
            set { reportFlag = value; }
        }
        private uint tP_pid;

        /// <summary>
        /// GSM协议类型
        /// </summary>
        public uint TP_pid
        {
            get { return tP_pid; }
            set { tP_pid = value; }
        }
        private uint tP_udhi;

        /// <summary>
        /// GSM协议类型
        /// </summary>
        public uint TP_udhi
        {
            get { return tP_udhi; }
            set { tP_udhi = value; }
        }
        private uint messageCoding;

        /// <summary>
        /// 短消息的编码格式。
        /// 0:纯ASCII字符串
        /// 3:写卡操作
        /// 4:二进制编码
        /// 8:UCS2编码
        /// 15: GBK编码
        /// 其它参见GSM3.38第4节:SMS Data Coding Scheme
        /// </summary>
        public uint MessageCoding
        {
            get { return messageCoding; }
            set { messageCoding = value; }
        }
        private uint messageType;

        /// <summary>
        /// 信息类型:0-短消息信息其它:待定
        /// </summary>
        public uint MessageType
        {
            get { return messageType; }
            set { messageType = value; }
        }
        private uint messageLength;

        /// <summary>
        /// 短消息的长度
        /// </summary>
        public uint MessageLength
        {
            get { return messageLength; }
            set { messageLength = value; }
        }
        private string messageContent;

        /// <summary>
        /// 短消息的内容
        /// </summary>
        public string MessageContent
        {
            get { return messageContent; }
            set
            {
                messageContent = value;
               // SetHeader();
            }
        }
        private string reserve;

        /// <summary>
        /// 保留,扩展用
        /// </summary>
        public string Reserve
        {
            get { return reserve; }
            set { reserve = value; }
        }

        private byte[] msg_Content_Bytes;

        /// <summary>
        /// 消息内容转化成的数组
        /// </summary>
        public byte[] Msg_Content_Bytes
        {
            get { return msg_Content_Bytes; }
            set { msg_Content_Bytes = value; }
        }

        private byte[] sequenceNumber;

        public byte[] SequenceNumber
        {
            get { return sequenceNumber; }
            set { sequenceNumber = value; }
        }

        public Submit(byte[] sequenceNumber)
        {
            this.sequenceNumber = sequenceNumber;
        }

        private void SetHeader()
        {
            switch (this.messageCoding)
            {
                case 8:
                    msg_Content_Bytes = Encoding.BigEndianUnicode.GetBytes(messageContent);
                    break;
                case 15: //gb2312
                    msg_Content_Bytes = Encoding.GetEncoding("gb2312").GetBytes(messageContent);
                    break;
                case 0: //ascii
                case 3: //短信写卡操作
                case 4: //二进制信息
                default:
                    msg_Content_Bytes = Encoding.ASCII.GetBytes(messageContent);
                    break;
            }
            this.messageLength = (uint)msg_Content_Bytes.Length;
            this.BodyLength = (int)(Length + 21 * userNumber.Length + this.messageLength);
            this.msgHeader = new MessageHeader((uint)(MessageHeader.HeaderLength + this.bodyLength), (CommandID)CommandID.SUBMIT, sequenceNumber);

        }

        /// <summary>
        /// 转成byte[]数组
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            SetHeader();
            int i = 0;
            //header
            byte[] bytes = new byte[MessageHeader.HeaderLength + this.bodyLength];
            byte[] buffer = this.msgHeader.ToBytes();
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);

            i = i + buffer.Length;
            //spnumber
            buffer = BIConvert.String2Bytes(this.sPNumber);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);

            i = i + 21;

            //chargenumber
            buffer = BIConvert.String2Bytes(this.chargeNumber);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);

            i = i + 21;
            //Usercount
            bytes[i++] = (byte)this.userCount;
           
            //userNumber
            for (int k = 0; k < userCount; k++)
            {
                buffer = BIConvert.String2Bytes(userNumber[k]);
                Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
                i = i + 21;
            }

            //corpid
            buffer = BIConvert.String2Bytes(this.corpId);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 5;

            //servicetype
            buffer = BIConvert.String2Bytes(this.serviceType);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 10;

            //feetype
            bytes[i++] = (byte)this.feeType;

            //feevalue
            buffer = BIConvert.String2Bytes(feeValue);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 6;

            //givennumber
            buffer = BIConvert.String2Bytes(givenValue);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 6;

            //agentflag
            bytes[i++] = (byte)this.agentFlag;
            //morelatetomtflag
            bytes[i++] = (byte)this.morelatetoMTFlag;
            //priority
            bytes[i++] = (byte)this.priority;

            //expiretime
            buffer = BIConvert.String2Bytes(this.expireTime);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 16;

            //scheduletime
            buffer = BIConvert.String2Bytes(this.scheduleTime);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 16;

            //reportflag
            bytes[i++] = (byte)this.reportFlag;

            //tp_pid
            bytes[i++] = (byte)this.tP_pid;
            //tp_udhi
            bytes[i++] = (byte)this.tP_udhi;
            //mesagecoding
            bytes[i++] = (byte)this.messageCoding;
            //mesagetype
            bytes[i++] = (byte)this.messageType;

            //msglength
            buffer = BIConvert.Int2Bytes(this.messageLength);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 4;

            //msgtypecontent
            buffer = msg_Content_Bytes;
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + buffer.Length;

            //reserve

            buffer = BIConvert.String2Bytes(this.reserve);
            Util.ByteCopyToByte(buffer, 0, bytes, i, buffer.Length);
            i = i + 8;

            return bytes;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值