ConnectionBase.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using BestHTTP.Logger;
  3. namespace BestHTTP.Connections
  4. {
  5. public abstract class ConnectionBase : IDisposable
  6. {
  7. #region Public Properties
  8. /// <summary>
  9. /// The address of the server that this connection is bound to.
  10. /// </summary>
  11. public string ServerAddress { get; protected set; }
  12. /// <summary>
  13. /// The state of this connection.
  14. /// </summary>
  15. public HTTPConnectionStates State { get; internal set; }
  16. /// <summary>
  17. /// If the State is HTTPConnectionStates.Processing, then it holds a HTTPRequest instance. Otherwise it's null.
  18. /// </summary>
  19. public HTTPRequest CurrentRequest { get; internal set; }
  20. /// <summary>
  21. /// How much the connection kept alive after its last request processing.
  22. /// </summary>
  23. public virtual TimeSpan KeepAliveTime { get; protected set; }
  24. public virtual bool CanProcessMultiple { get { return false; } }
  25. /// <summary>
  26. /// When we start to process the current request. It's set after the connection is established.
  27. /// </summary>
  28. public DateTime StartTime { get; protected set; }
  29. public Uri LastProcessedUri { get; protected set; }
  30. public DateTime LastProcessTime { get; protected set; }
  31. internal LoggingContext Context;
  32. #endregion
  33. #region Privates
  34. private bool IsThreaded;
  35. #endregion
  36. public ConnectionBase(string serverAddress)
  37. :this(serverAddress, true)
  38. {}
  39. public ConnectionBase(string serverAddress, bool threaded)
  40. {
  41. this.ServerAddress = serverAddress;
  42. this.State = HTTPConnectionStates.Initial;
  43. this.LastProcessTime = DateTime.Now;
  44. this.KeepAliveTime = HTTPManager.MaxConnectionIdleTime;
  45. this.IsThreaded = threaded;
  46. this.Context = new LoggingContext(this);
  47. this.Context.Add("ServerAddress", serverAddress);
  48. this.Context.Add("Threaded", threaded);
  49. }
  50. internal virtual void Process(HTTPRequest request)
  51. {
  52. if (State == HTTPConnectionStates.Processing)
  53. throw new Exception("Connection already processing a request! " + this.ToString());
  54. StartTime = DateTime.MaxValue;
  55. State = HTTPConnectionStates.Processing;
  56. CurrentRequest = request;
  57. LastProcessedUri = CurrentRequest.CurrentUri;
  58. if (IsThreaded)
  59. PlatformSupport.Threading.ThreadedRunner.RunLongLiving(ThreadFunc);
  60. else
  61. ThreadFunc();
  62. }
  63. protected virtual void ThreadFunc()
  64. {
  65. }
  66. public ShutdownTypes ShutdownType { get; protected set; }
  67. /// <summary>
  68. /// Called when the plugin shuts down immediately.
  69. /// </summary>
  70. public virtual void Shutdown(ShutdownTypes type)
  71. {
  72. this.ShutdownType = type;
  73. }
  74. #region Dispose Pattern
  75. public void Dispose()
  76. {
  77. Dispose(true);
  78. GC.SuppressFinalize(this);
  79. }
  80. protected virtual void Dispose(bool disposing)
  81. {
  82. }
  83. ~ConnectionBase()
  84. {
  85. Dispose(false);
  86. }
  87. #endregion
  88. public override string ToString()
  89. {
  90. return string.Format("[{0}:{1}]", this.GetHashCode(), this.ServerAddress);
  91. }
  92. public virtual bool TestConnection() => true;
  93. }
  94. }