HostConnection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using BestHTTP.Connections;
  4. using BestHTTP.Extensions;
  5. using BestHTTP.Logger;
  6. namespace BestHTTP.Core
  7. {
  8. public enum HostProtocolSupport : byte
  9. {
  10. Unknown = 0x00,
  11. HTTP1 = 0x01,
  12. HTTP2 = 0x02
  13. }
  14. /// <summary>
  15. /// A HostConnection object manages the connections to a host and the request queue.
  16. /// </summary>
  17. public sealed class HostConnection
  18. {
  19. public HostDefinition Host { get; private set; }
  20. public string VariantId { get; private set; }
  21. public HostProtocolSupport ProtocolSupport { get; private set; }
  22. public DateTime LastProtocolSupportUpdate { get; private set; }
  23. public int QueuedRequests { get { return this.Queue.Count; } }
  24. public LoggingContext Context { get; private set; }
  25. private List<ConnectionBase> Connections = new List<ConnectionBase>();
  26. private List<HTTPRequest> Queue = new List<HTTPRequest>();
  27. public HostConnection(HostDefinition host, string variantId)
  28. {
  29. this.Host = host;
  30. this.VariantId = variantId;
  31. this.Context = new LoggingContext(this);
  32. this.Context.Add("Host", this.Host.Host);
  33. this.Context.Add("VariantId", this.VariantId);
  34. }
  35. internal void AddProtocol(HostProtocolSupport protocolSupport)
  36. {
  37. this.LastProtocolSupportUpdate = DateTime.UtcNow;
  38. var oldProtocol = this.ProtocolSupport;
  39. if (oldProtocol != protocolSupport)
  40. {
  41. this.ProtocolSupport = protocolSupport;
  42. HTTPManager.Logger.Information(typeof(HostConnection).Name, string.Format("AddProtocol({0}) - changing from {1} to {2}", this.VariantId, oldProtocol, protocolSupport), this.Context);
  43. HostManager.Save();
  44. }
  45. if (protocolSupport == HostProtocolSupport.HTTP2)
  46. TryToSendQueuedRequests();
  47. }
  48. internal HostConnection Send(HTTPRequest request)
  49. {
  50. var conn = GetNextAvailable(request);
  51. if (conn != null)
  52. {
  53. request.State = HTTPRequestStates.Processing;
  54. request.Prepare();
  55. // then start process the request
  56. conn.Process(request);
  57. }
  58. else
  59. {
  60. // If no free connection found and creation prohibited, we will put back to the queue
  61. this.Queue.Add(request);
  62. }
  63. return this;
  64. }
  65. internal ConnectionBase GetNextAvailable(HTTPRequest request)
  66. {
  67. int activeConnections = 0;
  68. ConnectionBase conn = null;
  69. // Check the last created connection first. This way, if a higher level protocol is present that can handle more requests (== HTTP/2) that protocol will be chosen
  70. // and others will be closed when their inactivity time is reached.
  71. for (int i = Connections.Count - 1; i >= 0; --i)
  72. {
  73. conn = Connections[i];
  74. if (conn.State == HTTPConnectionStates.Initial || conn.State == HTTPConnectionStates.Free || conn.CanProcessMultiple)
  75. {
  76. if (!conn.TestConnection())
  77. {
  78. HTTPManager.Logger.Verbose("HostConnection", "GetNextAvailable - TestConnection returned false!", this.Context, request.Context, conn.Context);
  79. RemoveConnectionImpl(conn, HTTPConnectionStates.Closed);
  80. continue;
  81. }
  82. HTTPManager.Logger.Verbose("HostConnection", string.Format("GetNextAvailable - returning with connection. state: {0}, CanProcessMultiple: {1}", conn.State, conn.CanProcessMultiple), this.Context, request.Context, conn.Context);
  83. return conn;
  84. }
  85. activeConnections++;
  86. }
  87. if (activeConnections >= HTTPManager.MaxConnectionPerServer)
  88. {
  89. HTTPManager.Logger.Verbose("HostConnection", string.Format("GetNextAvailable - activeConnections({0}) >= HTTPManager.MaxConnectionPerServer({1})", activeConnections, HTTPManager.MaxConnectionPerServer), this.Context, request.Context);
  90. return null;
  91. }
  92. string key = HostDefinition.GetKeyForRequest(request);
  93. conn = null;
  94. #if UNITY_WEBGL && !UNITY_EDITOR
  95. conn = new WebGLConnection(key);
  96. #else
  97. if (request.CurrentUri.IsFile)
  98. conn = new FileConnection(key);
  99. else
  100. {
  101. #if !BESTHTTP_DISABLE_ALTERNATE_SSL
  102. // Hold back the creation of a new connection until we know more about the remote host's features.
  103. // If we send out multiple requests at once it will execute the first and delay the others.
  104. // While it will decrease performance initially, it will prevent the creation of TCP connections
  105. // that will be unused after their first request processing if the server supports HTTP/2.
  106. if (activeConnections >= 1 && (this.ProtocolSupport == HostProtocolSupport.Unknown || this.ProtocolSupport == HostProtocolSupport.HTTP2))
  107. {
  108. HTTPManager.Logger.Verbose("HostConnection", string.Format("GetNextAvailable - waiting for protocol support message. activeConnections: {0}, ProtocolSupport: {1} ", activeConnections, this.ProtocolSupport), this.Context, request.Context);
  109. return null;
  110. }
  111. #endif
  112. conn = new HTTPConnection(key);
  113. HTTPManager.Logger.Verbose("HostConnection", string.Format("GetNextAvailable - creating new connection, key: {0} ", key), this.Context, request.Context, conn.Context);
  114. }
  115. #endif
  116. Connections.Add(conn);
  117. return conn;
  118. }
  119. internal HostConnection RecycleConnection(ConnectionBase conn)
  120. {
  121. conn.State = HTTPConnectionStates.Free;
  122. BestHTTP.Extensions.Timer.Add(new TimerData(TimeSpan.FromSeconds(1), conn, CloseConnectionAfterInactivity));
  123. return this;
  124. }
  125. private bool RemoveConnectionImpl(ConnectionBase conn, HTTPConnectionStates setState)
  126. {
  127. conn.State = setState;
  128. conn.Dispose();
  129. bool found = this.Connections.Remove(conn);
  130. if (!found)
  131. HTTPManager.Logger.Information(typeof(HostConnection).Name, string.Format("RemoveConnection - Couldn't find connection! key: {0}", conn.ServerAddress), this.Context, conn.Context);
  132. return found;
  133. }
  134. internal HostConnection RemoveConnection(ConnectionBase conn, HTTPConnectionStates setState)
  135. {
  136. RemoveConnectionImpl(conn, setState);
  137. return this;
  138. }
  139. internal HostConnection TryToSendQueuedRequests()
  140. {
  141. while (this.Queue.Count > 0 && GetNextAvailable(this.Queue[0]) != null)
  142. {
  143. Send(this.Queue[0]);
  144. this.Queue.RemoveAt(0);
  145. }
  146. return this;
  147. }
  148. public ConnectionBase Find(Predicate<ConnectionBase> match)
  149. {
  150. return this.Connections.Find(match);
  151. }
  152. private bool CloseConnectionAfterInactivity(DateTime now, object context)
  153. {
  154. var conn = context as ConnectionBase;
  155. bool closeConnection = conn.State == HTTPConnectionStates.Free && now - conn.LastProcessTime >= conn.KeepAliveTime;
  156. if (closeConnection)
  157. {
  158. HTTPManager.Logger.Information(typeof(HostConnection).Name, string.Format("CloseConnectionAfterInactivity - [{0}] Closing! State: {1}, Now: {2}, LastProcessTime: {3}, KeepAliveTime: {4}",
  159. conn.ToString(), conn.State, now.ToString(System.Globalization.CultureInfo.InvariantCulture), conn.LastProcessTime.ToString(System.Globalization.CultureInfo.InvariantCulture), conn.KeepAliveTime), this.Context, conn.Context);
  160. RemoveConnection(conn, HTTPConnectionStates.Closed);
  161. return false;
  162. }
  163. // repeat until the connection's state is free
  164. return conn.State == HTTPConnectionStates.Free;
  165. }
  166. public void RemoveAllIdleConnections()
  167. {
  168. for (int i = 0; i < this.Connections.Count; i++)
  169. if (this.Connections[i].State == HTTPConnectionStates.Free)
  170. {
  171. int countBefore = this.Connections.Count;
  172. RemoveConnection(this.Connections[i], HTTPConnectionStates.Closed);
  173. if (countBefore != this.Connections.Count)
  174. i--;
  175. }
  176. }
  177. internal void Shutdown()
  178. {
  179. this.Queue.Clear();
  180. foreach (var conn in this.Connections)
  181. {
  182. // Swallow any exceptions, we are quitting anyway.
  183. try
  184. {
  185. conn.Shutdown(ShutdownTypes.Immediate);
  186. }
  187. catch { }
  188. }
  189. //this.Connections.Clear();
  190. }
  191. internal void SaveTo(System.IO.BinaryWriter bw)
  192. {
  193. bw.Write(this.LastProtocolSupportUpdate.ToBinary());
  194. bw.Write((byte)this.ProtocolSupport);
  195. }
  196. internal void LoadFrom(int version, System.IO.BinaryReader br)
  197. {
  198. this.LastProtocolSupportUpdate = DateTime.FromBinary(br.ReadInt64());
  199. this.ProtocolSupport = (HostProtocolSupport)br.ReadByte();
  200. if (DateTime.UtcNow - this.LastProtocolSupportUpdate >= TimeSpan.FromDays(1))
  201. {
  202. HTTPManager.Logger.Verbose("HostConnection", string.Format("LoadFrom - Too Old! LastProtocolSupportUpdate: {0}, ProtocolSupport: {1}", this.LastProtocolSupportUpdate.ToString(System.Globalization.CultureInfo.InvariantCulture), this.ProtocolSupport), this.Context);
  203. this.ProtocolSupport = HostProtocolSupport.Unknown;
  204. }
  205. else
  206. HTTPManager.Logger.Verbose("HostConnection", string.Format("LoadFrom - LastProtocolSupportUpdate: {0}, ProtocolSupport: {1}", this.LastProtocolSupportUpdate.ToString(System.Globalization.CultureInfo.InvariantCulture), this.ProtocolSupport), this.Context);
  207. }
  208. }
  209. }