CFContentStream.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // CFContentStream.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using System.IO;
  31. using System.Net;
  32. using System.Runtime.ExceptionServices;
  33. #if XAMCORE_4_0
  34. using CFNetwork;
  35. using CoreFoundation;
  36. #elif XAMCORE_2_0
  37. using CoreServices;
  38. using CoreFoundation;
  39. #else
  40. using MonoTouch.CoreServices;
  41. using MonoTouch.CoreFoundation;
  42. #endif
  43. namespace System.Net.Http
  44. {
  45. class BufferData
  46. {
  47. public byte[] Buffer;
  48. public int Length;
  49. }
  50. class CFContentStream : HttpContent
  51. {
  52. readonly CFHTTPStream http_stream;
  53. BufferData data;
  54. Mutex data_mutex;
  55. AutoResetEvent data_event;
  56. AutoResetEvent data_read_event;
  57. ExceptionDispatchInfo http_exception;
  58. // The requirements are:
  59. // * We must read at least one byte from the stream every time
  60. // we get a HasBytesAvailable event.
  61. // * SerializeToStreamAsync is executed on a separate thread,
  62. // so reads must somehow be synchronized with that thread.
  63. //
  64. // Current implementation:
  65. // * We read data in ReadStreamData (on the same thread
  66. // we got the HasBytesAvailable event, i.e. inside the
  67. // HasBytesAvailable event handler).
  68. // * Data is stored in a class-level buffer.
  69. // * SerializeToStreamAsync blocks while waiting for
  70. // data from ReadStreamData.
  71. // * ReadStreamData will only read more data once SerializeToStreamAsync
  72. // has consumed any existing data. This means we'll be
  73. // blocking in the HasBytesAvailable event handler until
  74. // any previously read data has been processed (this prevents
  75. // any unbound memory growth).
  76. public CFContentStream (CFHTTPStream stream)
  77. {
  78. this.http_stream = stream;
  79. this.http_stream.ErrorEvent += HandleErrorEvent;
  80. data = new BufferData () {
  81. Buffer = new byte [4096],
  82. };
  83. data_event = new AutoResetEvent (false);
  84. data_read_event = new AutoResetEvent (true);
  85. data_mutex = new Mutex ();
  86. }
  87. void HandleErrorEvent (object sender, CFStream.StreamEventArgs e)
  88. {
  89. var gotMutex = data_mutex.WaitOne ();
  90. if (gotMutex) {
  91. var stream = (CFHTTPStream)sender;
  92. if (e.EventType == CFStreamEventType.ErrorOccurred)
  93. Volatile.Write (ref http_exception, ExceptionDispatchInfo.Capture (stream.GetError ()));
  94. data_mutex.ReleaseMutex ();
  95. }
  96. }
  97. public void ReadStreamData ()
  98. {
  99. data_read_event.WaitOne (); // make sure there's no pending data.
  100. data_mutex.WaitOne ();
  101. data.Length = (int) http_stream.Read (data.Buffer, 0, data.Buffer.Length);
  102. data_mutex.ReleaseMutex ();
  103. data_event.Set ();
  104. }
  105. public void Close ()
  106. {
  107. data_read_event.WaitOne (); // make sure there's no pending data
  108. data_mutex.WaitOne ();
  109. data = null;
  110. this.http_stream.ErrorEvent -= HandleErrorEvent;
  111. data_mutex.ReleaseMutex ();
  112. data_event.Set ();
  113. }
  114. protected internal override async Task SerializeToStreamAsync (Stream stream, TransportContext context)
  115. {
  116. while (data_event.WaitOne ()) {
  117. data_mutex.WaitOne ();
  118. if (http_exception != null) {
  119. http_exception.Throw ();
  120. data_mutex.ReleaseMutex ();
  121. break;
  122. }
  123. if (data == null || data.Length <= 0) {
  124. data_mutex.ReleaseMutex ();
  125. data_read_event.Set ();
  126. break;
  127. }
  128. await stream.WriteAsync (data.Buffer, 0, data.Length).ConfigureAwait (false);
  129. data_mutex.ReleaseMutex ();
  130. data_read_event.Set ();
  131. }
  132. }
  133. protected internal override bool TryComputeLength (out long length)
  134. {
  135. length = 0;
  136. return false;
  137. }
  138. }
  139. }