public static void RecordSendData(MessageData data) { if (SendDatas == null) { SendDatas = new Dictionary <string, MessageData>(new avoidException()); } SendDatas.Add(HexCode.GetString(data.CompleteData), data); }
public static string RecordRecvData(MessageData data, Action <int> whenLostData) { if (RecvDatas == null) { RecvDatas = new Dictionary <string, MessageData>(new avoidException()); } RecvDatas.Add(HexCode.GetString(data.CompleteData), data); return(GetCompareResult(whenLostData)); }
private void Button_Click(object sender, RoutedEventArgs e) { byte[] data = HexCode.GetHex(toSendData.Text); if (data == null) { MessageBox.Show("Data is empty"); return; } _sender.SendMessage(new MessageData(LegalMessageType.MessageTypesDictionaryWithName["c7"], HexCode.GetHex(toSendData.Text)), (str) => { }); }
/// <summary> /// 加载报文类型 /// </summary> /// <returns>类型名称</returns> public static bool LoadTypes(Action <string[]> action) { /*********************************************************** * 1. 读取文件夹中所有JSON文件 * 2. 使用FileHandler.ReadJson()读取出内容,若读取错误, * 输出错误消息 * 3. 若读取出的类型不为空,则将其置入字典中,并将类型 * 名称添加到返回字符串数组。 * 4. 返回读取到的类型名称。 **********************************************************/ List <string> typeNames = new List <string>(); if (Directory.Exists(@"./MessageTypes")) { foreach (string path in Directory.GetFiles(@"./MessageTypes/", @"*.json")) { MessageType type = FileHandler.ReadJson(path, (str) => { Debug.WriteLine(str); }); if (type != null) { if (_messageTypesDictionaryWithHead == null) { _messageTypesDictionaryWithHead = new ConcurrentDictionary <string, MessageType>(); } if (_messageTypesDictionaryWithName == null) { _messageTypesDictionaryWithName = new ConcurrentDictionary <string, MessageType>(); } if ((!_messageTypesDictionaryWithHead.ContainsKey(HexCode.GetString(type.Head))) && (!_messageTypesDictionaryWithName.ContainsKey(type.TypeName))) { _messageTypesDictionaryWithHead.TryAdd(HexCode.GetString(type.Head), type); _messageTypesDictionaryWithName.TryAdd(type.TypeName, type); typeNames.Add(type.TypeName); } } } } if (typeNames.Count == 0) { return(false); } action(typeNames.ToArray()); return(true); }
/// <summary> /// 导出报文类型文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ExportJSON(object sender, RoutedEventArgs e) { if (_controls.TypeFoot != "" && _controls.TypeHead != "" && _controls.TypeName != "") { type = new MessageType { MessageLength = _controls.TypeLength, TypeFoot = Encryption.AESEncrypter.Encrypt(HexCode.GetHex(_controls.TypeFoot), _controls.TypeName), TypeFootLength = HexCode.GetHex(_controls.TypeFoot).Length, TypeHead = Encryption.AESEncrypter.Encrypt(HexCode.GetHex(_controls.TypeHead), _controls.TypeName), TypeHeadLength = HexCode.GetHex(_controls.TypeFoot).Length, TypeName = _controls.TypeName, }; FileHandler.WriteJson(@"./MessageTypes/" + type.TypeName + ".json", type, (message) => { SnackbarThree.MessageQueue.Enqueue(message); }); } }
/// <summary> /// 识别原始数据中的报文 /// </summary> /// <param name="data"></param> /// <param name="valuePairs"></param> /// <param name="receivedMessageCallback"></param> public static void Split(byte[] data, ConcurrentDictionary <string, MessageType> valuePairs, Action <MessageData> receivedMessageCallback) { int startIndex; if (_currentDataLength == 0)//若无缓存数据 { #region 报文头部识别 Dictionary <string, int> pairs = new Dictionary <string, int>(); //新 < 报文头, 头部位置索引值 > 键值对 foreach (MessageType mt in valuePairs.Values) //读取所有类型 { //监测数据中是否存在报文头 int refIndex = HexCode.GetString(data).IndexOf(HexCode.GetString(mt.Head), 0); if (refIndex != -1) { pairs.Add(HexCode.GetString(mt.Head), refIndex);//将报文头位置索引值和报文头添加到键值对中 } } if (pairs.Count != 0) { var keyValuePairs = from pair in pairs orderby pair.Value ascending select pair; _currentType = valuePairs[keyValuePairs.First().Key]; //使用第一个头 } else { return; } startIndex = pairs[HexCode.GetString(_currentType.Head)] / 3 + _currentType.TypeHeadLength; #endregion _currentDataCollection = new Stack <byte>(); } else { startIndex = 0; } int readLength = 0;//已读取长度 for (; _currentDataLength + readLength < _currentType.MessageLength + _currentType.TypeFootLength; readLength++) { if (startIndex + readLength >= data.Length) { break; } _currentDataCollection.Push(data[startIndex + readLength]); } _currentDataLength = _currentDataCollection.Count(); if (_currentDataLength == _currentType.MessageLength + _currentType.TypeFootLength) { List <byte> popFoot = new List <byte>(); for (int i = 0; i < _currentType.TypeFootLength; i++) { popFoot.Add(_currentDataCollection.Pop()); //弹出尾部 } if (popFoot.SequenceEqual(_currentType.Foot)) //监测尾部是否是合法数据 { //合法数据,回调传回消息 receivedMessageCallback(new MessageData(_currentType, _currentDataCollection.Reverse().ToArray())); _currentType = null; _currentDataCollection = null; _currentDataLength = 0; } else { _currentType = null; _currentDataCollection = null; _currentDataLength = 0; } if (readLength + startIndex < data.Length)//若还有过剩数据,则进行下一次拼接 { List <byte> anotherData = data.ToList(); lock (anotherData) { anotherData.RemoveRange(0, readLength + startIndex); } Split(anotherData.ToArray(), valuePairs, receivedMessageCallback); } } }