[fix]Config创建默认配置文件的开关Runtime.CreateConfigOnMissing,仅需对自动创建生效,而不应该阻止用户主动Save
智能大石头 编写于 2024-08-09 00:30:41 石头 提交于 2024-08-10 14:22:24
X
using System;
using System.IO;
using NewLife.Messaging;
using NewLife.Net.Sockets;
using NewLife.Reflection;

namespace NewLife.Net.Common
{
    /// <summary>网络会话消息提供者</summary>
    public class SessionMessageProvider : MessageProvider
    {
        private INetSession _Session;
        /// <summary>网络会话</summary>
        public INetSession Session { get { return _Session; } set { _Session = value; } }

        /// <summary>实例化一个网络会话消息提供者</summary>
        /// <param name="session"></param>
        public SessionMessageProvider(INetSession session)
        {
            Session = session;

            session.Received += new EventHandler<ReceivedEventArgs>(client_Received);
        }

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

        /// <summary>发送数据流。</summary>
        /// <param name="stream"></param>
        protected override void OnSend(Stream stream) { Session.Send(stream); }
    }
}