5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Threading;
30 using System.Threading.Tasks;
39 using MonoTouch.CoreServices;
42 namespace System.Net.Http
50 class CFContentStream : HttpContent
52 readonly CFHTTPStream http_stream;
55 AutoResetEvent data_event;
56 AutoResetEvent data_read_event;
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.
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).
77 public CFContentStream (CFHTTPStream stream)
79 this.http_stream = stream;
80 data = new BufferData () {
81 Buffer = new byte [4096],
83 data_event = new AutoResetEvent (false);
84 data_read_event = new AutoResetEvent (true);
85 data_mutex = new Mutex ();
88 public void ReadStreamData ()
90 data_read_event.WaitOne (); // make sure there's no pending data.
92 data_mutex.WaitOne ();
93 data.Length = (int) http_stream.Read (data.Buffer, 0, data.Buffer.Length);
94 data_mutex.ReleaseMutex ();
101 data_read_event.WaitOne (); // make sure there's no pending data
103 data_mutex.WaitOne ();
105 data_mutex.ReleaseMutex ();
110 protected internal override async Task SerializeToStreamAsync (Stream stream, TransportContext context)
112 while (data_event.WaitOne ()) {
113 data_mutex.WaitOne ();
114 if (data == null || data.Length <= 0) {
115 data_mutex.ReleaseMutex ();
116 data_read_event.Set ();
120 await stream.WriteAsync (data.Buffer, 0, data.Length).ConfigureAwait (false);
121 data_mutex.ReleaseMutex ();
123 data_read_event.Set ();
127 protected internal override bool TryComputeLength (out long length)