private IList <byte[]> InternalReceivePackage(SocketAsyncEventArgsEx saeaEx) { IList <byte[]> listBytes; if (this.Server.ServerConfig.StartReceiveDataFliter) { #region byte[] data = new byte[saeaEx.DataLength]; Buffer.BlockCopy(saeaEx.ReceiveBuffer, saeaEx.InitOffset, data, 0, data.Length); IRunDevice dev = InternalCheckCodeDevice(data); if (dev != null) { listBytes = InternalFliterData(saeaEx, dev); } else { listBytes = InternalFliterData(saeaEx, null); } #endregion } else { listBytes = InternalFliterData(saeaEx, null); } return(listBytes); }
private IList <byte[]> InternalFliterData(SocketAsyncEventArgsEx saeaEx, IRunDevice dev) { if (dev != null && dev.Protocol != null && dev.Protocol.ReceiveFilter != null) { IList <byte[]> listBytes = saeaEx.Get(dev.Protocol.ReceiveFilter); if (listBytes != null && listBytes.Count > 0) { LastActiveTime = DateTime.Now; } return(listBytes); } else { IList <byte[]> listBytes = new List <byte[]>(); byte[] data = saeaEx.Get(); if (data.Length > 0) { LastActiveTime = DateTime.Now; listBytes.Add(data); } return(listBytes); } }
private void ProcessFliterData(IReceivePackage dataPackage, SocketAsyncEventArgsEx saeaEx, IRunDevice dev) { if (dev == null || dev.Protocol == null || dev.Protocol.ReceiveFilter == null) { if (dataPackage.ListBytes == null) { dataPackage.ListBytes = new List <byte[]>(); } byte[] data = saeaEx.Get(); if (data.Length > 0) { LastActiveTime = DateTime.Now; } dataPackage.ListBytes.Add(data); } else { dataPackage.DeviceCode = dev.DeviceParameter.DeviceCode; IList <byte[]> listBytes = saeaEx.Get(dev.Protocol.ReceiveFilter); if (listBytes != null && listBytes.Count > 0) { LastActiveTime = DateTime.Now; } dataPackage.ListBytes = listBytes; } }
private void ProcessReceive(SocketAsyncEventArgs e) { ISocketSession socketSession = (ISocketSession)e.UserToken; if (socketSession != null && socketSession.Client != null) { try { if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { SocketAsyncEventArgsEx saeaEx = (SocketAsyncEventArgsEx)e; if (saeaEx.NextOffset >= saeaEx.InitOffset + saeaEx.Capacity) { saeaEx.Reset(); } saeaEx.DataLength += saeaEx.BytesTransferred; IList <byte[]> listBytes = InternalReceivePackage(saeaEx); IList <IRequestInfo> listRequestInfos = RequestInfo.ConvertBytesToRequestInfos(Key, Channel, listBytes); if (this.Server.ServerConfig.StartCheckPackageLength) { InternalReceivePackageData(listRequestInfos); } OnSocketReceiveData(new ReceivePackage(RemoteIP, RemotePort, listRequestInfos)); //没有经过检测数据包长度 saeaEx.SetBuffer(saeaEx.ReceiveBuffer, saeaEx.NextOffset, saeaEx.InitOffset + saeaEx.Capacity - saeaEx.NextOffset); bool willRaiseEvent = socketSession.Client.ReceiveAsync(this.SocketAsyncProxy.SocketReceiveEventArgsEx); if (!willRaiseEvent) { ProcessReceive(saeaEx); } } else { OnCloseSocket(); } } catch (SocketException ex) { OnCloseSocket(); this.Server.Logger.Error(true, ex.Message); } catch (Exception ex) { this.Server.Logger.Error(true, ex.Message); } } }
/// <summary> /// 异步读取固定长度数据 /// </summary> /// <param name="dataLength"></param> /// <param name="cts"></param> /// <returns></returns> protected override Task <byte[]> ReadAsync(int dataLength, CancellationTokenSource cts) { Task <byte[]> t = Task.Factory.StartNew(() => { int readLength = dataLength; List <byte> readBytes = new List <byte>(dataLength); while (readLength > 0) { if (cts.IsCancellationRequested) { break; } SocketAsyncEventArgsEx saeaEx = SocketAsyncProxy.SocketReceiveEventArgsEx; int oneLength = dataLength <= this.Client.ReceiveBufferSize ? dataLength : this.Client.ReceiveBufferSize; try { if (!this.IsDisposed && this.Client != null) { oneLength = this.Client.Receive(saeaEx.ReceiveBuffer, saeaEx.InitOffset, oneLength, SocketFlags.None); } else { break; } } catch (SocketException ex) { OnCloseSocket(); this.Server.Logger.Error(true, "", ex); break; } saeaEx.DataLength += oneLength; byte[] data = saeaEx.Get(); readBytes.AddRange(data); readLength -= oneLength; } return(readBytes.ToArray()); }, cts.Token); return(t); }
private void ProcessReceive(SocketAsyncEventArgs e) { ISocketSession socketSession = (ISocketSession)e.UserToken; if (socketSession != null && socketSession.Client != null) { try { if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { SocketAsyncEventArgsEx saeaEx = (SocketAsyncEventArgsEx)e; if (saeaEx.NextOffset >= saeaEx.InitOffset + saeaEx.Capacity) { saeaEx.Reset(); } saeaEx.DataLength += saeaEx.BytesTransferred; IList <byte[]> listBytes = InternalReceivePackage(saeaEx); IReceivePackage rp = InternalReceivePackageData(listBytes); saeaEx.SetBuffer(saeaEx.ReceiveBuffer, saeaEx.NextOffset, saeaEx.InitOffset + saeaEx.Capacity - saeaEx.NextOffset); OnSocketReceiveData(rp); bool willRaiseEvent = socketSession.Client.ReceiveAsync(this.SocketAsyncProxy.SocketReceiveEventArgsEx); if (!willRaiseEvent) { ProcessReceive(saeaEx); } } else { OnCloseSocket(); } } catch (SocketException ex) { OnCloseSocket(); this.Server.Logger.Error(true, ex.Message); } catch (Exception ex) { this.Server.Logger.Error(true, ex.Message); } } }
public SocketAsyncEventArgsProxy(SocketAsyncEventArgsEx saea) { SocketReceiveEventArgsEx = saea; SocketSendEventArgs = new SocketAsyncEventArgs(); }
public override IList <byte[]> Read(IReceiveFilter receiveFilter) { if (!this.IsDisposed) { System.Threading.Thread.Sleep(Server.ServerConfig.NetLoopInterval); if (this.Client != null && this.Client.Connected) { if (this.Client.Poll(10, SelectMode.SelectRead)) { try { SocketAsyncEventArgsEx saeaEx = SocketAsyncProxy.SocketReceiveEventArgsEx; if (saeaEx.NextOffset >= saeaEx.InitOffset + saeaEx.Capacity) { saeaEx.Reset(); } #region int num = this.Client.Receive(saeaEx.ReceiveBuffer, saeaEx.NextOffset, saeaEx.InitOffset + saeaEx.Capacity - saeaEx.NextOffset, SocketFlags.None); if (num <= 0) { throw new SocketException((int)SocketError.HostDown); } else { LastActiveTime = DateTime.Now; saeaEx.DataLength += num; if (receiveFilter == null) { IList <byte[]> listBytes = new List <byte[]>(); listBytes.Add(saeaEx.Get()); return(listBytes); } else { return(saeaEx.Get(receiveFilter)); } } #endregion } catch (SocketException) { OnCloseSocket(); throw new SocketException((int)SocketError.HostDown); } } else { return(null); } } else { OnCloseSocket(); throw new SocketException((int)SocketError.HostDown); } } else { return(null); } }
private void ProcessReceive(SocketAsyncEventArgs e) { ISocketSession socketSession = (ISocketSession)e.UserToken; if (socketSession != null && socketSession.Client != null) { try { if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { SocketAsyncEventArgsEx saeaEx = (SocketAsyncEventArgsEx)e; if (saeaEx.NextOffset >= saeaEx.InitOffset + saeaEx.Capacity) { saeaEx.Reset(); } saeaEx.DataLength += saeaEx.BytesTransferred; IReceivePackage dataPackage = new ReceivePackage(); dataPackage.RemoteIP = this.RemoteIP; dataPackage.RemotePort = this.RemotePort; #region 过滤数据 if (this.Server.ServerConfig.StartReceiveDataFliter) { IRunDevice[] devList = this.Server.DeviceManager.GetDevices(CommunicateType.NET); if (devList != null && devList.Length > 0) { if (this.Server.ServerConfig.ControlMode == ControlMode.Loop || this.Server.ServerConfig.ControlMode == ControlMode.Self || this.Server.ServerConfig.ControlMode == ControlMode.Parallel) { #region byte[] data = new byte[saeaEx.DataLength]; Buffer.BlockCopy(saeaEx.ReceiveBuffer, saeaEx.InitOffset, data, 0, data.Length); IRunDevice dev = null; try { dev = devList.FirstOrDefault(d => d.DeviceParameter.DeviceCode == d.Protocol.GetCode(data)); } catch (Exception ex) { this.Server.Logger.Error(true, ex.Message); } if (dev != null) { ProcessFliterData(dataPackage, saeaEx, dev); } #endregion } else if (this.Server.ServerConfig.ControlMode == ControlMode.Singleton) { ProcessFliterData(dataPackage, saeaEx, devList[0]); } } } else { ProcessFliterData(dataPackage, saeaEx, null); } #endregion saeaEx.SetBuffer(saeaEx.ReceiveBuffer, saeaEx.NextOffset, saeaEx.InitOffset + saeaEx.Capacity - saeaEx.NextOffset); OnSocketReceiveData(dataPackage); bool willRaiseEvent = socketSession.Client.ReceiveAsync(this.SocketAsyncProxy.SocketReceiveEventArgsEx); if (!willRaiseEvent) { ProcessReceive(this.SocketAsyncProxy.SocketReceiveEventArgsEx); } } else { OnCloseSocket(); } } catch (SocketException ex) { OnCloseSocket(); this.Server.Logger.Error(true, ex.Message); } catch (Exception ex) { this.Server.Logger.Error(true, ex.Message); } } }