ConnectionBase.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using Best.HTTP.HostSetting;
  3. using Best.HTTP.Shared;
  4. using Best.HTTP.Shared.Logger;
  5. using Best.HTTP.Shared.PlatformSupport.Threading;
  6. namespace Best.HTTP.Hosts.Connections
  7. {
  8. /// <summary>
  9. /// Abstract base class for concrete connection implementation (HTTP/1, HTTP/2, WebGL, File).
  10. /// </summary>
  11. public abstract class ConnectionBase : IDisposable
  12. {
  13. #region Public Properties
  14. /// <summary>
  15. /// The address of the server that this connection is bound to.
  16. /// </summary>
  17. public HostKey HostKey { get; protected set; }
  18. /// <summary>
  19. /// The state of this connection.
  20. /// </summary>
  21. public HTTPConnectionStates State { get; internal set; }
  22. /// <summary>
  23. /// If the State is HTTPConnectionStates.Processing, then it holds a HTTPRequest instance. Otherwise it's null.
  24. /// </summary>
  25. public HTTPRequest CurrentRequest { get; internal set; }
  26. /// <summary>
  27. /// How much the connection kept alive after its last request processing.
  28. /// </summary>
  29. public virtual TimeSpan KeepAliveTime { get; protected set; }
  30. public virtual bool CanProcessMultiple { get { return false; } }
  31. /// <summary>
  32. /// Number of assigned requests to process.
  33. /// </summary>
  34. public virtual int AssignedRequests { get { return this.State != HTTPConnectionStates.Initial && this.State != HTTPConnectionStates.Free ? 1 : 0; } }
  35. /// <summary>
  36. /// Maximum number of assignable requests.
  37. /// </summary>
  38. public virtual int MaxAssignedRequests { get; } = 1;
  39. /// <summary>
  40. /// When we start to process the current request. It's set after the connection is established.
  41. /// </summary>
  42. public DateTime StartTime { get; protected set; }
  43. public Uri LastProcessedUri { get; protected set; }
  44. public DateTime LastProcessTime { get; protected set; }
  45. public LoggingContext Context { get; protected set; }
  46. #endregion
  47. #region Privates
  48. protected bool IsThreaded;
  49. #endregion
  50. public ConnectionBase(HostKey hostKey)
  51. :this(hostKey, true)
  52. {}
  53. public ConnectionBase(HostKey hostKey, bool threaded)
  54. {
  55. this.HostKey = hostKey;
  56. this.State = HTTPConnectionStates.Initial;
  57. this.LastProcessTime = HTTPManager.CurrentFrameDateTime; // DateTime.Now;
  58. // By default we assume an HTTP/1 connection, but in the HTTP-Over-TCP-Connection the request handlers will decide its value.
  59. this.KeepAliveTime = HTTPManager.PerHostSettings.Get(this.HostKey.Host).HTTP1ConnectionSettings.MaxConnectionIdleTime;
  60. this.IsThreaded = threaded;
  61. this.Context = new LoggingContext(this);
  62. this.Context.Add("HostKey", this.HostKey.ToString());
  63. }
  64. internal virtual void Process(HTTPRequest request)
  65. {
  66. if (State == HTTPConnectionStates.Processing)
  67. throw new Exception("Connection already processing a request! " + this.ToString());
  68. this.State = HTTPConnectionStates.Processing;
  69. this.CurrentRequest = request;
  70. this.LastProcessedUri = this.CurrentRequest.CurrentUri;
  71. if (IsThreaded)
  72. {
  73. ThreadedRunner.RunLongLiving(ThreadFunc);
  74. }
  75. else
  76. ThreadFunc();
  77. }
  78. protected virtual void ThreadFunc()
  79. {
  80. }
  81. public ShutdownTypes ShutdownType { get; protected set; }
  82. /// <summary>
  83. /// Called when the plugin shuts down immediately.
  84. /// </summary>
  85. public virtual void Shutdown(ShutdownTypes type)
  86. {
  87. this.ShutdownType = type;
  88. }
  89. #region Dispose Pattern
  90. public void Dispose()
  91. {
  92. Dispose(true);
  93. GC.SuppressFinalize(this);
  94. }
  95. protected virtual void Dispose(bool disposing)
  96. {
  97. }
  98. ~ConnectionBase()
  99. {
  100. Dispose(false);
  101. }
  102. #endregion
  103. public override string ToString()
  104. {
  105. return string.Format("[{0}:{1}]", this.Context.Hash, this.HostKey.ToString());
  106. }
  107. }
  108. }