private void disconnectEvent(IConnectState connecter) { var subscribeItem = _subscribeGroup.Find(s => s.ConnectState == connecter); subscribeItem?.RemoveAll(); _subscribeGroup.Remove(subscribeItem); }
private void recivceDataHanding(IConnectState connecter) { lock (locker) { var bufferPool = connecter.ReadBufferPool; //将字节数组转换为字符串 bufferPool.ConvertToStrCache(); var headIndex = bufferPool.StrCache.IndexOf(_headStr); if (headIndex != -1) { var endIndex = bufferPool.StrCache.IndexOf(_endStr, headIndex); if (endIndex != -1) { var length = endIndex - headIndex + 4; var bodyStr = bufferPool.StrCache.Substring(headIndex, length); bodyStr = bodyStr.Replace("<", ""); var bodyArray = bodyStr.Split(new char[] { '>' }, StringSplitOptions.RemoveEmptyEntries); var sentCache = bufferRely(bodyArray, connecter); if (sentCache != null) { ////发送次数 //int sendCount =(int) Math.Ceiling((double)sentCache.Length / 60000); ////发送缓存 //byte[] sendBuffer = new byte[60000]; //for(int i = 0; i < sendCount; i++) //{ // connecter.Send(sentCache); //} connecter.SendAsync(sentCache); } else { connecter.ReceiveAsync(_readCacheSize); } if (bufferPool.StrCache.Length >= endIndex + 4) { bufferPool.StrCache = bufferPool.StrCache.Substring(endIndex + 4); } else { bufferPool.StrCache = ""; } } else { connecter.ReceiveAsync(_readCacheSize); } } else { connecter.ReceiveAsync(_readCacheSize); } } }
private void sendComplete(IConnectState connecter) { if (connecter.ReadBufferPool.StrCache == null || connecter.ReadBufferPool.StrCache.Length == 0) { connecter.ReceiveAsync(_readCacheSize); } else { recivceDataHanding(connecter);//如果bufferPool主队列缓存不为空,继续处理数据。 } }
private void sendComplete(IConnectState connecter) { if (connecter.ReadBufferPool.IsEmpty) { connecter.ReceiveAsync(readCacheSize); } else { recivceDataHanding(connecter);//如果bufferPool主队列缓存不为空,继续处理数据。 } }
/// <summary> /// 返回所有点的元数据 /// </summary> /// <returns></returns> private byte[] metaDataRely(IConnectState connecter) { string funCodeStr = "<1F>"; byte[] result = null; List <string> strList = new List <string>(); ASCIIEncoding encoding = new ASCIIEncoding(); foreach (var metaData in _pointMeDataList.Data) { strList.Add(metaData.Name); strList.Add(metaData.ValueType); strList.Add(metaData.IsVirtual.ToString()); strList.Add(metaData.Length.ToString()); } if (strList.Count != 0) { string resultStr; while (strList.Count >= 5000) { resultStr = ""; for (int i = 0; i < 5000; i++) { resultStr = string.Concat(resultStr, "<", strList[i], ">"); } strList.RemoveRange(0, 5000); if ((connecter.Send(encoding.GetBytes(string.Concat(_headStr, funCodeStr, resultStr, _endStr))) != 1)) { return(null); } } resultStr = ""; foreach (var s in strList) { resultStr = string.Concat(resultStr, "<", s, ">"); } if (resultStr != "") { connecter.Send(encoding.GetBytes(string.Concat(_headStr, funCodeStr, resultStr, _endStr))); } } else { funCodeStr = "<2F>"; result = encoding.GetBytes(string.Concat(_headStr, funCodeStr, "<Point Meta data is Null>", _endStr)); } return(result); }
private byte[] bufferRely(string[] source, IConnectState connecter) { byte[] result = null; if (source.Length >= 3) { string[] data = new string[source.Length - 3]; byte funCode; if (byte.TryParse(source[1], NumberStyles.HexNumber, null as IFormatProvider, out funCode)) { Array.Copy(source, 2, data, 0, data.Length); switch (funCode) { case 0x01: result = readCodeRely(data); break; case 0x02: result = writeCodeRely(data); break; case 0x05: result = subscribeCodeRely(data, connecter); break; case 0x06: result = cancelSubCodeRely(data, connecter); break; case 0x0F: result = metaDataRely(connecter); break; default: break; } } } return(result); }
/// <summary> /// Modbus报文处理函数 /// </summary> /// <param name="connecter">连接对象</param> private void recivceDataHanding(IConnectState connecter) { lock (locker) { var bufferPool = connecter.ReadBufferPool; bufferPool.HeadLength = headLength; if (bufferPool.HeadBuffer == null) { connecter.ReceiveAsync(readCacheSize); } else { bufferPool.BodyLength = bufferPool.HeadBuffer[5]; if (bufferPool.BodyBuffer == null) { connecter.ReceiveAsync(readCacheSize); } else { byte[] relyBuffer = bufferRely(bufferPool.BodyBuffer); if (relyBuffer != null) { bufferPool.SendBuffer = new byte[headLength + relyBuffer.Length]; Array.Copy(bufferPool.HeadBuffer, 0, bufferPool.SendBuffer, 0, headLength - 1); bufferPool.SendBuffer[5] = (byte)relyBuffer.Length; Array.Copy(relyBuffer, 0, bufferPool.SendBuffer, headLength, relyBuffer.Length); connecter.SendAsync(bufferPool.SendBuffer); bufferPool.clear(); } else { connecter.ReceiveAsync(readCacheSize); bufferPool.clear(); } } } } }
public SubscribeItem(IConnectState connectState, ILog log) { _connectState = connectState; _pointNames = new List <PointNameGroup>(); _log = log; }
/// <summary> /// 返回取消订阅数据 /// </summary> /// <param name="data"></param> /// <param name="connecter"></param> /// <returns></returns> private byte[] cancelSubCodeRely(string[] source, IConnectState connecter) { byte[] result = null; List <string> errorInfoList = new List <string>(); string errorInfo = null; ASCIIEncoding encoding = new ASCIIEncoding(); if (source.Length % 2 == 0) { for (int i = 0; i < source.Length; i = i + 2) { var pointName = source[i]; var pointType = source[i + 1]; var pointNameArrary = pointName.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries); byte index = 0; string type; if (pointNameArrary.Length >= 2) { byte.TryParse(pointNameArrary[1].Replace("]", ""), out index); } if (_pointMeDataList.Find(pointNameArrary[0], out type)) { if (pointType.ToLower() == type) { PointNameGroup nameGroup = new PointNameGroup { PointName = pointNameArrary[0], Index = index, Type = type }; //反馈第一次订阅值 //取消添加订阅 SubscribeItem item; if (_subscribeGroup.Exists(s => s.ConnectState.Equals(connecter))) { item = _subscribeGroup.Find(s => s.ConnectState.Equals(connecter)); if (!item.Remove(pointName)) { errorInfo = string.Concat("the point name:", pointName, " not Subscrible"); } } else { errorInfo = string.Concat("the point name:", pointName, " not Subscrible"); } } else { errorInfo = string.Concat("the point name:", pointName, " type : ", pointType, " type not match"); errorInfoList.Add(errorInfo); } } else { errorInfo = string.Concat("Not found the point name:", pointName); errorInfoList.Add(errorInfo); } } } else { errorInfoList.Add("steam length error"); } if (errorInfoList.Count != 0) { string funCodeStr = "<26>"; errorInfo = ""; foreach (var s in errorInfoList) { errorInfo = string.Concat(errorInfo, "<", s, ">"); } errorInfo = string.Concat(_headStr, funCodeStr, "<", errorInfo, ">", _endStr); connecter.Send(encoding.GetBytes(errorInfo)); } else { string funCodeStr = "<16>"; result = encoding.GetBytes(string.Concat(_headStr, funCodeStr, _endStr)); } return(result); }