• 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       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);
49 
50   int flags = fcntl(fd, F_GETFL, 0);
51   GPR_ASSERT(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0);
52 
53   grpc_endpoint* client = grpc_tcp_client_create_from_fd(
54       grpc_fd_create(fd, "client", true), args, "fd-client");
55 
56   grpc_transport* transport =
57       grpc_create_chttp2_transport(final_args, client, true);
58   GPR_ASSERT(transport);
59   grpc_error* error = nullptr;
60   grpc_channel* channel =
61       grpc_channel_create(target, final_args, GRPC_CLIENT_DIRECT_CHANNEL,
62                           transport, nullptr, &error);
63   grpc_channel_args_destroy(final_args);
64   if (channel != nullptr) {
65     grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
66     grpc_core::ExecCtx::Get()->Flush();
67   } else {
68     intptr_t integer;
69     grpc_status_code status = GRPC_STATUS_INTERNAL;
70     if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
71       status = static_cast<grpc_status_code>(integer);
72     }
73     GRPC_ERROR_UNREF(error);
74     grpc_transport_destroy(transport);
75     channel = grpc_lame_client_channel_create(
76         target, status, "Failed to create client channel");
77   }
78 
79   return channel;
80 }
81 
82 #else  // !GPR_SUPPORT_CHANNELS_FROM_FD
83 
grpc_insecure_channel_create_from_fd(const char * target,int fd,const grpc_channel_args * args)84 grpc_channel* grpc_insecure_channel_create_from_fd(
85     const char* target, int fd, const grpc_channel_args* args) {
86   GPR_ASSERT(0);
87   return nullptr;
88 }
89 
90 #endif  // GPR_SUPPORT_CHANNELS_FROM_FD
91