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 <grpc/credentials.h>
20 #include <grpc/grpc.h>
21 #include <grpc/grpc_posix.h>
22 #include <grpc/grpc_security.h>
23 #include <grpcpp/channel.h>
24 #include <grpcpp/impl/grpc_library.h>
25 #include <grpcpp/support/channel_arguments.h>
26 #include <grpcpp/support/client_interceptor.h>
27
28 #include <memory>
29 #include <string>
30 #include <utility>
31 #include <vector>
32
33 #include "src/cpp/client/create_channel_internal.h"
34
35 namespace grpc {
36
37 class ChannelArguments;
38
39 #ifdef GPR_SUPPORT_CHANNELS_FROM_FD
40
CreateInsecureChannelFromFd(const std::string & target,int fd)41 std::shared_ptr<Channel> CreateInsecureChannelFromFd(const std::string& target,
42 int fd) {
43 internal::GrpcLibrary init_lib;
44 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
45 auto channel = CreateChannelInternal(
46 "", grpc_channel_create_from_fd(target.c_str(), fd, creds, nullptr),
47 std::vector<
48 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
49 grpc_channel_credentials_release(creds);
50 return channel;
51 }
52
CreateCustomInsecureChannelFromFd(const std::string & target,int fd,const grpc::ChannelArguments & args)53 std::shared_ptr<Channel> CreateCustomInsecureChannelFromFd(
54 const std::string& target, int fd, const grpc::ChannelArguments& args) {
55 internal::GrpcLibrary init_lib;
56 grpc_channel_args channel_args;
57 args.SetChannelArgs(&channel_args);
58 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
59 auto channel = CreateChannelInternal(
60 "", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args),
61 std::vector<
62 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
63 grpc_channel_credentials_release(creds);
64 // Channel also initializes gRPC, so we can decrement the init ref count here.
65 return channel;
66 }
67
68 namespace experimental {
69
CreateCustomInsecureChannelWithInterceptorsFromFd(const std::string & target,int fd,const ChannelArguments & args,std::vector<std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> interceptor_creators)70 std::shared_ptr<Channel> CreateCustomInsecureChannelWithInterceptorsFromFd(
71 const std::string& target, int fd, const ChannelArguments& args,
72 std::vector<
73 std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
74 interceptor_creators) {
75 internal::GrpcLibrary init_lib;
76 grpc_channel_args channel_args;
77 args.SetChannelArgs(&channel_args);
78 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
79 auto channel = CreateChannelInternal(
80 "", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args),
81 std::move(interceptor_creators));
82 grpc_channel_credentials_release(creds);
83 return channel;
84 }
85
86 } // namespace experimental
87
88 #endif // GPR_SUPPORT_CHANNELS_FROM_FD
89
90 } // namespace grpc
91