예제 #1
0
        public static Flags decode(byte flagsByte, SNType type)
        {
            List <Flag> bitMask = new List <Flag>();

            foreach (var flag in Enum.GetValues(typeof(Flag)))
            {
                if ((flagsByte & (int)flag) == (int)flag)
                {
                    bitMask.Add((Flag)flag);
                }
            }

            return(Flags.validateAndCreate(bitMask, type));
        }
예제 #2
0
 public static long GetNextTick(SNType type = SNType.Default)
 {
     lock (dict[type].ObjLock)
     {
         while (true)
         {
             if (dict[type].Ticks + 1 < DateTime.Now.Ticks)
             {
                 return ++dict[type].Ticks;
             }
             Thread.Sleep(1);
         }
     }
 }
예제 #3
0
 public static long GetNextTick(SNType type = SNType.Default)
 {
     lock (dict[type].ObjLock)
     {
         while (true)
         {
             if (dict[type].Ticks + 1 < DateTime.Now.Ticks)
             {
                 return(++dict[type].Ticks);
             }
             Thread.Sleep(1);
         }
     }
 }
예제 #4
0
        private SNObject GetSN(SNType tab)
        {
            SNObject sn = new SNObject();

            using (var tx = new TransactionScope())
            {
                var get_sn_db = getDB0;
                try
                {
                    get_sn_db = getDB0;
                    string tab_name = Enum.GetName(typeof(ProcCore.Business.SNType), tab);
                    var    items    = get_sn_db.i_SN.Single(x => x.sn_type == tab_name);

                    if (items.y == DateTime.Now.Year &&
                        items.m == DateTime.Now.Month &&
                        items.d == DateTime.Now.Day
                        )
                    {
                        int now_max = items.sn_max;
                        now_max++;
                        items.sn_max = now_max;
                    }
                    else
                    {
                        items.y      = DateTime.Now.Year;
                        items.m      = DateTime.Now.Month;
                        items.d      = DateTime.Now.Day;
                        items.sn_max = 1;
                    }

                    get_sn_db.SaveChanges();
                    tx.Complete();

                    sn.y      = items.y;
                    sn.m      = items.m;
                    sn.d      = items.d;
                    sn.sn_max = items.sn_max;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    get_sn_db.Dispose();
                }
            }
            return(sn);
        }
