void client_Received(object sender, ReceivedEventArgs e)
        {
            var session = Session.Session;
            var s       = e.Stream;

            //// 如果上次还留有数据,复制进去
            //if (session.Stream != null && session.Stream.Position < session.Stream.Length)
            //{
            //    var ms = new MemoryStream();
            //    session.Stream.CopyTo(ms);
            //    s.CopyTo(ms);
            //    s = ms;
            //}
            try
            {
                Process(s, session, session.Remote);

                //// 如果还有剩下,写入数据流,供下次使用
                //if (s.Position < s.Length)
                //    session.Stream = s;
                //else
                //    session.Stream = null;
            }
            catch (Exception ex)
            {
                if (NetHelper.Debug)
                {
                    NetHelper.WriteLog(ex.ToString());
                }

                // 去掉内部异常,以免过大
                if (ex.InnerException != null)
                {
                    ex.SetValue("_innerException", null);
                }
                var msg = new ExceptionMessage()
                {
                    Value = ex
                };
                session.Send(msg.GetStream());

                //// 出错后清空数据流,避免连锁反应
                //session.Stream = null;
            }
        }
Пример #2
0
        /// <summary>收到数据流</summary>
        /// <param name="session"></param>
        /// <param name="stream"></param>
        protected virtual void OnReceive(ISocketSession session, Stream stream)
        {
            try
            {
                Process(stream, session, session.Remote);

                //// 如果还有剩下,写入数据流,供下次使用
                //if (stream.Position < stream.Length)
                //{
                //    var ms = new MemoryStream();
                //    stream.CopyTo(ms);
                //    ms.Position = 0;
                //    session.Stream = ms;
                //}
                //else
                //    session.Stream = null;
            }
            catch (Exception ex)
            {
                if (NetHelper.Debug)
                {
                    NetHelper.WriteLog(ex.ToString());
                }

                // 去掉内部异常,以免过大
                if (ex.InnerException != null)
                {
                    ex.SetValue("_innerException", null);
                }
                var msg = new ExceptionMessage()
                {
                    Value = ex
                };
                //session.Send(msg.GetStream());
                OnSend(msg.GetStream());

                //// 出错后清空数据流,避免连锁反应
                //session.Stream = null;
            }
        }