[xbuild] Disable pre-2.0 csc hack to use /debug:portable (#4322)
[mono.git] / mcs / class / System.Net.Http / CFContentStream.cs
1 //
2 // CFContentStream.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
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
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.IO;
32 using System.Net;
33
34 #if XAMCORE_4_0
35 using CFNetwork;
36 #elif XAMCORE_2_0
37 using CoreServices;
38 #else
39 using MonoTouch.CoreServices;
40 #endif
41
42 namespace System.Net.Http
43 {
44         class BufferData 
45         {
46                 public byte[] Buffer;
47                 public int Length;
48         }
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
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
77                 public CFContentStream (CFHTTPStream stream)
78                 {
79                         this.http_stream = stream;
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
88                 public void ReadStreamData ()
89                 {
90                         data_read_event.WaitOne (); // make sure there's no pending data.
91
92                         data_mutex.WaitOne ();
93                         data.Length = (int) http_stream.Read (data.Buffer, 0, data.Buffer.Length);
94                         data_mutex.ReleaseMutex ();
95
96                         data_event.Set ();
97                 }
98
99                 public void Close ()
100                 {
101                         data_read_event.WaitOne (); // make sure there's no pending data
102
103                         data_mutex.WaitOne ();
104                         data = null;
105                         data_mutex.ReleaseMutex ();
106
107                         data_event.Set ();
108                 }
109
110                 protected internal override async Task SerializeToStreamAsync (Stream stream, TransportContext context)
111                 {
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 ();
117                                         break;
118                                 }
119
120                                 await stream.WriteAsync (data.Buffer, 0, data.Length).ConfigureAwait (false);
121                                 data_mutex.ReleaseMutex ();
122
123                                 data_read_event.Set ();
124                         }
125                 }
126
127                 protected internal override bool TryComputeLength (out long length)
128                 {
129                         length = 0;
130                         return false;
131                 }
132         }
133 }