CFContentStream.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if XAMCORE_4_0
  33. using CFNetwork;
  34. #elif XAMCORE_2_0
  35. using CoreServices;
  36. #else
  37. using MonoTouch.CoreServices;
  38. #endif
  39. namespace System.Net.Http
  40. {
  41. class BufferData
  42. {
  43. public byte[] Buffer;
  44. public int Length;
  45. }
  46. class CFContentStream : HttpContent
  47. {
  48. readonly CFHTTPStream http_stream;
  49. BufferData data;
  50. Mutex data_mutex;
  51. AutoResetEvent data_event;
  52. AutoResetEvent data_read_event;
  53. // The requirements are:
  54. // * We must read at least one byte from the stream every time
  55. // we get a HasBytesAvailable event.
  56. // * SerializeToStreamAsync is executed on a separate thread,
  57. // so reads must somehow be synchronized with that thread.
  58. //
  59. // Current implementation:
  60. // * We read data in ReadStreamData (on the same thread
  61. // we got the HasBytesAvailable event, i.e. inside the
  62. // HasBytesAvailable event handler).
  63. // * Data is stored in a class-level buffer.
  64. // * SerializeToStreamAsync blocks while waiting for
  65. // data from ReadStreamData.
  66. // * ReadStreamData will only read more data once SerializeToStreamAsync
  67. // has consumed any existing data. This means we'll be
  68. // blocking in the HasBytesAvailable event handler until
  69. // any previously read data has been processed (this prevents
  70. // any unbound memory growth).
  71. public CFContentStream (CFHTTPStream stream)
  72. {
  73. this.http_stream = stream;
  74. data = new BufferData () {
  75. Buffer = new byte [4096],
  76. };
  77. data_event = new AutoResetEvent (false);
  78. data_read_event = new AutoResetEvent (true);
  79. data_mutex = new Mutex ();
  80. }
  81. public void ReadStreamData ()
  82. {
  83. data_read_event.WaitOne (); // make sure there's no pending data.
  84. data_mutex.WaitOne ();
  85. data.Length = (int) http_stream.Read (data.Buffer, 0, data.Buffer.Length);
  86. data_mutex.ReleaseMutex ();
  87. data_event.Set ();
  88. }
  89. public void Close ()
  90. {
  91. data_read_event.WaitOne (); // make sure there's no pending data
  92. data_mutex.WaitOne ();
  93. data = null;
  94. data_mutex.ReleaseMutex ();
  95. data_event.Set ();
  96. }
  97. protected internal override async Task SerializeToStreamAsync (Stream stream, TransportContext context)
  98. {
  99. while (data_event.WaitOne ()) {
  100. data_mutex.WaitOne ();
  101. if (data == null || data.Length <= 0) {
  102. data_mutex.ReleaseMutex ();
  103. data_read_event.Set ();
  104. break;
  105. }
  106. await stream.WriteAsync (data.Buffer, 0, data.Length).ConfigureAwait (false);
  107. data_mutex.ReleaseMutex ();
  108. data_read_event.Set ();
  109. }
  110. }
  111. protected internal override bool TryComputeLength (out long length)
  112. {
  113. length = 0;
  114. return false;
  115. }
  116. }
  117. }