/// <summary> /// Handles server side background activities. /// </summary> private void OnServerBkTimer() { DateTime now; SessionKeepAliveMsg keepAliveMsg; // Send periodic keep-alives back to the client as long as // the session is still running. if (!IsRunning) { return; } // If the session is async and has exceeded its lifespan // then stop sending keep-alives. now = SysTime.Now; if (base.IsAsync && now >= base.TTD) { IsRunning = false; return; } // Send a keep-alive if it's time. if (now >= nextKeepAlive) { nextKeepAlive = now + keepAliveInterval; keepAliveMsg = new SessionKeepAliveMsg(base.SessionInfo.KeepAliveTime); keepAliveMsg._SessionID = base.SessionID; base.Router.SendTo(base.ClientEP, keepAliveMsg); } }
public void Msg_Clone_SessionKeepAliveMsg() { SessionKeepAliveMsg msgIn, msgOut; Msg.ClearTypes(); Msg.LoadTypes(Assembly.GetExecutingAssembly()); msgOut = new SessionKeepAliveMsg(TimeSpan.FromMinutes(77)); msgIn = (SessionKeepAliveMsg)msgOut.Clone(); Assert.IsNotNull(msgIn); Assert.AreEqual(msgOut.SessionTTL, msgIn.SessionTTL); TestBaseCloning(msgOut); }
public void Msg_Serialize_SessionKeepAliveMsg() { SessionKeepAliveMsg msgIn, msgOut; EnhancedStream es = new EnhancedMemoryStream(); Msg.ClearTypes(); Msg.LoadTypes(Assembly.GetExecutingAssembly()); msgOut = new SessionKeepAliveMsg(TimeSpan.FromMinutes(77)); Msg.Save(es, msgOut); es.Seek(0, SeekOrigin.Begin); msgIn = (SessionKeepAliveMsg)Msg.Load(es); Assert.IsNotNull(msgIn); Assert.AreEqual(msgOut.SessionTTL, msgIn.SessionTTL); }
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; } } } }