Exemplo n.º 1
0
 public static ServerSafeHandle NewServer(ChannelArgsSafeHandle args)
 {
     // Increment reference count for the native gRPC environment to make sure we don't do grpc_shutdown() before destroying the server handle.
     // Doing so would make object finalizer crash if we end up abandoning the handle.
     GrpcEnvironment.GrpcNativeInit();
     return Native.grpcsharp_server_create(args);
 }
Exemplo n.º 2
0
 public static ServerSafeHandle NewServer(ChannelArgsSafeHandle args)
 {
     // Increment reference count for the native gRPC environment to make sure we don't do grpc_shutdown() before destroying the server handle.
     // Doing so would make object finalizer crash if we end up abandoning the handle.
     GrpcEnvironment.GrpcNativeInit();
     return(Native.grpcsharp_server_create(args));
 }
Exemplo n.º 3
0
 private static ChannelArgsSafeHandle CreateNativeChannelArgs(ChannelArgs args)
 {
     if (args == null)
     {
         return(ChannelArgsSafeHandle.CreateNull());
     }
     return(args.ToNativeChannelArgs());
 }
Exemplo n.º 4
0
        public void CreateFillAndDestroy()
        {
            var channelArgs = ChannelArgsSafeHandle.Create(3);

            channelArgs.SetInteger(0, "somekey", 12345);
            channelArgs.SetString(1, "somekey", "abcdefghijkl");
            channelArgs.SetString(2, "somekey", "XYZ");
            channelArgs.Dispose();
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a channel that connects to a specific host.
 /// Port will default to 80 for an unsecure channel and to 443 a secure channel.
 /// </summary>
 /// <param name="host">The DNS name of IP address of the host.</param>
 /// <param name="credentials">Optional credentials to create a secure channel.</param>
 /// <param name="options">Channel options.</param>
 public Channel(string host, Credentials credentials = null, IEnumerable <ChannelOption> options = null)
 {
     using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(options))
     {
         if (credentials != null)
         {
             using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
             {
                 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
             }
         }
         else
         {
             this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs);
         }
     }
     this.target = GetOverridenTarget(host, options);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a channel.
 /// </summary>
 public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null)
 {
     using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs))
     {
         if (credentials != null)
         {
             using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
             {
                 this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs);
             }
         }
         else
         {
             this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs);
         }
     }
     this.target = GetOverridenTarget(target, channelArgs);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a channel that connects to a specific host.
        /// Port will default to 80 for an unsecure channel and to 443 for a secure channel.
        /// </summary>
        /// <param name="target">Target of the channel.</param>
        /// <param name="credentials">Credentials to secure the channel.</param>
        /// <param name="options">Channel options.</param>
        public Channel(string target, Credentials credentials, IEnumerable <ChannelOption> options = null)
        {
            this.target      = Preconditions.CheckNotNull(target, "target");
            this.environment = GrpcEnvironment.AddRef();
            this.options     = options != null ? new List <ChannelOption>(options) : new List <ChannelOption>();

            EnsureUserAgentChannelOption(this.options);
            using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
                using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options))
                {
                    if (nativeCredentials != null)
                    {
                        this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs);
                    }
                    else
                    {
                        this.handle = ChannelSafeHandle.CreateInsecure(target, nativeChannelArgs);
                    }
                }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates native object for a collection of channel options.
        /// </summary>
        /// <returns>The native channel arguments.</returns>
        internal static ChannelArgsSafeHandle CreateChannelArgs(ICollection <ChannelOption> options)
        {
            if (options == null || options.Count == 0)
            {
                return(ChannelArgsSafeHandle.CreateNull());
            }
            ChannelArgsSafeHandle nativeArgs = null;

            try
            {
                nativeArgs = ChannelArgsSafeHandle.Create(options.Count);
                int i = 0;
                foreach (var option in options)
                {
                    if (option.Type == ChannelOption.OptionType.Integer)
                    {
                        nativeArgs.SetInteger(i, option.Name, option.IntValue);
                    }
                    else if (option.Type == ChannelOption.OptionType.String)
                    {
                        nativeArgs.SetString(i, option.Name, option.StringValue);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown option type");
                    }
                    i++;
                }
                return(nativeArgs);
            }
            catch (Exception)
            {
                if (nativeArgs != null)
                {
                    nativeArgs.Dispose();
                }
                throw;
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates native object for a collection of channel options.
        /// </summary>
        /// <returns>The native channel arguments.</returns>
        internal static ChannelArgsSafeHandle CreateChannelArgs(IEnumerable <ChannelOption> options)
        {
            if (options == null)
            {
                return(ChannelArgsSafeHandle.CreateNull());
            }
            var optionList = new List <ChannelOption>(options);  // It's better to do defensive copy
            ChannelArgsSafeHandle nativeArgs = null;

            try
            {
                nativeArgs = ChannelArgsSafeHandle.Create(optionList.Count);
                for (int i = 0; i < optionList.Count; i++)
                {
                    var option = optionList[i];
                    if (option.Type == ChannelOption.OptionType.Integer)
                    {
                        nativeArgs.SetInteger(i, option.Name, option.IntValue);
                    }
                    else if (option.Type == ChannelOption.OptionType.String)
                    {
                        nativeArgs.SetString(i, option.Name, option.StringValue);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown option type");
                    }
                }
                return(nativeArgs);
            }
            catch (Exception)
            {
                if (nativeArgs != null)
                {
                    nativeArgs.Dispose();
                }
                throw;
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates native object for the channel arguments.
        /// </summary>
        /// <returns>The native channel arguments.</returns>
        internal ChannelArgsSafeHandle ToNativeChannelArgs()
        {
            ChannelArgsSafeHandle nativeArgs = null;

            try
            {
                nativeArgs = ChannelArgsSafeHandle.Create(stringArgs.Count);
                int i = 0;
                foreach (var entry in stringArgs)
                {
                    nativeArgs.SetString(i, entry.Key, entry.Value);
                    i++;
                }
                return(nativeArgs);
            }
            catch (Exception)
            {
                if (nativeArgs != null)
                {
                    nativeArgs.Dispose();
                }
                throw;
            }
        }
Exemplo n.º 11
0
        public void CreateNullAndDestroy()
        {
            var channelArgs = ChannelArgsSafeHandle.CreateNull();

            channelArgs.Dispose();
        }
Exemplo n.º 12
0
        public void CreateNonEmptyAndDestroy()
        {
            var channelArgs = ChannelArgsSafeHandle.Create(5);

            channelArgs.Dispose();
        }