最近做一个项目要实现CMPP网关,基于不重复造轮子的原则,查遍开源社区也没有找到相关开源代码。
实在没有办法逼着自己学习已经还给老师的计算机原理,废话不多说,直接贴代码。
信息标识,生成算法如下:
采用64位(8字节)的整数:
- 时间(格式为MMDDHHMMSS,即月日时分秒):bit64~bit39,其中
bit64~bit61:月份的二进制表示;
bit60~bit56:日的二进制表示;
bit55~bit51:小时的二进制表示;
bit50~bit45:分的二进制表示;
bit44~bit39:秒的二进制表示;
- 短信网关代码:bit38~bit17,把短信网关的代码转换为整数填写到该字段中。
- 序列号:bit16~bit1,顺序增加,步长为1,循环使用。
各部分如不能填满,左补零,右对齐。
/// <summary>
/// 信息标识
/// 生成算法如下:
/// 采用64位(8字节)的整数:
/// (1)时间(格式为MMDDHHMMSS,即月日时分秒):bit64 ~bit39,其中
/// bit64~bit61:月份的二进制表示;
/// bit60 ~bit56:日的二进制表示;
/// bit55 ~bit51:小时的二进制表示;
/// bit50 ~bit45:分的二进制表示;
/// bit44 ~bit39:秒的二进制表示;
/// (2)短信网关代码:bit38 ~bit17,把短信网关的代码转换为整数填写到该字段中。
/// (3)序列号:bit16 ~bit1,顺序增加,步长为1,循环使用。
/// 各部分如不能填满,左补零,右对齐。
/// </summary>
public class CmppMsgId
{
private static char[] HEX = "0123456789abcdef".ToCharArray();
private static volatile short _sequenceId = 0;
private static short NextSequenceId { get { return _sequenceId++; } }
private long msgId;
private int month;
private int day;
private int hour;
private int minute;
private int second;
private int gateway;
private int sequence;
private CmppMsgId()
{
}
private CmppMsgId(int gateway)
{
this.gateway = gateway;
this.sequence = NextSequenceId & 0xff;
TimeSetting();
MsgIdSetting();
}
public static CmppMsgId Next(int gateway)
{
return new CmppMsgId(gateway);
}
public static CmppMsgId Parse(string hexMessageId)
{
return Parse(Int64.Parse(hexMessageId));
}
public static CmppMsgId Parse(long msgId)
{
CmppMsgId cmppMsgId = new CmppMsgId();
cmppMsgId.msgId = msgId;
cmppMsgId.month = (int)((msgId >> 60) & 0xf);
cmppMsgId.day = (int)((msgId >> 55) & 0x1f);
cmppMsgId.hour = (int)((msgId >> 50) & 0x1f);
cmppMsgId.minute = (int)((msgId >> 44) & 0x3f);
cmppMsgId.second = (int)((msgId >> 38) & 0x3f);
cmppMsgId.gateway = (int)((msgId >> 16) & 0x3fffff);
cmppMsgId.sequence = (int)(msgId & 0xff);
return cmppMsgId;
}
private void TimeSetting()
{
this.month = DateTime.Now.Month;
this.day = DateTime.Now.Day;
this.hour = DateTime.Now.Hour;
this.minute = DateTime.Now.Minute;
this.second = DateTime.Now.Second;
}
private void MsgIdSetting()
{
msgId |= (long)month << 60;
msgId |= (long)day << 55;
msgId |= (long)hour << 50;
msgId |= (long)minute << 44;
msgId |= (long)second << 38;
msgId |= (long)gateway << 16;
msgId |= (long)sequence & 0xff;
}
public String GetHexMsgId()
{
char[] chs = new char[16];
chs[0] = HEX[(int)((msgId >> 60) & 0xf)];
chs[1] = HEX[(int)((msgId >> 56) & 0xf)];
chs[2] = HEX[(int)((msgId >> 52) & 0xf)];
chs[3] = HEX[(int)((msgId >> 48) & 0xf)];
chs[4] = HEX[(int)((msgId >> 44) & 0xf)];
chs[5] = HEX[(int)((msgId >> 40) & 0xf)];
chs[6] = HEX[(int)((msgId >> 36) & 0xf)];
chs[7] = HEX[(int)((msgId >> 32) & 0xf)];
chs[8] = HEX[(int)((msgId >> 28) & 0xf)];
chs[9] = HEX[(int)((msgId >> 24) & 0xf)];
chs[10] = HEX[(int)((msgId >> 20) & 0xf)];
chs[11] = HEX[(int)((msgId >> 16) & 0xf)];
chs[12] = HEX[(int)((msgId >> 12) & 0xf)];
chs[13] = HEX[(int)((msgId >> 8) & 0xf)];
chs[14] = HEX[(int)((msgId >> 4) & 0xf)];
chs[15] = HEX[(int)(msgId & 0xf)];
return new String(chs);
}
public DateTime EventTime
{
get
{
return new DateTime(DateTime.Now.Year, month, day, hour, minute, second);
}
}
public ulong MsgId
{
get
{
return (ulong)msgId;
}
}
public int Month
{
get
{
return month;
}
}
public int Day
{
get
{
return day;
}
}
public int Hour
{
get
{
return hour;
}
}
public int Minute
{
get
{
return minute;
}
}
public int Second
{
get
{
return second;
}
}
public int Gateway
{
get
{
return gateway;
}
}
public uint Sequence
{
get
{
return (uint)sequence;
}
}
public override int GetHashCode()
{
int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + gateway;
result = prime * result + hour;
result = prime * result + (int)(msgId ^ (msgId >> 32));
result = prime * result + minute;
result = prime * result + month;
result = prime * result + second;
result = prime * result + sequence;
return result;
}
public override bool Equals(object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj is CmppMsgId))
return false;
CmppMsgId other = (CmppMsgId)obj;
if (day != other.day)
return false;
if (gateway != other.gateway)
return false;
if (hour != other.hour)
return false;
if (msgId != other.msgId)
return false;
if (minute != other.minute)
return false;
if (month != other.month)
return false;
if (second != other.second)
return false;
if (sequence != other.sequence)
return false;
return true;
}
public override string ToString()
{
return "MsgId: " + GetHexMsgId() +
", month: " + month +
", day: " + day +
", hour: " + hour +
", minute: " + minute +
", second: " + second +
", gateway: " + gateway +
", sequence: " + sequence;
}
}

1万+

被折叠的 条评论
为什么被折叠?



