HTTP2Handler.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Collections.Concurrent;
  6. using BestHTTP.Extensions;
  7. using BestHTTP.Core;
  8. using BestHTTP.PlatformSupport.Memory;
  9. using BestHTTP.Logger;
  10. namespace BestHTTP.Connections.HTTP2
  11. {
  12. public sealed class HTTP2Handler : IHTTPRequestHandler
  13. {
  14. public bool HasCustomRequestProcessor { get { return true; } }
  15. public KeepAliveHeader KeepAlive { get { return null; } }
  16. public bool CanProcessMultiple { get { return this.goAwaySentAt == DateTime.MaxValue && this.isRunning; } }
  17. // Connection preface starts with the string PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n).
  18. private static readonly byte[] MAGIC = new byte[24] { 0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a, 0x0d, 0x0a };
  19. public const UInt32 MaxValueFor31Bits = 0xFFFFFFFF >> 1;
  20. public double Latency { get; private set; }
  21. public HTTP2SettingsManager settings;
  22. public HPACKEncoder HPACKEncoder;
  23. public LoggingContext Context { get; private set; }
  24. private DateTime lastPingSent = DateTime.MinValue;
  25. private TimeSpan pingFrequency = TimeSpan.MaxValue; // going to be overridden in RunHandler
  26. private int waitingForPingAck = 0;
  27. public static int RTTBufferCapacity = 5;
  28. private CircularBuffer<double> rtts = new CircularBuffer<double>(RTTBufferCapacity);
  29. private volatile bool isRunning;
  30. private AutoResetEvent newFrameSignal = new AutoResetEvent(false);
  31. private ConcurrentQueue<HTTPRequest> requestQueue = new ConcurrentQueue<HTTPRequest>();
  32. private List<HTTP2Stream> clientInitiatedStreams = new List<HTTP2Stream>();
  33. private ConcurrentQueue<HTTP2FrameHeaderAndPayload> newFrames = new ConcurrentQueue<HTTP2FrameHeaderAndPayload>();
  34. private List<HTTP2FrameHeaderAndPayload> outgoingFrames = new List<HTTP2FrameHeaderAndPayload>();
  35. private UInt32 remoteWindow;
  36. private DateTime lastInteraction;
  37. private DateTime goAwaySentAt = DateTime.MaxValue;
  38. private HTTPConnection conn;
  39. private int threadExitCount;
  40. private TimeSpan MaxGoAwayWaitTime { get { return this.goAwaySentAt == DateTime.MaxValue ? TimeSpan.MaxValue : TimeSpan.FromMilliseconds(Math.Max(this.Latency * 2.5, 1500)); } }
  41. // https://httpwg.org/specs/rfc7540.html#StreamIdentifiers
  42. // Streams initiated by a client MUST use odd-numbered stream identifiers
  43. // With an initial value of -1, the first client initiated stream's id going to be 1.
  44. private long LastStreamId = -1;
  45. public HTTP2Handler(HTTPConnection conn)
  46. {
  47. this.Context = new LoggingContext(this);
  48. this.conn = conn;
  49. this.isRunning = true;
  50. this.settings = new HTTP2SettingsManager(this);
  51. Process(this.conn.CurrentRequest);
  52. }
  53. public void Process(HTTPRequest request)
  54. {
  55. HTTPManager.Logger.Information("HTTP2Handler", "Process request called", this.Context, request.Context);
  56. request.QueuedAt = DateTime.MinValue;
  57. request.ProcessingStarted = this.lastInteraction = DateTime.UtcNow;
  58. this.requestQueue.Enqueue(request);
  59. // Wee might added the request to a dead queue, signaling would be pointless.
  60. // When the ConnectionEventHelper processes the Close state-change event
  61. // requests in the queue going to be resent. (We should avoid resending the request just right now,
  62. // as it might still select this connection/handler resulting in a infinite loop.)
  63. if (Volatile.Read(ref this.threadExitCount) == 0)
  64. this.newFrameSignal.Set();
  65. }
  66. public void SignalRunnerThread()
  67. {
  68. this.newFrameSignal.Set();
  69. }
  70. public void RunHandler()
  71. {
  72. HTTPManager.Logger.Information("HTTP2Handler", "Processing thread up and running!", this.Context);
  73. Thread.CurrentThread.Name = "BestHTTP.HTTP2 Process";
  74. PlatformSupport.Threading.ThreadedRunner.RunLongLiving(ReadThread);
  75. try
  76. {
  77. bool atLeastOneStreamHasAFrameToSend = true;
  78. this.HPACKEncoder = new HPACKEncoder(this, this.settings);
  79. // https://httpwg.org/specs/rfc7540.html#InitialWindowSize
  80. // The connection flow-control window is also 65,535 octets.
  81. this.remoteWindow = this.settings.RemoteSettings[HTTP2Settings.INITIAL_WINDOW_SIZE];
  82. // we want to pack as many data as we can in one tcp segment, but setting the buffer's size too high
  83. // we might keep data too long and send them in bursts instead of in a steady stream.
  84. // Keeping it too low might result in a full tcp segment and one with very low payload
  85. // Is it possible that one full tcp segment sized buffer would be the best, or multiple of it.
  86. // It would keep the network busy without any fragments. The ethernet layer has a maximum of 1500 bytes,
  87. // but there's two layers of 20 byte headers each, so as a theoretical maximum it's 1500-20-20 bytes.
  88. // On the other hand, if the buffer is small (1-2), that means that for larger data, we have to do a lot
  89. // of system calls, in that case a larger buffer might be better. Still, if we are not cpu bound,
  90. // a well saturated network might serve us better.
  91. using (WriteOnlyBufferedStream bufferedStream = new WriteOnlyBufferedStream(this.conn.connector.Stream, 1024 * 1024 /*1500 - 20 - 20*/))
  92. {
  93. // The client connection preface starts with a sequence of 24 octets
  94. bufferedStream.Write(MAGIC, 0, MAGIC.Length);
  95. // This sequence MUST be followed by a SETTINGS frame (Section 6.5), which MAY be empty.
  96. // The client sends the client connection preface immediately upon receipt of a
  97. // 101 (Switching Protocols) response (indicating a successful upgrade)
  98. // or as the first application data octets of a TLS connection
  99. // Set streams' initial window size to its maximum.
  100. this.settings.InitiatedMySettings[HTTP2Settings.INITIAL_WINDOW_SIZE] = HTTPManager.HTTP2Settings.InitialStreamWindowSize;
  101. this.settings.InitiatedMySettings[HTTP2Settings.MAX_CONCURRENT_STREAMS] = HTTPManager.HTTP2Settings.MaxConcurrentStreams;
  102. this.settings.InitiatedMySettings[HTTP2Settings.ENABLE_CONNECT_PROTOCOL] = (uint)(HTTPManager.HTTP2Settings.EnableConnectProtocol ? 1 : 0);
  103. this.settings.InitiatedMySettings[HTTP2Settings.ENABLE_PUSH] = 0;
  104. this.settings.SendChanges(this.outgoingFrames);
  105. this.settings.RemoteSettings.OnSettingChangedEvent += OnRemoteSettingChanged;
  106. // The default window size for the whole connection is 65535 bytes,
  107. // but we want to set it to the maximum possible value.
  108. Int64 initialConnectionWindowSize = HTTPManager.HTTP2Settings.InitialConnectionWindowSize;
  109. // yandex.ru returns with an FLOW_CONTROL_ERROR (3) error when the plugin tries to set the connection window to 2^31 - 1
  110. // and works only with a maximum value of 2^31 - 10Mib (10 * 1024 * 1024).
  111. if (initialConnectionWindowSize == HTTP2Handler.MaxValueFor31Bits)
  112. initialConnectionWindowSize -= 10 * 1024 * 1024;
  113. Int64 diff = initialConnectionWindowSize - 65535;
  114. if (diff > 0)
  115. this.outgoingFrames.Add(HTTP2FrameHelper.CreateWindowUpdateFrame(0, (UInt32)diff));
  116. this.pingFrequency = HTTPManager.HTTP2Settings.PingFrequency;
  117. while (this.isRunning)
  118. {
  119. DateTime now = DateTime.UtcNow;
  120. if (!atLeastOneStreamHasAFrameToSend)
  121. {
  122. // buffered stream will call flush automatically if its internal buffer is full.
  123. // But we have to make it sure that we flush remaining data before we go to sleep.
  124. bufferedStream.Flush();
  125. // Wait until we have to send the next ping, OR a new frame is received on the read thread.
  126. // lastPingSent Now lastPingSent+frequency lastPingSent+Ping timeout
  127. //----|---------------------|---------------|----------------------|----------------------|------------|
  128. // lastInteraction lastInteraction + MaxIdleTime
  129. var sendPingAt = this.lastPingSent + this.pingFrequency;
  130. var timeoutAt = this.lastPingSent + HTTPManager.HTTP2Settings.Timeout;
  131. var nextPingInteraction = sendPingAt < timeoutAt ? sendPingAt : timeoutAt;
  132. var disconnectByIdleAt = this.lastInteraction + HTTPManager.HTTP2Settings.MaxIdleTime;
  133. var nextDueClientInteractionAt = nextPingInteraction < disconnectByIdleAt ? nextPingInteraction : disconnectByIdleAt;
  134. int wait = (int)(nextDueClientInteractionAt - now).TotalMilliseconds;
  135. wait = (int)Math.Min(wait, this.MaxGoAwayWaitTime.TotalMilliseconds);
  136. if (wait >= 1)
  137. {
  138. if (HTTPManager.Logger.Level <= Logger.Loglevels.All)
  139. HTTPManager.Logger.Information("HTTP2Handler", string.Format("Sleeping for {0:N0}ms", wait), this.Context);
  140. this.newFrameSignal.WaitOne(wait);
  141. now = DateTime.UtcNow;
  142. }
  143. }
  144. // Don't send a new ping until a pong isn't received for the last one
  145. if (now - this.lastPingSent >= this.pingFrequency && Interlocked.CompareExchange(ref this.waitingForPingAck, 1, 0) == 0)
  146. {
  147. this.lastPingSent = now;
  148. var frame = HTTP2FrameHelper.CreatePingFrame(HTTP2PingFlags.None);
  149. BufferHelper.SetLong(frame.Payload, 0, now.Ticks);
  150. this.outgoingFrames.Add(frame);
  151. }
  152. // If no pong received in a (configurable) reasonable time, treat the connection broken
  153. if (this.waitingForPingAck != 0 && now - this.lastPingSent >= HTTPManager.HTTP2Settings.Timeout)
  154. throw new TimeoutException("Ping ACK isn't received in time!");
  155. // Process received frames
  156. HTTP2FrameHeaderAndPayload header;
  157. while (this.newFrames.TryDequeue(out header))
  158. {
  159. if (header.StreamId > 0)
  160. {
  161. HTTP2Stream http2Stream = FindStreamById(header.StreamId);
  162. // Add frame to the stream, so it can process it when its Process function is called
  163. if (http2Stream != null)
  164. {
  165. http2Stream.AddFrame(header, this.outgoingFrames);
  166. }
  167. else
  168. {
  169. // Error? It's possible that we closed and removed the stream while the server was in the middle of sending frames
  170. if (HTTPManager.Logger.Level == Loglevels.All)
  171. HTTPManager.Logger.Warning("HTTP2Handler", string.Format("No stream found for id: {0}! Can't deliver frame: {1}", header.StreamId, header), this.Context, http2Stream.Context);
  172. }
  173. }
  174. else
  175. {
  176. switch (header.Type)
  177. {
  178. case HTTP2FrameTypes.SETTINGS:
  179. this.settings.Process(header, this.outgoingFrames);
  180. PluginEventHelper.EnqueuePluginEvent(
  181. new PluginEventInfo(PluginEvents.HTTP2ConnectProtocol,
  182. new HTTP2ConnectProtocolInfo(this.conn.LastProcessedUri.Host,
  183. this.settings.MySettings[HTTP2Settings.ENABLE_CONNECT_PROTOCOL] == 1 && this.settings.RemoteSettings[HTTP2Settings.ENABLE_CONNECT_PROTOCOL] == 1)));
  184. break;
  185. case HTTP2FrameTypes.PING:
  186. var pingFrame = HTTP2FrameHelper.ReadPingFrame(header);
  187. // https://httpwg.org/specs/rfc7540.html#PING
  188. // if it wasn't an ack for our ping, we have to send one
  189. if ((pingFrame.Flags & HTTP2PingFlags.ACK) == 0)
  190. {
  191. var frame = HTTP2FrameHelper.CreatePingFrame(HTTP2PingFlags.ACK);
  192. Array.Copy(pingFrame.OpaqueData, 0, frame.Payload, 0, pingFrame.OpaqueDataLength);
  193. this.outgoingFrames.Add(frame);
  194. }
  195. break;
  196. case HTTP2FrameTypes.WINDOW_UPDATE:
  197. var windowUpdateFrame = HTTP2FrameHelper.ReadWindowUpdateFrame(header);
  198. this.remoteWindow += windowUpdateFrame.WindowSizeIncrement;
  199. break;
  200. case HTTP2FrameTypes.GOAWAY:
  201. // parse the frame, so we can print out detailed information
  202. HTTP2GoAwayFrame goAwayFrame = HTTP2FrameHelper.ReadGoAwayFrame(header);
  203. HTTPManager.Logger.Information("HTTP2Handler", "Received GOAWAY frame: " + goAwayFrame.ToString(), this.Context);
  204. string msg = string.Format("Server closing the connection! Error code: {0} ({1})", goAwayFrame.Error, goAwayFrame.ErrorCode);
  205. for (int i = 0; i < this.clientInitiatedStreams.Count; ++i)
  206. this.clientInitiatedStreams[i].Abort(msg);
  207. this.clientInitiatedStreams.Clear();
  208. // set the running flag to false, so the thread can exit
  209. this.isRunning = false;
  210. this.conn.State = HTTPConnectionStates.Closed;
  211. break;
  212. case HTTP2FrameTypes.ALT_SVC:
  213. //HTTP2AltSVCFrame altSvcFrame = HTTP2FrameHelper.ReadAltSvcFrame(header);
  214. // Implement
  215. //HTTPManager.EnqueuePluginEvent(new PluginEventInfo(PluginEvents.AltSvcHeader, new AltSvcEventInfo(altSvcFrame.Origin, ))
  216. break;
  217. }
  218. if (header.Payload != null)
  219. BufferPool.Release(header.Payload);
  220. }
  221. }
  222. UInt32 maxConcurrentStreams = Math.Min(HTTPManager.HTTP2Settings.MaxConcurrentStreams, this.settings.RemoteSettings[HTTP2Settings.MAX_CONCURRENT_STREAMS]);
  223. // pre-test stream count to lock only when truly needed.
  224. if (this.clientInitiatedStreams.Count < maxConcurrentStreams && this.isRunning)
  225. {
  226. // grab requests from queue
  227. HTTPRequest request;
  228. while (this.clientInitiatedStreams.Count < maxConcurrentStreams && this.requestQueue.TryDequeue(out request))
  229. {
  230. // create a new stream
  231. var newStream = new HTTP2Stream((UInt32)Interlocked.Add(ref LastStreamId, 2), this, this.settings, this.HPACKEncoder);
  232. // process the request
  233. newStream.Assign(request);
  234. this.clientInitiatedStreams.Add(newStream);
  235. }
  236. }
  237. // send any settings changes
  238. this.settings.SendChanges(this.outgoingFrames);
  239. atLeastOneStreamHasAFrameToSend = false;
  240. // process other streams
  241. // Room for improvement Streams should be processed by their priority!
  242. for (int i = 0; i < this.clientInitiatedStreams.Count; ++i)
  243. {
  244. var stream = this.clientInitiatedStreams[i];
  245. stream.Process(this.outgoingFrames);
  246. // remove closed, empty streams (not enough to check the closed flag, a closed stream still can contain frames to send)
  247. if (stream.State == HTTP2StreamStates.Closed && !stream.HasFrameToSend)
  248. {
  249. this.clientInitiatedStreams.RemoveAt(i--);
  250. stream.Removed();
  251. }
  252. atLeastOneStreamHasAFrameToSend |= stream.HasFrameToSend;
  253. this.lastInteraction = DateTime.UtcNow;
  254. }
  255. // If we encounter a data frame that too large for the current remote window, we have to stop
  256. // sending all data frames as we could send smaller data frames before the large ones.
  257. // Room for improvement: An improvement would be here to stop data frame sending per-stream.
  258. bool haltDataSending = false;
  259. if (this.ShutdownType == ShutdownTypes.Running && now - this.lastInteraction >= HTTPManager.HTTP2Settings.MaxIdleTime)
  260. {
  261. this.lastInteraction = DateTime.UtcNow;
  262. HTTPManager.Logger.Information("HTTP2Handler", "Reached idle time, sending GoAway frame!", this.Context);
  263. this.outgoingFrames.Add(HTTP2FrameHelper.CreateGoAwayFrame(0, HTTP2ErrorCodes.NO_ERROR));
  264. this.goAwaySentAt = DateTime.UtcNow;
  265. }
  266. // https://httpwg.org/specs/rfc7540.html#GOAWAY
  267. // Endpoints SHOULD always send a GOAWAY frame before closing a connection so that the remote peer can know whether a stream has been partially processed or not.
  268. if (this.ShutdownType == ShutdownTypes.Gentle)
  269. {
  270. HTTPManager.Logger.Information("HTTP2Handler", "Connection abort requested, sending GoAway frame!", this.Context);
  271. this.outgoingFrames.Clear();
  272. this.outgoingFrames.Add(HTTP2FrameHelper.CreateGoAwayFrame(0, HTTP2ErrorCodes.NO_ERROR));
  273. this.goAwaySentAt = DateTime.UtcNow;
  274. }
  275. if (this.isRunning && now - goAwaySentAt >= this.MaxGoAwayWaitTime)
  276. {
  277. HTTPManager.Logger.Information("HTTP2Handler", "No GoAway frame received back. Really quitting now!", this.Context);
  278. this.isRunning = false;
  279. conn.State = HTTPConnectionStates.Closed;
  280. }
  281. uint streamWindowUpdates = 0;
  282. // Go through all the collected frames and send them.
  283. for (int i = 0; i < this.outgoingFrames.Count; ++i)
  284. {
  285. var frame = this.outgoingFrames[i];
  286. if (HTTPManager.Logger.Level <= Logger.Loglevels.All && frame.Type != HTTP2FrameTypes.DATA /*&& frame.Type != HTTP2FrameTypes.PING*/)
  287. HTTPManager.Logger.Information("HTTP2Handler", "Sending frame: " + frame.ToString(), this.Context);
  288. // post process frames
  289. switch (frame.Type)
  290. {
  291. case HTTP2FrameTypes.DATA:
  292. if (haltDataSending)
  293. continue;
  294. // if the tracked remoteWindow is smaller than the frame's payload, we stop sending
  295. // data frames until we receive window-update frames
  296. if (frame.PayloadLength > this.remoteWindow)
  297. {
  298. haltDataSending = true;
  299. HTTPManager.Logger.Warning("HTTP2Handler", string.Format("Data sending halted for this round. Remote Window: {0:N0}, frame: {1}", this.remoteWindow, frame.ToString()), this.Context);
  300. continue;
  301. }
  302. break;
  303. case HTTP2FrameTypes.WINDOW_UPDATE:
  304. if (frame.StreamId > 0)
  305. streamWindowUpdates += BufferHelper.ReadUInt31(frame.Payload, 0);
  306. break;
  307. }
  308. this.outgoingFrames.RemoveAt(i--);
  309. using (var buffer = HTTP2FrameHelper.HeaderAsBinary(frame))
  310. bufferedStream.Write(buffer.Data, 0, buffer.Length);
  311. if (frame.PayloadLength > 0)
  312. {
  313. bufferedStream.Write(frame.Payload, (int)frame.PayloadOffset, (int)frame.PayloadLength);
  314. if (!frame.DontUseMemPool)
  315. BufferPool.Release(frame.Payload);
  316. }
  317. if (frame.Type == HTTP2FrameTypes.DATA)
  318. this.remoteWindow -= frame.PayloadLength;
  319. }
  320. if (streamWindowUpdates > 0)
  321. {
  322. var frame = HTTP2FrameHelper.CreateWindowUpdateFrame(0, streamWindowUpdates);
  323. if (HTTPManager.Logger.Level <= Logger.Loglevels.All)
  324. HTTPManager.Logger.Information("HTTP2Handler", "Sending frame: " + frame.ToString(), this.Context);
  325. using (var buffer = HTTP2FrameHelper.HeaderAsBinary(frame))
  326. bufferedStream.Write(buffer.Data, 0, buffer.Length);
  327. bufferedStream.Write(frame.Payload, (int)frame.PayloadOffset, (int)frame.PayloadLength);
  328. }
  329. } // while (this.isRunning)
  330. bufferedStream.Flush();
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. // Log out the exception if it's a non-expected one.
  336. if (this.ShutdownType == ShutdownTypes.Running && this.goAwaySentAt == DateTime.MaxValue && HTTPManager.IsQuitting)
  337. HTTPManager.Logger.Exception("HTTP2Handler", "Sender thread", ex, this.Context);
  338. }
  339. finally
  340. {
  341. TryToCleanup();
  342. HTTPManager.Logger.Information("HTTP2Handler", "Sender thread closing - cleaning up remaining request...", this.Context);
  343. for (int i = 0; i < this.clientInitiatedStreams.Count; ++i)
  344. this.clientInitiatedStreams[i].Abort("Connection closed unexpectedly");
  345. this.clientInitiatedStreams.Clear();
  346. HTTPManager.Logger.Information("HTTP2Handler", "Sender thread closing", this.Context);
  347. }
  348. try
  349. {
  350. if (this.conn != null && this.conn.connector != null)
  351. {
  352. // Works in the new runtime
  353. if (this.conn.connector.TopmostStream != null)
  354. using (this.conn.connector.TopmostStream) { }
  355. // Works in the old runtime
  356. if (this.conn.connector.Stream != null)
  357. using (this.conn.connector.Stream) { }
  358. }
  359. }
  360. catch
  361. { }
  362. }
  363. private void OnRemoteSettingChanged(HTTP2SettingsRegistry registry, HTTP2Settings setting, uint oldValue, uint newValue)
  364. {
  365. switch(setting)
  366. {
  367. case HTTP2Settings.INITIAL_WINDOW_SIZE:
  368. this.remoteWindow = newValue - (oldValue - this.remoteWindow);
  369. break;
  370. }
  371. }
  372. private void ReadThread()
  373. {
  374. try
  375. {
  376. Thread.CurrentThread.Name = "BestHTTP.HTTP2 Read";
  377. HTTPManager.Logger.Information("HTTP2Handler", "Reader thread up and running!", this.Context);
  378. while (this.isRunning)
  379. {
  380. HTTP2FrameHeaderAndPayload header = HTTP2FrameHelper.ReadHeader(this.conn.connector.Stream);
  381. if (HTTPManager.Logger.Level <= Logger.Loglevels.Information && header.Type != HTTP2FrameTypes.DATA /*&& header.Type != HTTP2FrameTypes.PING*/)
  382. HTTPManager.Logger.Information("HTTP2Handler", "New frame received: " + header.ToString(), this.Context);
  383. // Add the new frame to the queue. Processing it on the write thread gives us the advantage that
  384. // we don't have to deal with too much locking.
  385. this.newFrames.Enqueue(header);
  386. // ping write thread to process the new frame
  387. this.newFrameSignal.Set();
  388. switch (header.Type)
  389. {
  390. // Handle pongs on the read thread, so no additional latency is added to the rtt calculation.
  391. case HTTP2FrameTypes.PING:
  392. var pingFrame = HTTP2FrameHelper.ReadPingFrame(header);
  393. if ((pingFrame.Flags & HTTP2PingFlags.ACK) != 0)
  394. {
  395. if (Interlocked.CompareExchange(ref this.waitingForPingAck, 0, 1) == 0)
  396. break; // waitingForPingAck was 0 == aren't expecting a ping ack!
  397. // it was an ack, payload must contain what we sent
  398. var ticks = BufferHelper.ReadLong(pingFrame.OpaqueData, 0);
  399. // the difference between the current time and the time when the ping message is sent
  400. TimeSpan diff = TimeSpan.FromTicks(DateTime.UtcNow.Ticks - ticks);
  401. // add it to the buffer
  402. this.rtts.Add(diff.TotalMilliseconds);
  403. // and calculate the new latency
  404. this.Latency = CalculateLatency();
  405. HTTPManager.Logger.Verbose("HTTP2Handler", string.Format("Latency: {0:F2}ms, RTT buffer: {1}", this.Latency, this.rtts.ToString()), this.Context);
  406. }
  407. break;
  408. case HTTP2FrameTypes.GOAWAY:
  409. // Just exit from this thread. The processing thread will handle the frame too.
  410. return;
  411. }
  412. }
  413. }
  414. catch //(Exception ex)
  415. {
  416. //HTTPManager.Logger.Exception("HTTP2Handler", "", ex, this.Context);
  417. //this.isRunning = false;
  418. }
  419. finally
  420. {
  421. TryToCleanup();
  422. HTTPManager.Logger.Information("HTTP2Handler", "Reader thread closing", this.Context);
  423. }
  424. }
  425. private void TryToCleanup()
  426. {
  427. this.isRunning = false;
  428. // First thread closing notifies the ConnectionEventHelper
  429. int counter = Interlocked.Increment(ref this.threadExitCount);
  430. if (counter == 1)
  431. ConnectionEventHelper.EnqueueConnectionEvent(new ConnectionEventInfo(this.conn, HTTPConnectionStates.Closed));
  432. // Last thread closes the AutoResetEvent
  433. if (counter == 2)
  434. {
  435. if (this.newFrameSignal != null)
  436. this.newFrameSignal.Close();
  437. this.newFrameSignal = null;
  438. }
  439. }
  440. private double CalculateLatency()
  441. {
  442. if (this.rtts.Count == 0)
  443. return 0;
  444. double sumLatency = 0;
  445. for (int i = 0; i < this.rtts.Count; ++i)
  446. sumLatency += this.rtts[i];
  447. return sumLatency / this.rtts.Count;
  448. }
  449. HTTP2Stream FindStreamById(UInt32 streamId)
  450. {
  451. for (int i = 0; i < this.clientInitiatedStreams.Count; ++i)
  452. {
  453. var stream = this.clientInitiatedStreams[i];
  454. if (stream.Id == streamId)
  455. return stream;
  456. }
  457. return null;
  458. }
  459. public ShutdownTypes ShutdownType { get; private set; }
  460. public void Shutdown(ShutdownTypes type)
  461. {
  462. this.ShutdownType = type;
  463. switch(this.ShutdownType)
  464. {
  465. case ShutdownTypes.Gentle:
  466. this.newFrameSignal.Set();
  467. break;
  468. case ShutdownTypes.Immediate:
  469. this.conn.connector.Stream.Dispose();
  470. break;
  471. }
  472. }
  473. public void Dispose()
  474. {
  475. HTTPRequest request = null;
  476. while (this.requestQueue.TryDequeue(out request))
  477. {
  478. HTTPManager.Logger.Information("HTTP2Handler", string.Format("Dispose - Request '{0}' IsCancellationRequested: {1}", request.CurrentUri.ToString(), request.IsCancellationRequested.ToString()), this.Context);
  479. if (request.IsCancellationRequested)
  480. {
  481. request.Response = null;
  482. request.State = HTTPRequestStates.Aborted;
  483. }
  484. else
  485. RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(request, RequestEvents.Resend));
  486. }
  487. }
  488. }
  489. }
  490. #endif