Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RpcServer" |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
| 21 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 22 | #include <thread> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include <binder/Parcel.h> |
| 26 | #include <binder/RpcServer.h> |
| 27 | #include <log/log.h> |
| 28 | #include "RpcState.h" |
| 29 | |
| 30 | #include "RpcWireFormat.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | RpcServer::RpcServer() {} |
| 35 | RpcServer::~RpcServer() {} |
| 36 | |
| 37 | sp<RpcServer> RpcServer::make() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 38 | return sp<RpcServer>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() { |
| 42 | mAgreedExperimental = true; |
| 43 | } |
| 44 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 45 | void RpcServer::setMaxThreads(size_t threads) { |
| 46 | LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads"); |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 47 | { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 48 | // this lock should only ever be needed in the error case |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 49 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 50 | LOG_ALWAYS_FATAL_IF(mConnections.size() > 0, |
| 51 | "Must specify max threads before creating a connection"); |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 52 | } |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 53 | mMaxThreads = threads; |
| 54 | } |
| 55 | |
| 56 | size_t RpcServer::getMaxThreads() { |
| 57 | return mMaxThreads; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void RpcServer::setRootObject(const sp<IBinder>& binder) { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 61 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 62 | mRootObject = binder; |
| 63 | } |
| 64 | |
| 65 | sp<IBinder> RpcServer::getRootObject() { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 66 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 67 | return mRootObject; |
| 68 | } |
| 69 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame^] | 70 | sp<RpcConnection> RpcServer::addClientConnection() { |
| 71 | LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); |
| 72 | |
| 73 | auto connection = RpcConnection::make(); |
| 74 | connection->setForServer(sp<RpcServer>::fromExisting(this)); |
| 75 | { |
| 76 | std::lock_guard<std::mutex> _l(mLock); |
| 77 | LOG_ALWAYS_FATAL_IF(mStarted, |
| 78 | "currently only supports adding client connections at creation time"); |
| 79 | mConnections.push_back(connection); |
| 80 | } |
| 81 | return connection; |
| 82 | } |
| 83 | |
| 84 | void RpcServer::join() { |
| 85 | std::vector<std::thread> pool; |
| 86 | { |
| 87 | std::lock_guard<std::mutex> _l(mLock); |
| 88 | mStarted = true; |
| 89 | for (const sp<RpcConnection>& connection : mConnections) { |
| 90 | for (size_t i = 0; i < mMaxThreads; i++) { |
| 91 | pool.push_back(std::thread([=] { connection->join(); })); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // TODO(b/185167543): don't waste extra thread for join, and combine threads |
| 97 | // between clients |
| 98 | for (auto& t : pool) t.join(); |
| 99 | } |
| 100 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 101 | } // namespace android |