예제 #5
0
        private static Flags validateAndCreate(List <Flag> bitMask, SNType type)
        {
            if (bitMask.Contains(Flag.RESERVED_TOPIC))
            {
                throw new MalformedMessageException("Invalid topic type encoding. TopicType reserved bit must not be encoded");
            }

            Boolean dup          = bitMask.Contains(Flag.DUPLICATE);
            Boolean retain       = bitMask.Contains(Flag.RETAIN);
            Boolean will         = bitMask.Contains(Flag.WILL);
            Boolean cleanSession = bitMask.Contains(Flag.CLEAN_SESSION);

            SNQoS?qos = null;

            if (bitMask.Contains(Flag.QOS_LEVEL_ONE))
            {
                qos = SNQoS.LEVEL_ONE;
            }
            else if (bitMask.Contains(Flag.QOS_2))
            {
                qos = SNQoS.EXACTLY_ONCE;
            }
            else if (bitMask.Contains(Flag.QOS_1))
            {
                qos = SNQoS.AT_LEAST_ONCE;
            }
            else
            {
                qos = SNQoS.AT_MOST_ONCE;
            }

            TopicType?topicType = null;

            if (bitMask.Contains(Flag.SHORT_TOPIC))
            {
                topicType = avps.TopicType.SHORT;
            }
            else if (bitMask.Contains(Flag.ID_TOPIC))
            {
                topicType = avps.TopicType.ID;
            }
            else
            {
                topicType = avps.TopicType.NAMED;
            }

            switch (type)
            {
            case SNType.CONNECT:
                if (dup)
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag");
                }
                if (qos != SNQoS.AT_MOST_ONCE)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag - " + qos);
                }
                if (retain)
                {
                    throw new MalformedMessageException(type + " invalid encoding: retain flag");
                }
                if (!topicType.HasValue || topicType != avps.TopicType.NAMED)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag - " + topicType);
                }
                break;

            case SNType.WILL_TOPIC:
                if (dup)
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag");
                }
                if (qos == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (!topicType.HasValue || topicType != avps.TopicType.NAMED)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag - " + topicType);
                }
                break;

            case SNType.PUBLISH:
                if (qos == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (topicType == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (dup && (qos == SNQoS.AT_MOST_ONCE || qos == SNQoS.LEVEL_ONE))
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag with invalid qos:" + qos);
                }
                break;

            case SNType.SUBSCRIBE:
                if (qos == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (qos == SNQoS.LEVEL_ONE)
                {
                    throw new MalformedMessageException(type + "invalid encoding: qos " + qos);
                }
                if (retain)
                {
                    throw new MalformedMessageException(type + " invalid encoding: retain flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (!topicType.HasValue)
                {
                    throw new MalformedMessageException(type + " invalid encoding: retain flag");
                }
                break;

            case SNType.SUBACK:
                if (dup)
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag");
                }
                if (qos == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (retain)
                {
                    throw new MalformedMessageException(type + " invalid encoding: retain flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (!topicType.HasValue || topicType != avps.TopicType.NAMED)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag");
                }
                break;

            case SNType.UNSUBSCRIBE:
                if (dup)
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag");
                }
                if (qos != SNQoS.AT_MOST_ONCE)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (retain)
                {
                    throw new MalformedMessageException(type + " invalid encoding: retain flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (topicType == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag");
                }
                break;

            case SNType.WILL_TOPIC_UPD:
                if (dup)
                {
                    throw new MalformedMessageException(type + " invalid encoding: dup flag");
                }
                if (qos == null)
                {
                    throw new MalformedMessageException(type + " invalid encoding: qos flag");
                }
                if (will)
                {
                    throw new MalformedMessageException(type + " invalid encoding: will flag");
                }
                if (cleanSession)
                {
                    throw new MalformedMessageException(type + " invalid encoding: cleanSession flag");
                }
                if (!topicType.HasValue || topicType.Value != avps.TopicType.NAMED)
                {
                    throw new MalformedMessageException(type + " invalid encoding: topicIdType flag");
                }
                break;

            default:
                break;
            }
            return(new Flags(dup, qos, retain, will, cleanSession, topicType));
        }
예제 #6
0
        private SNObject GetSN(SNType tab)
        {
            SNObject sn = new SNObject();

            using (var tx = new TransactionScope())
            {
                var get_sn_db = getDB0;
                try
                {
                    get_sn_db = getDB0;
                    string tab_name = Enum.GetName(typeof(ProcCore.Business.SNType), tab);
                    var items = get_sn_db.i_SN.Single(x => x.sn_type == tab_name);

                    if (items.y == DateTime.Now.Year &&
                        items.m == DateTime.Now.Month &&
                        items.d == DateTime.Now.Day
                        )
                    {
                        int now_max = items.sn_max;
                        now_max++;
                        items.sn_max = now_max;
                    }
                    else
                    {
                        items.y = DateTime.Now.Year;
                        items.m = DateTime.Now.Month;
                        items.d = DateTime.Now.Day;
                        items.sn_max = 1;
                    }

                    get_sn_db.SaveChanges();
                    tx.Complete();

                    sn.y = items.y;
                    sn.m = items.m;
                    sn.d = items.d;
                    sn.sn_max = items.sn_max;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    get_sn_db.Dispose();
                }
            }
            return sn;
        }
        public static IByteBuffer encode(SNMessage message)
        {
            int         length = message.getLength();
            IByteBuffer buf    = Unpooled.Buffer(length);

            if (length <= 255)
            {
                buf.WriteByte(length);
            }
            else
            {
                buf.WriteByte(THREE_OCTET_LENGTH_SUFFIX);
                buf.WriteShort(length);
            }
            SNType type = message.getType();

            buf.WriteByte((byte)(int)type);

            switch (type)
            {
            case SNType.ADVERTISE:
                Advertise advertise = (Advertise)message;
                buf.WriteByte(advertise.gwID);
                buf.WriteShort(advertise.duration);
                break;

            case SNType.SEARCHGW:
                SearchGW searchGw = (SearchGW)message;
                buf.WriteByte((byte)(int)searchGw.Radius);
                break;

            case SNType.GWINFO:
                GWInfo gwInfo = (GWInfo)message;
                buf.WriteByte(gwInfo.gwID);
                if (gwInfo.gwAddress != null)
                {
                    buf.WriteBytes(Encoding.UTF8.GetBytes(gwInfo.gwAddress));
                }
                break;

            case SNType.CONNECT:
                SNConnect connect          = (SNConnect)message;
                byte      connectFlagsByte = Flags.encode(false, null, false, connect.WillPresent, connect.CleanSession, null);
                buf.WriteByte(connectFlagsByte);
                buf.WriteByte(connect.ProtocolID);
                buf.WriteShort(connect.Duration);
                buf.WriteBytes(Encoding.UTF8.GetBytes(connect.ClientID));
                break;

            case SNType.CONNACK:
            case SNType.WILL_TOPIC_RESP:
            case SNType.WILL_MSG_RESP:
                ResponseMessage responseMessage = (ResponseMessage)message;
                buf.WriteByte((byte)(int)responseMessage.ReturnCode);
                break;

            case SNType.WILL_TOPIC:
                WillTopic willTopic = (WillTopic)message;
                if (willTopic.Topic != null)
                {
                    byte willTopicFlagsByte = Flags.encode(false, willTopic.Topic.getQos(), willTopic.Retain, false, false, willTopic.Topic.getType());
                    buf.WriteByte(willTopicFlagsByte);
                    buf.WriteBytes(Encoding.UTF8.GetBytes(willTopic.Topic.Value));
                }
                break;

            case SNType.WILL_MSG:
                WillMsg willMsg = (WillMsg)message;
                buf.WriteBytes(willMsg.Content);
                break;

            case SNType.REGISTER:
                Register register = (Register)message;
                buf.WriteShort(register.topicID);
                buf.WriteShort(register.MessageID.Value);
                buf.WriteBytes(Encoding.UTF8.GetBytes(register.TopicName));
                break;

            case SNType.REGACK:
                Regack regack = (Regack)message;
                buf.WriteShort(regack.topicID);
                buf.WriteShort(regack.MessageID.Value);
                buf.WriteByte((byte)(int)regack.code);
                break;

            case SNType.PUBLISH:
                SNPublish publish          = (SNPublish)message;
                byte      publishFlagsByte = Flags.encode(publish.Dup, publish.SnTopic.getQos(), publish.Retain, false, false, publish.SnTopic.getType());
                buf.WriteByte(publishFlagsByte);
                buf.WriteBytes(publish.SnTopic.encode());
                if (publish.MessageID.HasValue)
                {
                    buf.WriteShort(publish.MessageID.Value);
                }
                else
                {
                    buf.WriteShort(0);
                }
                buf.WriteBytes(publish.Content);
                break;

            case SNType.PUBACK:
                SNPuback puback = (SNPuback)message;
                buf.WriteShort(puback.topicID);
                buf.WriteShort(puback.MessageID.Value);
                buf.WriteByte((byte)(int)puback.ReturnCode);
                break;

            case SNType.PUBREC:
            case SNType.PUBREL:
            case SNType.PUBCOMP:
            case SNType.UNSUBACK:
                CountableMessage contableMessage = (CountableMessage)message;
                buf.WriteShort(contableMessage.MessageID.Value);
                break;

            case SNType.SUBSCRIBE:
                SNSubscribe subscribe      = (SNSubscribe)message;
                byte        subscribeFlags = Flags.encode(subscribe.Dup, subscribe.SnTopic.getQos(), false, false, false, subscribe.SnTopic.getType());
                buf.WriteByte(subscribeFlags);
                buf.WriteShort(subscribe.MessageID.Value);
                buf.WriteBytes(subscribe.SnTopic.encode());
                break;

            case SNType.SUBACK:
                SNSuback suback     = (SNSuback)message;
                byte     subackByte = Flags.encode(false, suback.AllowedQos, false, false, false, null);
                buf.WriteByte(subackByte);
                buf.WriteShort(suback.topicID);
                buf.WriteShort(suback.MessageID.Value);
                buf.WriteByte((byte)(int)suback.ReturnCode);
                break;

            case SNType.UNSUBSCRIBE:
                SNUnsubscribe unsubscribe      = (SNUnsubscribe)message;
                byte          unsubscribeFlags = Flags.encode(false, null, false, false, false, unsubscribe.SnTopic.getType());
                buf.WriteByte(unsubscribeFlags);
                buf.WriteShort(unsubscribe.MessageID.Value);
                buf.WriteBytes(unsubscribe.SnTopic.encode());
                break;

            case SNType.PINGREQ:
                if (length > 2)
                {
                    SNPingreq pingreq = (SNPingreq)message;
                    buf.WriteBytes(Encoding.UTF8.GetBytes(pingreq.ClientID));
                }
                break;

            case SNType.DISCONNECT:
                if (length > 2)
                {
                    SNDisconnect disconnect = (SNDisconnect)message;
                    buf.WriteShort(disconnect.Duration);
                }
                break;

            case SNType.WILL_TOPIC_UPD:
                WillTopicUpd willTopicUpd = (WillTopicUpd)message;
                if (willTopicUpd.Topic != null)
                {
                    byte willTopicUpdByte = Flags.encode(false, willTopicUpd.Topic.getQos(), willTopicUpd.Retain, false, false, null);
                    buf.WriteByte(willTopicUpdByte);
                    buf.WriteBytes(Encoding.UTF8.GetBytes(willTopicUpd.Topic.Value));
                }
                break;

            case SNType.WILL_MSG_UPD:
                WillMsgUpd willMsgUpd = (WillMsgUpd)message;
                buf.WriteBytes(willMsgUpd.Content);
                break;

            case SNType.WILL_TOPIC_REQ:
            case SNType.WILL_MSG_REQ:
            case SNType.PINGRESP:
                break;

            case SNType.ENCAPSULATED:
                Encapsulated encapsulated = (Encapsulated)message;

                buf.WriteByte(Controls.encode(encapsulated.radius));
                buf.WriteBytes(Encoding.UTF8.GetBytes(encapsulated.wirelessNodeID));
                buf.WriteBytes(SNParser.encode(encapsulated.message));
                break;

            default:
                break;
            }

            if (type != SNType.ENCAPSULATED && message.getLength() != buf.ReadableBytes)
            {
                throw new MalformedMessageException("invalid message encoding: expected length-" + message.getLength() + ",actual-" + buf.ReadableBytes);
            }

            return(buf);
        }