Пример #1
0
        private DateTime nextKeepAlive;         // Time to next keep-alive transmission (SYS)

        /// <summary>
        /// Starts the server session initialized with InitServer().
        /// </summary>
        public override void StartServer()
        {
            SessionKeepAliveMsg keepAliveMsg;
            bool     exceptionThrown = false;
            DateTime now             = SysTime.Now;

            try
            {
                base.IsRunning = true;
                base.TTD       = DateTime.MaxValue;
                this.query     = base.ServerInitMsg;

                // Send the first keep alive and schedule the next.

                keepAliveInterval = TimeSpan.FromTicks(base.SessionInfo.KeepAliveTime.Ticks / 3);
                nextKeepAlive     = now + keepAliveInterval;

                keepAliveMsg            = new SessionKeepAliveMsg(base.SessionInfo.KeepAliveTime);
                keepAliveMsg._SessionID = base.SessionID;

                base.Router.SendTo(base.ClientEP, keepAliveMsg);

                // This simply drops through to the message handler.  Unhandled
                // exceptions will cause an Ack to be sent in response to the
                // original message.

                base.Method.Invoke(base.Target, new object[] { base.ServerInitMsg });
            }
            catch (TargetInvocationException eInvoke)
            {
                var e   = eInvoke.InnerException;
                var ack = new Ack();

                exceptionThrown       = true;
                ack.Exception         = e.Message;
                ack.ExceptionTypeName = e.GetType().FullName;

                base.Router.ReplyTo(query, ack);
            }
            catch (Exception e)
            {
                var ack = new Ack();

                exceptionThrown       = true;
                ack.Exception         = e.Message;
                ack.ExceptionTypeName = e.GetType().FullName;

                base.Router.ReplyTo(query, ack);
            }
            finally
            {
                if (!base.IsAsync || exceptionThrown)
                {
                    base.IsRunning = false;

                    if (base.CacheEnable)
                    {
                        base.TTD = now + base.Router.SessionCacheTime;
                        base.SessionManager.OnFinished(this);
                    }
                    else
                    {
                        base.TTD = now;
                    }
                }
                else
                {
                    if (base.SessionInfo.MaxAsyncKeepAliveTime == TimeSpan.MaxValue)
                    {
                        base.TTD = DateTime.MaxValue;
                    }
                    else
                    {
                        base.TTD = SysTime.Now + SessionInfo.MaxAsyncKeepAliveTime;
                    }
                }
            }
        }