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 "absl/strings/str_cat.h"
28
29 #include <grpc/support/alloc.h>
30
31 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/iomgr/endpoint.h"
34 #include "src/core/lib/iomgr/exec_ctx.h"
35 #include "src/core/lib/iomgr/tcp_posix.h"
36 #include "src/core/lib/surface/completion_queue.h"
37 #include "src/core/lib/surface/server.h"
38
grpc_server_add_insecure_channel_from_fd(grpc_server * server,void * reserved,int fd)39 void grpc_server_add_insecure_channel_from_fd(grpc_server* server,
40 void* reserved, int fd) {
41 GPR_ASSERT(reserved == nullptr);
42
43 grpc_core::ExecCtx exec_ctx;
44
45 std::string name = absl::StrCat("fd:", fd);
46 grpc_endpoint* server_endpoint =
47 grpc_tcp_create(grpc_fd_create(fd, name.c_str(), true),
48 grpc_server_get_channel_args(server), name.c_str());
49
50 const grpc_channel_args* server_args = grpc_server_get_channel_args(server);
51 grpc_transport* transport = grpc_create_chttp2_transport(
52 server_args, server_endpoint, false /* is_client */);
53
54 for (grpc_pollset* pollset : grpc_server_get_pollsets(server)) {
55 grpc_endpoint_add_to_pollset(server_endpoint, pollset);
56 }
57
58 grpc_server_setup_transport(server, transport, nullptr, server_args, nullptr);
59 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
60 }
61
62 #else // !GPR_SUPPORT_CHANNELS_FROM_FD
63
grpc_server_add_insecure_channel_from_fd(grpc_server * server,void * reserved,int fd)64 void grpc_server_add_insecure_channel_from_fd(grpc_server* server,
65 void* reserved, int fd) {
66 GPR_ASSERT(0);
67 }
68
69 #endif // GPR_SUPPORT_CHANNELS_FROM_FD
70