namespace AsiaINFO.SMS.CMPP2 { using System; using System.Text; public class CMPP_DELIVER { private int _BodyLength; private string _Dest_Id; private MessageHeader _Header; private string _Msg_Content; private uint _Msg_Fmt; private ulong _Msg_Id; private uint _Msg_Length; private uint _Registered_Delivery; private byte[] _Report; private string _Reserved; private string _Service_Id; private string _Src_terminal_Id; private uint _TP_pid; private uint _TP_udhi; public const int FixedBodyLength = 0x61; public CMPP_DELIVER(byte[] bytes) { int srcOffset = 0; byte[] dst = new byte[12]; Buffer.BlockCopy(bytes, 0, dst, 0, 12); this._Header = new MessageHeader(dst); srcOffset += 12; dst = new byte[8]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); Array.Reverse(dst); this._Msg_Id = BitConverter.ToUInt64(dst, 0); string str = null; srcOffset += 8; dst = new byte[0x15]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); str = Encoding.ASCII.GetString(dst).Trim(); if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } this._Dest_Id = str; srcOffset += 0x15; dst = new byte[10]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); str = Encoding.ASCII.GetString(dst).Trim(); if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } this._Service_Id = str; srcOffset += 10; this._TP_pid = bytes[srcOffset++]; this._TP_udhi = bytes[srcOffset++]; this._Msg_Fmt = bytes[srcOffset++]; dst = new byte[0x15]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); str = Encoding.ASCII.GetString(dst).Trim(); if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } if (((str != null) && (str.Length > 11)) && (str.Substring(0, 2) == "86")) { str = str.Substring(2, str.Length - 2); } this._Src_terminal_Id = str; srcOffset += 0x15; this._Registered_Delivery = bytes[srcOffset++]; this._Msg_Length = bytes[srcOffset++]; dst = new byte[this._Msg_Length]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); if (this._Registered_Delivery == 1) { this._Msg_Content = ""; this._Report = new byte[this._Msg_Length]; Buffer.BlockCopy(dst, 0, this._Report, 0, this._Report.Length); } else { switch (this._Msg_Fmt) { case 8: this._Msg_Content = Encoding.BigEndianUnicode.GetString(dst).Trim(); goto Label_0276; case 15: this._Msg_Content = Encoding.GetEncoding("gb2312").GetString(dst).Trim(); goto Label_0276; } this._Msg_Content = Encoding.ASCII.GetString(dst).ToString(); } Label_0276: srcOffset += (int) this._Msg_Length; dst = new byte[8]; Buffer.BlockCopy(bytes, srcOffset, dst, 0, dst.Length); str = Encoding.ASCII.GetString(dst).Trim(); if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } this._Reserved = str; } public byte[] ToBytes() { byte[] bytes; switch (this._Msg_Fmt) { case 8: bytes = Encoding.BigEndianUnicode.GetBytes(this._Msg_Content); break; case 15: bytes = Encoding.GetEncoding("gb2312").GetBytes(this._Msg_Content); break; default: bytes = Encoding.ASCII.GetBytes(this._Msg_Content); break; } this._Msg_Length = (uint) bytes.Length; this._BodyLength = 0x61 + ((int) this._Msg_Length); byte[] dst = new byte[12 + this._BodyLength]; int dstOffset = 0; byte[] array = null; this._Header = new MessageHeader((uint) (12 + this._BodyLength), CMPP_Command_Id.CMPP_DELIVER, 0); dstOffset += 12; array = new byte[8]; array = BitConverter.GetBytes(this._Msg_Id); Array.Reverse(array); Buffer.BlockCopy(array, 0, dst, dstOffset, array.Length); dstOffset += 8; array = new byte[0x15]; array = Encoding.ASCII.GetBytes(this._Dest_Id); Buffer.BlockCopy(array, 0, dst, dstOffset, array.Length); dstOffset += 0x15; array = new byte[10]; array = Encoding.ASCII.GetBytes(this._Service_Id); Buffer.BlockCopy(array, 0, dst, dstOffset, array.Length); dstOffset += 10; dst[dstOffset++] = (byte) this._TP_pid; dst[dstOffset++] = (byte) this._TP_udhi; dst[dstOffset++] = (byte) this._Msg_Fmt; array = new byte[0x15]; array = Encoding.ASCII.GetBytes(this._Src_terminal_Id); Buffer.BlockCopy(array, 0, dst, dstOffset, array.Length); dstOffset += 0x15; dst[dstOffset++] = (byte) this._Registered_Delivery; dst[dstOffset++] = (byte) this._Msg_Length; Buffer.BlockCopy(bytes, 0, dst, dstOffset, bytes.Length); dstOffset += (int) this._Msg_Length; return dst; } public override string ToString() { return ("[\r\n" + this._Header.ToString() + "\r\n" + string.Format("\tMessageBody:\r\n\t\tBodyLength: {0}\r\n\t\tDest_Id: {1}\r\n\t\tReserved: {2}\r\n\t\tMsg_Content: {3}\r\n\t\tMsg_Fmt: {4}\r\n\t\tMsg_Id: {5}\r\n\t\tMsg_Length: {6}\r\n\t\tRegistered_Delivery: {7}\r\n\t\tService_Id: {8}\r\n\t\tSrc_terminal_Id: {9}\r\n\t\tTP_pid: {10}\r\n\t\tTP_udhi: {11}", new object[] { this._BodyLength, this._Dest_Id, this._Reserved, this._Msg_Content, this._Msg_Fmt, this._Msg_Id, this._Msg_Length, this._Registered_Delivery, this._Service_Id, this._Src_terminal_Id, this._TP_pid, this._TP_udhi }) + "\r\n]"); } public int BodyLength { get { return this._BodyLength; } } public string Dest_Id { get { return this._Dest_Id; } } public MessageHeader Header { get { return this._Header; } } public string Msg_Content { get { return this._Msg_Content; } } public uint Msg_Fmt { get { return this._Msg_Fmt; } } public ulong Msg_Id { get { return this._Msg_Id; } } public uint Msg_Length { get { return this._Msg_Length; } } public uint Registered_Delivery { get { return this._Registered_Delivery; } } public byte[] Report { get { return this._Report; } } public string Reserved { get { return this._Reserved; } } public string Service_Id { get { return this._Service_Id; } } public string Src_terminal_Id { get { return this._Src_terminal_Id; } } public uint TP_pid { get { return this._TP_pid; } } public uint TP_udhi { get { return this._TP_udhi; } } } }