1 /*
2 *
3 * Copyright 2019 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 "src/cpp/server/external_connection_acceptor_impl.h"
20
21 #include <memory>
22
23 #include <grpcpp/server_builder.h>
24 #include <grpcpp/support/channel_arguments.h>
25
26 namespace grpc {
27 namespace internal {
28 namespace {
29 // The actual type to return to user. It co-owns the internal impl object with
30 // the server.
31 class AcceptorWrapper : public experimental::ExternalConnectionAcceptor {
32 public:
AcceptorWrapper(std::shared_ptr<ExternalConnectionAcceptorImpl> impl)33 explicit AcceptorWrapper(std::shared_ptr<ExternalConnectionAcceptorImpl> impl)
34 : impl_(std::move(impl)) {}
HandleNewConnection(NewConnectionParameters * p)35 void HandleNewConnection(NewConnectionParameters* p) override {
36 impl_->HandleNewConnection(p);
37 }
38
39 private:
40 std::shared_ptr<ExternalConnectionAcceptorImpl> impl_;
41 };
42 } // namespace
43
ExternalConnectionAcceptorImpl(const std::string & name,ServerBuilder::experimental_type::ExternalConnectionType type,std::shared_ptr<ServerCredentials> creds)44 ExternalConnectionAcceptorImpl::ExternalConnectionAcceptorImpl(
45 const std::string& name,
46 ServerBuilder::experimental_type::ExternalConnectionType type,
47 std::shared_ptr<ServerCredentials> creds)
48 : name_(name), creds_(std::move(creds)) {
49 GPR_ASSERT(type ==
50 ServerBuilder::experimental_type::ExternalConnectionType::FROM_FD);
51 }
52
53 std::unique_ptr<experimental::ExternalConnectionAcceptor>
GetAcceptor()54 ExternalConnectionAcceptorImpl::GetAcceptor() {
55 grpc_core::MutexLock lock(&mu_);
56 GPR_ASSERT(!has_acceptor_);
57 has_acceptor_ = true;
58 return std::unique_ptr<experimental::ExternalConnectionAcceptor>(
59 new AcceptorWrapper(shared_from_this()));
60 }
61
HandleNewConnection(experimental::ExternalConnectionAcceptor::NewConnectionParameters * p)62 void ExternalConnectionAcceptorImpl::HandleNewConnection(
63 experimental::ExternalConnectionAcceptor::NewConnectionParameters* p) {
64 grpc_core::MutexLock lock(&mu_);
65 if (shutdown_ || !started_) {
66 // TODO(yangg) clean up.
67 gpr_log(
68 GPR_ERROR,
69 "NOT handling external connection with fd %d, started %d, shutdown %d",
70 p->fd, started_, shutdown_);
71 return;
72 }
73 if (handler_) {
74 handler_->Handle(p->listener_fd, p->fd, p->read_buffer.c_buffer());
75 }
76 }
77
Shutdown()78 void ExternalConnectionAcceptorImpl::Shutdown() {
79 grpc_core::MutexLock lock(&mu_);
80 shutdown_ = true;
81 }
82
Start()83 void ExternalConnectionAcceptorImpl::Start() {
84 grpc_core::MutexLock lock(&mu_);
85 GPR_ASSERT(!started_);
86 GPR_ASSERT(has_acceptor_);
87 GPR_ASSERT(!shutdown_);
88 started_ = true;
89 }
90
SetToChannelArgs(ChannelArguments * args)91 void ExternalConnectionAcceptorImpl::SetToChannelArgs(ChannelArguments* args) {
92 args->SetPointer(name_.c_str(), &handler_);
93 }
94
95 } // namespace internal
96 } // namespace grpc
97