| | | 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.Collections.ObjectModel; |
| | | 6 | | using System.Net.Http; |
| | | 7 | | using System.Net.WebSockets; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | public sealed class WebSocketMessageProperty |
| | | 13 | | { |
| | | 14 | | public const string Name = "WebSocketMessageProperty"; |
| | | 15 | | private readonly ReadOnlyDictionary<string, object> _properties; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the WebSocketMessageProperty class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks>Defaults: WebSocketMessageType.Binary</remarks> |
| | 0 | 21 | | public WebSocketMessageProperty() |
| | | 22 | | { |
| | 0 | 23 | | MessageType = WebSocketDefaults.DefaultWebSocketMessageType; |
| | 0 | 24 | | } |
| | | 25 | | |
| | 16 | 26 | | internal WebSocketMessageProperty(WebSocketContext context, string subProtocol, WebSocketMessageType incomingMes |
| | | 27 | | { |
| | 16 | 28 | | WebSocketContext = context; |
| | 16 | 29 | | SubProtocol = subProtocol; |
| | 16 | 30 | | MessageType = incomingMessageType; |
| | 16 | 31 | | _properties = properties; |
| | 16 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the web socket context. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public WebSocketContext WebSocketContext { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets the sub-protocol. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public string SubProtocol { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets or sets the type of the message. The default is WebSocketMessageType.Binary. |
| | | 46 | | /// </summary> |
| | 16 | 47 | | public WebSocketMessageType MessageType { get; set; } |
| | | 48 | | |
| | | 49 | | public ReadOnlyDictionary<string, object> OpeningHandshakeProperties |
| | | 50 | | { |
| | | 51 | | get |
| | | 52 | | { |
| | 0 | 53 | | if (_properties == null) |
| | | 54 | | { |
| | 0 | 55 | | throw Fx.Exception.AsError(new InvalidOperationException(SR.Format( |
| | 0 | 56 | | SR.WebSocketOpeningHandshakePropertiesNotAvailable, |
| | 0 | 57 | | "RequestMessage", |
| | 0 | 58 | | typeof(HttpResponseMessage).Name, |
| | 0 | 59 | | typeof(DelegatingHandler).Name))); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return _properties; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal static bool TryGet(MessageProperties properties, out WebSocketMessageProperty property) |
| | | 67 | | { |
| | 15 | 68 | | if (properties == null) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 71 | | } |
| | | 72 | | |
| | 15 | 73 | | property = null; |
| | 15 | 74 | | if (properties.TryGetValue(Name, out object foundProperty)) |
| | | 75 | | { |
| | 0 | 76 | | property = (WebSocketMessageProperty)foundProperty; |
| | 0 | 77 | | return true; |
| | | 78 | | } |
| | 15 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |