Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CFContentStream.cs « System.Net.Http « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8bba8f0f9a9b32851a3b275dbd287e858cdf36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// CFContentStream.cs
//
// Authors:
//	Marek Safar  <marek.safar@gmail.com>
//
// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Runtime.ExceptionServices;

#if XAMCORE_4_0
using CFNetwork;
using CoreFoundation;
#elif XAMCORE_2_0
using CoreServices;
using CoreFoundation;
#else
using MonoTouch.CoreServices;
using MonoTouch.CoreFoundation;
#endif

namespace System.Net.Http
{
	class BufferData 
	{
		public byte[] Buffer;
		public int Length;
	}

	class CFContentStream : HttpContent
	{
		readonly CFHTTPStream http_stream;
		BufferData data;
		Mutex data_mutex;
		AutoResetEvent data_event;
		AutoResetEvent data_read_event;
		ExceptionDispatchInfo http_exception;

		// The requirements are:
		// * We must read at least one byte from the stream every time
		//   we get a HasBytesAvailable event.
		// * SerializeToStreamAsync is executed on a separate thread,
		//   so reads must somehow be synchronized with that thread.
		//
		// Current implementation:
		// * We read data in ReadStreamData (on the same thread
		//   we got the HasBytesAvailable event, i.e. inside the 
		//   HasBytesAvailable event handler).
		// * Data is stored in a class-level buffer.
		// * SerializeToStreamAsync blocks while waiting for
		//   data from ReadStreamData.
		// * ReadStreamData will only read more data once SerializeToStreamAsync
		//   has consumed any existing data. This means we'll be
		//   blocking in the HasBytesAvailable event handler until
		//   any previously read data has been processed (this prevents
		//   any unbound memory growth).

		public CFContentStream (CFHTTPStream stream)
		{
			this.http_stream = stream;
			this.http_stream.ErrorEvent += HandleErrorEvent;
			data = new BufferData () {
				Buffer = new byte [4096],
			};
			data_event = new AutoResetEvent (false);
			data_read_event = new AutoResetEvent (true);
			data_mutex = new Mutex ();
		}

		void HandleErrorEvent (object sender, CFStream.StreamEventArgs e)
		{
			var gotMutex = data_mutex.WaitOne ();
			if (gotMutex) {
				var stream = (CFHTTPStream)sender;
				if (e.EventType == CFStreamEventType.ErrorOccurred)
					Volatile.Write (ref http_exception, ExceptionDispatchInfo.Capture (stream.GetError ()));
				data_mutex.ReleaseMutex ();
			}
		}

		public void ReadStreamData ()
		{
			data_read_event.WaitOne (); // make sure there's no pending data.

			data_mutex.WaitOne ();
			data.Length = (int) http_stream.Read (data.Buffer, 0, data.Buffer.Length);
			data_mutex.ReleaseMutex ();

			data_event.Set ();
		}

		public void Close ()
		{
			data_read_event.WaitOne (); // make sure there's no pending data

			data_mutex.WaitOne ();
			data = null;
			this.http_stream.ErrorEvent -= HandleErrorEvent;
			data_mutex.ReleaseMutex ();

			data_event.Set ();
		}

		protected internal override async Task SerializeToStreamAsync (Stream stream, TransportContext context)
		{
			while (data_event.WaitOne ()) {
				data_mutex.WaitOne ();
				if (http_exception != null) {
					http_exception.Throw ();
					data_mutex.ReleaseMutex ();
					break;
				}
				if (data == null || data.Length <= 0) {
					data_mutex.ReleaseMutex ();
					data_read_event.Set ();
					break;
				}

				await stream.WriteAsync (data.Buffer, 0, data.Length).ConfigureAwait (false);
				data_mutex.ReleaseMutex ();

				data_read_event.Set ();
			}
		}

		protected internal override bool TryComputeLength (out long length)
		{
			length = 0;
			return false;
		}
	}
}