/// <summary> /// 返回数据 /// </summary> /// <param name="data"></param> public void AddData(TopicData data) { queue.Enqueue(data); if (isRun) { return; } ThreadQueue(); }
/// <summary> /// 解析数据 /// </summary> /// <param name="data"></param> /// <returns></returns> public static TopicData UnPackTopicData(byte[] data) { TopicData topic = new TopicData(); MemoryStream memory = new MemoryStream(data); memory.ReadByte(); byte[] len = new byte[2]; memory.Read(len, 0, 2); short tlen = BitConverter.ToInt16(len, 0); byte[] topicbytes = new byte[tlen]; memory.Read(topicbytes, 0, tlen); string str = Encoding.Default.GetString(topicbytes); byte[] tmp = new byte[data.Length - 2 - tlen]; memory.Read(tmp, 0, tmp.Length); topic.TopicName = str; topic.Data = tmp; return(topic); }
private void ThreadQueue() { isRun = true; Task.Factory.StartNew(() => { TopicData data = null; List <Subscriber> lst = null; while (!queue.IsEmpty) { if (queue.TryDequeue(out data)) { if (dicSubscriber.TryGetValue(data.TopicName, out lst)) { for (int i = 0; i < lst.Count; i++) { lst[i].Call(data.TopicName, data.Data); } } } } isRun = false; }); }