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 <grpcpp/server_builder.h>
22 #include <grpcpp/support/byte_buffer.h>
23 #include <grpcpp/support/channel_arguments.h>
24
25 #include <memory>
26 #include <utility>
27
28 #include "absl/log/check.h"
29 #include "absl/log/log.h"
30
31 namespace grpc {
32 namespace internal {
33 namespace {
34 // The actual type to return to user. It co-owns the internal impl object with
35 // the server.
36 class AcceptorWrapper : public experimental::ExternalConnectionAcceptor {
37 public:
AcceptorWrapper(std::shared_ptr<ExternalConnectionAcceptorImpl> impl)38 explicit AcceptorWrapper(std::shared_ptr<ExternalConnectionAcceptorImpl> impl)
39 : impl_(std::move(impl)) {}
HandleNewConnection(NewConnectionParameters * p)40 void HandleNewConnection(NewConnectionParameters* p) override {
41 impl_->HandleNewConnection(p);
42 }
43
44 private:
45 std::shared_ptr<ExternalConnectionAcceptorImpl> impl_;
46 };
47 } // namespace
48
ExternalConnectionAcceptorImpl(const std::string & name,ServerBuilder::experimental_type::ExternalConnectionType type,std::shared_ptr<ServerCredentials> creds)49 ExternalConnectionAcceptorImpl::ExternalConnectionAcceptorImpl(
50 const std::string& name,
51 ServerBuilder::experimental_type::ExternalConnectionType type,
52 std::shared_ptr<ServerCredentials> creds)
53 : name_(name), creds_(std::move(creds)) {
54 CHECK(type ==
55 ServerBuilder::experimental_type::ExternalConnectionType::FROM_FD);
56 }
57
58 std::unique_ptr<experimental::ExternalConnectionAcceptor>
GetAcceptor()59 ExternalConnectionAcceptorImpl::GetAcceptor() {
60 grpc_core::MutexLock lock(&mu_);
61 CHECK(!has_acceptor_);
62 has_acceptor_ = true;
63 return std::unique_ptr<experimental::ExternalConnectionAcceptor>(
64 new AcceptorWrapper(shared_from_this()));
65 }
66
HandleNewConnection(experimental::ExternalConnectionAcceptor::NewConnectionParameters * p)67 void ExternalConnectionAcceptorImpl::HandleNewConnection(
68 experimental::ExternalConnectionAcceptor::NewConnectionParameters* p) {
69 grpc_core::MutexLock lock(&mu_);
70 if (shutdown_ || !started_) {
71 // TODO(yangg) clean up.
72 LOG(ERROR) << "NOT handling external connection with fd " << p->fd
73 << ", started " << started_ << ", shutdown " << 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 CHECK(!started_);
89 CHECK(has_acceptor_);
90 CHECK(!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