3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_posix.h>
23 #include <grpc/support/log.h>
25 #ifdef GPR_SUPPORT_CHANNELS_FROM_FD
29 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/iomgr/endpoint.h"
32 #include "src/core/lib/iomgr/tcp_client_posix.h"
33 #include "src/core/lib/iomgr/tcp_posix.h"
34 #include "src/core/lib/surface/api_trace.h"
35 #include "src/core/lib/surface/channel.h"
36 #include "src/core/lib/transport/transport.h"
38 grpc_channel* grpc_insecure_channel_create_from_fd(
39 const char* target, int fd, const grpc_channel_args* args) {
40 grpc_core::ExecCtx exec_ctx;
41 GRPC_API_TRACE("grpc_insecure_channel_create(target=%p, fd=%d, args=%p)", 3,
44 grpc_arg default_authority_arg = grpc_channel_arg_string_create(
45 const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
46 const_cast<char*>("test.authority"));
47 grpc_channel_args* final_args =
48 grpc_channel_args_copy_and_add(args, &default_authority_arg, 1);
50 int flags = fcntl(fd, F_GETFL, 0);
51 GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0);
52 grpc_resource_quota* resource_quota =
53 grpc_resource_quota_from_channel_args(args, true);
54 grpc_slice_allocator* allocator = grpc_slice_allocator_create(
55 resource_quota, "fd-client:endpoint", final_args);
56 grpc_endpoint* client = grpc_tcp_client_create_from_fd(
57 grpc_fd_create(fd, "client", true), args, "fd-client", allocator);
58 grpc_transport* transport = grpc_create_chttp2_transport(
59 final_args, client, true,
60 grpc_resource_user_create(resource_quota, "fd-client:transport"));
61 grpc_resource_quota_unref_internal(resource_quota);
62 GPR_ASSERT(transport);
63 grpc_error_handle error = GRPC_ERROR_NONE;
64 grpc_channel* channel =
65 grpc_channel_create(target, final_args, GRPC_CLIENT_DIRECT_CHANNEL,
66 transport, nullptr, 0, &error);
67 grpc_channel_args_destroy(final_args);
68 if (channel != nullptr) {
69 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
70 grpc_core::ExecCtx::Get()->Flush();
73 grpc_status_code status = GRPC_STATUS_INTERNAL;
74 if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
75 status = static_cast<grpc_status_code>(integer);
77 GRPC_ERROR_UNREF(error);
78 grpc_transport_destroy(transport);
79 channel = grpc_lame_client_channel_create(
80 target, status, "Failed to create client channel");
86 #else // !GPR_SUPPORT_CHANNELS_FROM_FD
88 grpc_channel* grpc_insecure_channel_create_from_fd(
89 const char* /* target */, int /* fd */,
90 const grpc_channel_args* /* args */) {
95 #endif // GPR_SUPPORT_CHANNELS_FROM_FD