xref: /aosp_15_r20/external/grpc-grpc/src/cpp/client/create_channel_posix.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/grpc_posix.h>
26 #include <grpc/grpc_security.h>
27 #include <grpcpp/channel.h>
28 #include <grpcpp/impl/grpc_library.h>
29 #include <grpcpp/support/channel_arguments.h>
30 #include <grpcpp/support/client_interceptor.h>
31 
32 #include "src/cpp/client/create_channel_internal.h"
33 
34 namespace grpc {
35 
36 class ChannelArguments;
37 
38 #ifdef GPR_SUPPORT_CHANNELS_FROM_FD
39 
CreateInsecureChannelFromFd(const std::string & target,int fd)40 std::shared_ptr<Channel> CreateInsecureChannelFromFd(const std::string& target,
41                                                      int fd) {
42   internal::GrpcLibrary init_lib;
43   grpc_channel_credentials* creds = grpc_insecure_credentials_create();
44   auto channel = CreateChannelInternal(
45       "", grpc_channel_create_from_fd(target.c_str(), fd, creds, nullptr),
46       std::vector<
47           std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
48   grpc_channel_credentials_release(creds);
49   return channel;
50 }
51 
CreateCustomInsecureChannelFromFd(const std::string & target,int fd,const grpc::ChannelArguments & args)52 std::shared_ptr<Channel> CreateCustomInsecureChannelFromFd(
53     const std::string& target, int fd, const grpc::ChannelArguments& args) {
54   internal::GrpcLibrary init_lib;
55   grpc_channel_args channel_args;
56   args.SetChannelArgs(&channel_args);
57   grpc_channel_credentials* creds = grpc_insecure_credentials_create();
58   auto channel = CreateChannelInternal(
59       "", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args),
60       std::vector<
61           std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
62   grpc_channel_credentials_release(creds);
63   // Channel also initializes gRPC, so we can decrement the init ref count here.
64   return channel;
65 }
66 
67 namespace experimental {
68 
CreateCustomInsecureChannelWithInterceptorsFromFd(const std::string & target,int fd,const ChannelArguments & args,std::vector<std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> interceptor_creators)69 std::shared_ptr<Channel> CreateCustomInsecureChannelWithInterceptorsFromFd(
70     const std::string& target, int fd, const ChannelArguments& args,
71     std::vector<
72         std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
73         interceptor_creators) {
74   internal::GrpcLibrary init_lib;
75   grpc_channel_args channel_args;
76   args.SetChannelArgs(&channel_args);
77   grpc_channel_credentials* creds = grpc_insecure_credentials_create();
78   auto channel = CreateChannelInternal(
79       "", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args),
80       std::move(interceptor_creators));
81   grpc_channel_credentials_release(creds);
82   return channel;
83 }
84 
85 }  // namespace experimental
86 
87 #endif  // GPR_SUPPORT_CHANNELS_FROM_FD
88 
89 }  // namespace grpc
90