Example #1
0
        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>
        public void Process()
        {
            var context = Context;
            try
            {
                var s = context.Request.InputStream;
                if (s == null || s.Position >= s.Length)
                {
                    var d = context.Request.Url.Query;
                    if (!String.IsNullOrEmpty(d))
                    {
                        if (d.StartsWith("?")) d = d.Substring(1);
                        s = new MemoryStream(DataHelper.FromHex(d));
                    }
                }
                //var msg = Message.Read(s);
                //var msg = Message.Read(Context.Request.InputStream);

                // 支持多消息
                while (s.Position < s.Length) Process(Message.Read(s));
            }
            catch (Exception ex)
            {
                // 去掉内部异常,以免过大
                if (ex.InnerException != null) FieldInfoX.SetValue(ex, "_innerException", null);
                var msg = new ExceptionMessage() { Value = ex };
                var data = msg.GetStream().ReadBytes();
                context.Response.BinaryWrite(data);
            }
        }
 /// <summary>处理。</summary>
 public void Process()
 {
     var context = Context;
     try
     {
         var s = context.Request.InputStream;
         if (s == null || s.Position >= s.Length)
         {
             var d = context.Request.Url.Query;
             if (!String.IsNullOrEmpty(d))
             {
                 if (d.StartsWith("?")) d = d.Substring(1);
                 s = new MemoryStream(DataHelper.FromHex(d));
             }
         }
         var msg = Message.Read(s);
         //var msg = Message.Read(Context.Request.InputStream);
         Process(msg);
     }
     catch (Exception ex)
     {
         var msg = new ExceptionMessage() { Value = ex };
         var data = msg.GetStream().ReadBytes();
         context.Response.BinaryWrite(data);
     }
 }
Example #4
0
        static MessageHandler()
        {
            Message.Init();

            LoadConfig();

            ExceptionMessage msg = new ExceptionMessage();
            Register(msg.ID, new DefaultMessageHandler(), false);
        }
Example #5
0
 void client_Received(object sender, ReceivedEventArgs e)
 {
     var session = Session;
     try
     {
         var message = Message.Read(e.Stream);
         Process(message, session.Remote);
     }
     catch (Exception ex)
     {
         var msg = new ExceptionMessage() { Value = ex };
         Process(msg, session.Remote);
     }
 }
 void ProcessResponse(AsyncCompletedEventArgs e, Byte[] result)
 {
     if (e.Error != null)
     {
         var msg = new ExceptionMessage() { Value = e.Error };
         Process(msg);
     }
     else if (result == null || result.Length <= 0)
     {
         Process(new NullMessage());
     }
     else
     {
         try
         {
             var ms = new MemoryStream(result);
             var msg = Message.Read(ms);
             Process(msg);
         }
         catch (Exception ex)
         {
             var msg = new ExceptionMessage() { Value = ex };
             Process(msg);
         }
     }
 }
Example #7
0
 /// <summary>
 /// 处理消息
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="option"></param>
 public static void Process(Stream stream, MessageExceptionOption option)
 {
     switch (option)
     {
         case MessageExceptionOption.Ignore:
             try
             {
                 ProcessInternal(stream);
             }
             catch (Exception ex)
             {
                 if (XTrace.Debug) XTrace.WriteException(ex);
             }
             break;
         case MessageExceptionOption.Throw:
             ProcessInternal(stream);
             break;
         case MessageExceptionOption.SaveAsMessage:
             try
             {
                 ProcessInternal(stream);
             }
             catch (Exception ex)
             {
                 ExceptionMessage message = new ExceptionMessage(ex);
                 message.Serialize(stream);
             }
             break;
         default:
             break;
     }
 }