| | | 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 | | using System; |
| | | 5 | | using System.Net.WebSockets; |
| | | 6 | | using System.Threading; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | public sealed class WebSocketTransportSettings : IEquatable<WebSocketTransportSettings> |
| | | 12 | | { |
| | | 13 | | public const string ConnectionOpenedAction = "http://schemas.microsoft.com/2011/02/session/onopen"; |
| | | 14 | | public const string BinaryMessageReceivedAction = "http://schemas.microsoft.com/2011/02/websockets/onbinarymessa |
| | | 15 | | public const string TextMessageReceivedAction = "http://schemas.microsoft.com/2011/02/websockets/ontextmessage"; |
| | | 16 | | public const string SoapContentTypeHeader = "soap-content-type"; |
| | | 17 | | public const string BinaryEncoderTransferModeHeader = "microsoft-binary-transfer-mode"; |
| | | 18 | | internal const string WebSocketMethod = "WEBSOCKET"; |
| | | 19 | | internal const string SoapSubProtocol = "soap"; |
| | | 20 | | internal const string TransportUsageMethodName = "TransportUsage"; |
| | | 21 | | private WebSocketTransportUsage _transportUsage; |
| | | 22 | | private TimeSpan _keepAliveInterval; |
| | | 23 | | private string _subProtocol; |
| | | 24 | | private int _maxPendingConnections; |
| | | 25 | | |
| | 874 | 26 | | public WebSocketTransportSettings() |
| | | 27 | | { |
| | 874 | 28 | | _transportUsage = WebSocketDefaults.TransportUsage; |
| | 874 | 29 | | CreateNotificationOnConnection = WebSocketDefaults.CreateNotificationOnConnection; |
| | 874 | 30 | | _keepAliveInterval = WebSocketDefaults.DefaultKeepAliveInterval; |
| | 874 | 31 | | _subProtocol = WebSocketDefaults.SubProtocol; |
| | 874 | 32 | | DisablePayloadMasking = WebSocketDefaults.DisablePayloadMasking; |
| | 874 | 33 | | _maxPendingConnections = WebSocketDefaults.DefaultMaxPendingConnections; |
| | 874 | 34 | | } |
| | | 35 | | |
| | 8768 | 36 | | private WebSocketTransportSettings(WebSocketTransportSettings settings) |
| | | 37 | | { |
| | | 38 | | Fx.Assert(settings != null, "settings should not be null."); |
| | 8768 | 39 | | TransportUsage = settings.TransportUsage; |
| | 8768 | 40 | | SubProtocol = settings.SubProtocol; |
| | 8768 | 41 | | KeepAliveInterval = settings.KeepAliveInterval; |
| | 8768 | 42 | | DisablePayloadMasking = settings.DisablePayloadMasking; |
| | 8768 | 43 | | CreateNotificationOnConnection = settings.CreateNotificationOnConnection; |
| | 8768 | 44 | | MaxPendingConnections = settings.MaxPendingConnections; |
| | 8768 | 45 | | } |
| | | 46 | | |
| | | 47 | | public WebSocketTransportUsage TransportUsage |
| | | 48 | | { |
| | | 49 | | get |
| | | 50 | | { |
| | 10553 | 51 | | return _transportUsage; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | set |
| | | 55 | | { |
| | 8809 | 56 | | WebSocketTransportUsageHelper.Validate(value); |
| | 8809 | 57 | | _transportUsage = value; |
| | 8809 | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | 18432 | 61 | | public bool CreateNotificationOnConnection { get; set; } |
| | | 62 | | |
| | | 63 | | public TimeSpan KeepAliveInterval |
| | | 64 | | { |
| | | 65 | | get |
| | | 66 | | { |
| | 8772 | 67 | | return _keepAliveInterval; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | set |
| | | 71 | | { |
| | 8774 | 72 | | if (value < TimeSpan.Zero && value != Timeout.InfiniteTimeSpan) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException( |
| | 0 | 75 | | nameof(value), |
| | 0 | 76 | | value, |
| | 0 | 77 | | SRCommon.SFxTimeoutOutOfRange0)); |
| | | 78 | | } |
| | | 79 | | |
| | 8774 | 80 | | if (TimeoutHelper.IsTooLarge(value)) |
| | | 81 | | { |
| | 0 | 82 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException( |
| | 0 | 83 | | nameof(value), |
| | 0 | 84 | | value, |
| | 0 | 85 | | SRCommon.SFxTimeoutOutOfRangeTooBig)); |
| | | 86 | | } |
| | | 87 | | |
| | 8774 | 88 | | _keepAliveInterval = value; |
| | 8774 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public string SubProtocol |
| | | 93 | | { |
| | | 94 | | get |
| | | 95 | | { |
| | 8783 | 96 | | return _subProtocol; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | set |
| | | 100 | | { |
| | 8800 | 101 | | if (value != null) |
| | | 102 | | { |
| | 473 | 103 | | if (value == string.Empty) |
| | | 104 | | { |
| | 0 | 105 | | throw Fx.Exception.Argument(nameof(value), SR.WebSocketInvalidProtocolEmptySubprotocolString); |
| | | 106 | | } |
| | | 107 | | |
| | 473 | 108 | | if (value.Split(WebSocketHelper.ProtocolSeparators).Length > 1) |
| | | 109 | | { |
| | 0 | 110 | | throw Fx.Exception.Argument(nameof(value), SR.Format(SR.WebSocketInvalidProtocolContainsMultiple |
| | | 111 | | } |
| | | 112 | | |
| | 473 | 113 | | if (WebSocketHelper.IsSubProtocolInvalid(value, out string invalidChar)) |
| | | 114 | | { |
| | 0 | 115 | | throw Fx.Exception.Argument(nameof(value), SR.Format(SR.WebSocketInvalidProtocolInvalidCharInPro |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 8800 | 119 | | _subProtocol = value; |
| | 8800 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 18420 | 123 | | public bool DisablePayloadMasking { get; set; } |
| | | 124 | | |
| | | 125 | | public int MaxPendingConnections |
| | | 126 | | { |
| | | 127 | | get |
| | | 128 | | { |
| | 8772 | 129 | | return _maxPendingConnections; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | set |
| | | 133 | | { |
| | 8774 | 134 | | if (value < 0) |
| | | 135 | | { |
| | 0 | 136 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException( |
| | 0 | 137 | | nameof(value), |
| | 0 | 138 | | value, |
| | 0 | 139 | | SRCommon.ValueMustBePositive)); |
| | | 140 | | } |
| | | 141 | | |
| | 8774 | 142 | | _maxPendingConnections = value; |
| | 8774 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public bool Equals(WebSocketTransportSettings other) |
| | | 147 | | { |
| | 2 | 148 | | if (other == null) |
| | | 149 | | { |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 2 | 153 | | return TransportUsage == other.TransportUsage |
| | 2 | 154 | | && CreateNotificationOnConnection == other.CreateNotificationOnConnection |
| | 2 | 155 | | && KeepAliveInterval == other.KeepAliveInterval |
| | 2 | 156 | | && DisablePayloadMasking == other.DisablePayloadMasking |
| | 2 | 157 | | && StringComparer.OrdinalIgnoreCase.Compare(SubProtocol, other.SubProtocol) == 0 |
| | 2 | 158 | | && MaxPendingConnections == other.MaxPendingConnections; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | public override bool Equals(object obj) |
| | | 162 | | { |
| | 0 | 163 | | if (obj == null) |
| | | 164 | | { |
| | 0 | 165 | | return base.Equals(obj); |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | WebSocketTransportSettings settings = obj as WebSocketTransportSettings; |
| | 0 | 169 | | return Equals(settings); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | public override int GetHashCode() |
| | | 173 | | { |
| | 0 | 174 | | int hashcode = TransportUsage.GetHashCode() |
| | 0 | 175 | | ^ CreateNotificationOnConnection.GetHashCode() |
| | 0 | 176 | | ^ KeepAliveInterval.GetHashCode() |
| | 0 | 177 | | ^ DisablePayloadMasking.GetHashCode() |
| | 0 | 178 | | ^ MaxPendingConnections.GetHashCode(); |
| | 0 | 179 | | if (SubProtocol != null) |
| | | 180 | | { |
| | 0 | 181 | | hashcode ^= SubProtocol.ToLowerInvariant().GetHashCode(); |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | return hashcode; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | internal WebSocketTransportSettings Clone() |
| | | 188 | | { |
| | 8768 | 189 | | return new WebSocketTransportSettings(this); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal TimeSpan GetEffectiveKeepAliveInterval() |
| | | 193 | | { |
| | 22 | 194 | | return _keepAliveInterval == TimeSpan.Zero ? WebSocket.DefaultKeepAliveInterval : _keepAliveInterval; |
| | | 195 | | } |
| | | 196 | | } |
| | | 197 | | } |