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); } } }
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); } }