• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/support/port_platform.h>
20 
21 #include <grpc/grpc.h>
22 #include <grpc/grpc_posix.h>
23 #include <grpc/support/log.h>
24 
25 #ifdef GPR_SUPPORT_CHANNELS_FROM_FD
26 
27 #include <fcntl.h>
28 
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"
37 
grpc_insecure_channel_create_from_fd(const char * target,int fd,const grpc_channel_args * args)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,
42                  (target, fd, args));
43 
44   grpc_arg default_authority_arg = grpc_channel_arg_string_create(
45       (char*)GRPC_ARG_DEFAULT_AUTHORITY, (char*)"test.authority");
46   grpc_channel_args* final_args =
47       grpc_channel_args_copy_and_add(args, &default_authority_arg, 1);
48 
49   int flags = fcntl(fd, F_GETFL, 0);
50   GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0);
51 
52   grpc_endpoint* client = grpc_tcp_client_create_from_fd(
53       grpc_fd_create(fd, "client", true), args, "fd-client");
54 
55   grpc_transport* transport =
56       grpc_create_chttp2_transport(final_args, client, true);
57   GPR_ASSERT(transport);
58   grpc_channel* channel = grpc_channel_create(
59       target, final_args, GRPC_CLIENT_DIRECT_CHANNEL, transport);
60   grpc_channel_args_destroy(final_args);
61   grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
62 
63   grpc_core::ExecCtx::Get()->Flush();
64 
65   return channel != nullptr ? channel
66                             : grpc_lame_client_channel_create(
67                                   target, GRPC_STATUS_INTERNAL,
68                                   "Failed to create client channel");
69 }
70 
71 #else  // !GPR_SUPPORT_CHANNELS_FROM_FD
72 
grpc_insecure_channel_create_from_fd(const char * target,int fd,const grpc_channel_args * args)73 grpc_channel* grpc_insecure_channel_create_from_fd(
74     const char* target, int fd, const grpc_channel_args* args) {
75   GPR_ASSERT(0);
76   return nullptr;
77 }
78 
79 #endif  // GPR_SUPPORT_CHANNELS_FROM_FD
80