| 1 | // Licensed to the .NET Foundation under one or more agreements.
|
|---|
| 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
|---|
| 3 |
|
|---|
| 4 | //
|
|---|
| 5 | // System.Net.HttpStreamAsyncResult
|
|---|
| 6 | //
|
|---|
| 7 | // Authors:
|
|---|
| 8 | // Gonzalo Paniagua Javier (gonzalo@novell.com)
|
|---|
| 9 | //
|
|---|
| 10 | // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
|
|---|
| 11 | //
|
|---|
| 12 | // Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 13 | // a copy of this software and associated documentation files (the
|
|---|
| 14 | // "Software"), to deal in the Software without restriction, including
|
|---|
| 15 | // without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 16 | // distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 17 | // permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 18 | // the following conditions:
|
|---|
| 19 | //
|
|---|
| 20 | // The above copyright notice and this permission notice shall be
|
|---|
| 21 | // included in all copies or substantial portions of the Software.
|
|---|
| 22 | //
|
|---|
| 23 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 24 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 25 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|---|
| 26 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|---|
| 27 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|---|
| 28 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|---|
| 29 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 30 | //
|
|---|
| 31 |
|
|---|
| 32 | using System;
|
|---|
| 33 | using System.Threading;
|
|---|
| 34 | using System.Threading.Tasks;
|
|---|
| 35 |
|
|---|
| 36 | namespace SpaceWizards.HttpListener
|
|---|
| 37 | {
|
|---|
| 38 | internal sealed class HttpStreamAsyncResult : IAsyncResult
|
|---|
| 39 | {
|
|---|
| 40 | private object _locker = new object();
|
|---|
| 41 | private ManualResetEvent? _handle;
|
|---|
| 42 | private bool _completed;
|
|---|
| 43 |
|
|---|
| 44 | internal readonly object _parent;
|
|---|
| 45 | internal byte[]? _buffer;
|
|---|
| 46 | internal int _offset;
|
|---|
| 47 | internal int _count;
|
|---|
| 48 | internal AsyncCallback? _callback;
|
|---|
| 49 | internal object? _state;
|
|---|
| 50 | internal int _synchRead;
|
|---|
| 51 | internal Exception? _error;
|
|---|
| 52 | internal bool _endCalled;
|
|---|
| 53 |
|
|---|
| 54 | internal HttpStreamAsyncResult(object parent)
|
|---|
| 55 | {
|
|---|
| 56 | _parent = parent;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | public void Complete(Exception e)
|
|---|
| 60 | {
|
|---|
| 61 | _error = e;
|
|---|
| 62 | Complete();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public void Complete()
|
|---|
| 66 | {
|
|---|
| 67 | lock (_locker)
|
|---|
| 68 | {
|
|---|
| 69 | if (_completed)
|
|---|
| 70 | return;
|
|---|
| 71 |
|
|---|
| 72 | _completed = true;
|
|---|
| 73 | if (_handle != null)
|
|---|
| 74 | _handle.Set();
|
|---|
| 75 |
|
|---|
| 76 | if (_callback != null)
|
|---|
| 77 | Task.Run(() => _callback(this));
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public object? AsyncState
|
|---|
| 82 | {
|
|---|
| 83 | get { return _state; }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public WaitHandle AsyncWaitHandle
|
|---|
| 87 | {
|
|---|
| 88 | get
|
|---|
| 89 | {
|
|---|
| 90 | lock (_locker)
|
|---|
| 91 | {
|
|---|
| 92 | if (_handle == null)
|
|---|
| 93 | _handle = new ManualResetEvent(_completed);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | return _handle;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public bool CompletedSynchronously => false;
|
|---|
| 101 |
|
|---|
| 102 | public bool IsCompleted
|
|---|
| 103 | {
|
|---|
| 104 | get
|
|---|
| 105 | {
|
|---|
| 106 | lock (_locker)
|
|---|
| 107 | {
|
|---|
| 108 | return _completed;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|