aboutsummaryrefslogtreecommitdiff
path: root/lib/Net.Http/src/AlternateProtocolBase.cs
blob: 929bc338809027cbc0fbe13eba4941433c6af940 (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
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Net.Http
* File: AlternateProtocolBase.cs 
*
* AlternateProtocolBase.cs is part of VNLib.Net.Http which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Net.Http is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Net.Http is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using VNLib.Net.Http.Core;

namespace VNLib.Net.Http
{
    /// <summary>
    /// A base class for all non-http protocol handlers
    /// </summary>
    public abstract class AlternateProtocolBase : MarshalByRefObject, IAlternateProtocol
    {
        /// <summary>
        /// A cancelation source that allows for canceling running tasks, that is linked 
        /// to the server that called <see cref="RunAsync(Stream)"/>.
        /// </summary>
        /// <remarks>
        /// This property is only available while the <see cref="RunAsync(Stream)"/> 
        /// method is executing
        /// </remarks>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        protected CancellationTokenSource CancelSource { get; private set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
        
        ///<inheritdoc/>
        async Task IAlternateProtocol.RunAsync(Stream transport, CancellationToken handlerToken)
        {
            //Create new cancel source
            CancelSource ??= new();
            //Register the token to cancel the source and save the registration for unregister on dispose
            CancellationTokenRegistration Registration = handlerToken.Register(CancelSource.Cancel);
            try
            {
                //Call child initialize method
                await RunAsync(new AlternateProtocolTransportStreamWrapper(transport));
                CancelSource.Cancel();
            }
            finally
            {
                //dispose the cancelation registration
                await Registration.DisposeAsync();
                //Dispose cancel source
                CancelSource.Dispose();
            }
        }

        /// <summary>
        /// Is the current socket connected using transport security
        /// </summary>
        public virtual bool IsSecure { get; init; }

        /// <summary>
        /// Determines if the instance is pending cancelation 
        /// </summary>
        public bool IsCancellationRequested => CancelSource.IsCancellationRequested;

        /// <summary>
        /// Cancels all pending operations. This session will be unusable after this function is called
        /// </summary>
        public virtual void CancelAll() => CancelSource?.Cancel();

        /// <summary>
        /// Called when the protocol swtich handshake has completed and the transport is 
        /// available for the new protocol
        /// </summary>
        /// <param name="transport">The transport stream</param>
        /// <returns>A task that represents the active use of the transport, and when complete all operations are unwound</returns>
        protected abstract Task RunAsync(Stream transport);    
    }
}