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.IsEmpty) { connecter.ReceiveAsync(readCacheSize); } else { recivceDataHanding(connecter);//如果bufferPool主队列缓存不为空,继续处理数据。 } }
private void sendComplete(IConnectState connecter) { if (connecter.ReadBufferPool.StrCache == null || connecter.ReadBufferPool.StrCache.Length == 0) { connecter.ReceiveAsync(_readCacheSize); } else { recivceDataHanding(connecter);//如果bufferPool主队列缓存不为空,继续处理数据。 } }
/// <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(); } } } } }