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