Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Copyright 2015, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | #endregion |
| 31 | using System; |
| 32 | using System.Collections.Generic; |
| 33 | using System.Collections.Immutable; |
| 34 | using System.Runtime.InteropServices; |
| 35 | using System.Threading; |
| 36 | using System.Threading.Tasks; |
| 37 | using Grpc.Core.Internal; |
| 38 | using Grpc.Core.Utils; |
| 39 | |
| 40 | namespace Grpc.Core |
| 41 | { |
| 42 | /// <summary> |
| 43 | /// Channel option specified when creating a channel. |
| 44 | /// Corresponds to grpc_channel_args from grpc/grpc.h. |
| 45 | /// </summary> |
| 46 | public sealed class ChannelOption |
| 47 | { |
| 48 | public enum OptionType |
| 49 | { |
| 50 | Integer, |
| 51 | String |
| 52 | } |
| 53 | |
| 54 | private readonly OptionType type; |
| 55 | private readonly string name; |
| 56 | private readonly int intValue; |
| 57 | private readonly string stringValue; |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Creates a channel option with a string value. |
| 61 | /// </summary> |
| 62 | /// <param name="name">Name.</param> |
| 63 | /// <param name="stringValue">String value.</param> |
| 64 | public ChannelOption(string name, string stringValue) |
| 65 | { |
| 66 | this.type = OptionType.String; |
| 67 | this.name = Preconditions.CheckNotNull(name); |
| 68 | this.stringValue = Preconditions.CheckNotNull(stringValue); |
| 69 | } |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Creates a channel option with an integer value. |
| 73 | /// </summary> |
| 74 | /// <param name="name">Name.</param> |
| 75 | /// <param name="stringValue">String value.</param> |
| 76 | public ChannelOption(string name, int intValue) |
| 77 | { |
| 78 | this.type = OptionType.Integer; |
| 79 | this.name = Preconditions.CheckNotNull(name); |
| 80 | this.intValue = intValue; |
| 81 | } |
| 82 | |
| 83 | public OptionType Type |
| 84 | { |
| 85 | get |
| 86 | { |
| 87 | return type; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public string Name |
| 92 | { |
| 93 | get |
| 94 | { |
| 95 | return name; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public int IntValue |
| 100 | { |
| 101 | get |
| 102 | { |
| 103 | Preconditions.CheckState(type == OptionType.Integer); |
| 104 | return intValue; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public string StringValue |
| 109 | { |
| 110 | get |
| 111 | { |
| 112 | Preconditions.CheckState(type == OptionType.String); |
| 113 | return stringValue; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static class ChannelOptions |
| 119 | { |
| 120 | // Override SSL target check. Only to be used for testing. |
| 121 | public const string SslTargetNameOverride = "grpc.ssl_target_name_override"; |
| 122 | |
| 123 | // Enable census for tracing and stats collection |
| 124 | public const string Census = "grpc.census"; |
| 125 | |
| 126 | // Maximum number of concurrent incoming streams to allow on a http2 connection |
| 127 | public const string MaxConcurrentStreams = "grpc.max_concurrent_streams"; |
| 128 | |
| 129 | // Maximum message length that the channel can receive |
| 130 | public const string MaxMessageLength = "grpc.max_message_length"; |
| 131 | |
| 132 | // Initial sequence number for http2 transports |
| 133 | public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number"; |
| 134 | |
| 135 | /// <summary> |
| 136 | /// Creates native object for a collection of channel options. |
| 137 | /// </summary> |
| 138 | /// <returns>The native channel arguments.</returns> |
| 139 | internal static ChannelArgsSafeHandle CreateChannelArgs(IEnumerable<ChannelOption> options) |
| 140 | { |
| 141 | if (options == null) |
| 142 | { |
| 143 | return ChannelArgsSafeHandle.CreateNull(); |
| 144 | } |
| 145 | var optionList = new List<ChannelOption>(options); // It's better to do defensive copy |
| 146 | ChannelArgsSafeHandle nativeArgs = null; |
| 147 | try |
| 148 | { |
| 149 | nativeArgs = ChannelArgsSafeHandle.Create(optionList.Count); |
| 150 | for (int i = 0; i < optionList.Count; i++) |
| 151 | { |
| 152 | var option = optionList[i]; |
| 153 | if (option.Type == ChannelOption.OptionType.Integer) |
| 154 | { |
| 155 | nativeArgs.SetInteger(i, option.Name, option.IntValue); |
| 156 | } |
| 157 | else if (option.Type == ChannelOption.OptionType.String) |
| 158 | { |
| 159 | nativeArgs.SetString(i, option.Name, option.StringValue); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | throw new InvalidOperationException("Unknown option type"); |
| 164 | } |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 165 | } |
| 166 | return nativeArgs; |
| 167 | } |
| 168 | catch (Exception) |
| 169 | { |
| 170 | if (nativeArgs != null) |
| 171 | { |
| 172 | nativeArgs.Dispose(); |
| 173 | } |
| 174 | throw; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |