| 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 <grpc/impl/codegen/grpc_types.h> |
| 28 |
#include <grpcpp/channel.h> |
| 29 |
#include <grpcpp/impl/grpc_library.h> |
| 30 |
#include <grpcpp/support/channel_arguments.h> |
| 31 |
#include <grpcpp/support/client_interceptor.h> |
| 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 |
|
| 41 |
std::shared_ptr<Channel> CreateInsecureChannelFromFd(const std::string& target, |
| 42 |
int fd) { |
| 43 |
internal::GrpcLibrary init_lib; |
| 44 |
init_lib.init(); |
| 45 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 46 |
auto channel = CreateChannelInternal( |
| 47 |
"", grpc_channel_create_from_fd(target.c_str(), fd, creds, nullptr), |
| 48 |
std::vector< |
| 49 |
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>()); |
| 50 |
grpc_channel_credentials_release(creds); |
| 51 |
// Channel also initializes gRPC, so we can decrement the init ref count here. |
| 52 |
init_lib.shutdown(); |
| 53 |
return channel; |
| 54 |
} |
| 55 |
|
| 56 |
std::shared_ptr<Channel> CreateCustomInsecureChannelFromFd( |
| 57 |
const std::string& target, int fd, const grpc::ChannelArguments& args) { |
| 58 |
internal::GrpcLibrary init_lib; |
| 59 |
init_lib.init(); |
| 60 |
grpc_channel_args channel_args; |
| 61 |
args.SetChannelArgs(&channel_args); |
| 62 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 63 |
auto channel = CreateChannelInternal( |
| 64 |
"", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args), |
| 65 |
std::vector< |
| 66 |
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>()); |
| 67 |
grpc_channel_credentials_release(creds); |
| 68 |
// Channel also initializes gRPC, so we can decrement the init ref count here. |
| 69 |
init_lib.shutdown(); |
| 70 |
return channel; |
| 71 |
} |
| 72 |
|
| 73 |
namespace experimental { |
| 74 |
|
| 75 |
std::shared_ptr<Channel> CreateCustomInsecureChannelWithInterceptorsFromFd( |
| 76 |
const std::string& target, int fd, const ChannelArguments& args, |
| 77 |
std::vector< |
| 78 |
std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> |
| 79 |
interceptor_creators) { |
| 80 |
internal::GrpcLibrary init_lib; |
| 81 |
init_lib.init(); |
| 82 |
grpc_channel_args channel_args; |
| 83 |
args.SetChannelArgs(&channel_args); |
| 84 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 85 |
auto channel = CreateChannelInternal( |
| 86 |
"", grpc_channel_create_from_fd(target.c_str(), fd, creds, &channel_args), |
| 87 |
std::move(interceptor_creators)); |
| 88 |
grpc_channel_credentials_release(creds); |
| 89 |
// Channel also initializes gRPC, so we can decrement the init ref count here. |
| 90 |
init_lib.shutdown(); |
| 91 |
return channel; |
| 92 |
} |
| 93 |
|
| 94 |
} // namespace experimental |
| 95 |
|
| 96 |
#endif // GPR_SUPPORT_CHANNELS_FROM_FD |
| 97 |
|
| 98 |
} // namespace grpc |