Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2026 by Thun Lu. All rights reserved. 3 : : * Author: Thun Lu <thun.lu@zohomail.cn> 4 : : * Repo: https://github.com/thun-res/vlink 5 : : * _ __ __ _ __ 6 : : * | | / / / / (_) ____ / /__ 7 : : * | | / / / / / / / __ \ / //_/ 8 : : * | |/ / / /___ / / / / / / / ,< 9 : : * |___/ /_____/ /_/ /_/ /_/ /_/|_| 10 : : * 11 : : * Licensed under the Apache License, Version 2.0 (the "License"); 12 : : * you may not use this file except in compliance with the License. 13 : : * You may obtain a copy of the License at 14 : : * 15 : : * http://www.apache.org/licenses/LICENSE-2.0 16 : : * 17 : : * Unless required by applicable law or agreed to in writing, software 18 : : * distributed under the License is distributed on an "AS IS" BASIS, 19 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 : : * See the License for the specific language governing permissions and 21 : : * limitations under the License. 22 : : */ 23 : : 24 : : #include "./shm_server_impl.h" 25 : : 26 : : #include <utility> 27 : : 28 : : #include "./base/helpers.h" 29 : : 30 : : namespace vlink { 31 : : 32 : : // ShmServerImpl 33 [ + - + - ]: 20 : ShmServerImpl::ShmServerImpl(const ShmConf& conf) : conf_(conf) {} 34 : : 35 : 20 : void ShmServerImpl::init() { 36 [ + + + - : 20 : static auto& factory = ShmFactory::get(); + - - - ] 37 : : 38 : 20 : conf_.hash_code = Helpers::get_hash_code(conf_.event); 39 : : 40 : : object_ = 41 [ + - ]: 20 : factory.get_object<Object>({kImplType, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait}); 42 : : 43 : 20 : object_->add_impl(this); 44 : 20 : } 45 : : 46 : 20 : void ShmServerImpl::deinit() { 47 : 20 : detach(); 48 : : 49 : 20 : object_->remove_impl(this); 50 : 20 : } 51 : : 52 : 1 : bool ShmServerImpl::suspend() { return object_->suspend(); } 53 : : 54 : 1 : bool ShmServerImpl::resume() { return object_->resume(); } 55 : : 56 : 2 : bool ShmServerImpl::is_suspend() const { return object_->is_suspend(); } 57 : : 58 : 21 : bool ShmServerImpl::is_support_loan() const { return true; } 59 : : 60 : 16 : Bytes ShmServerImpl::loan(int64_t size) { 61 [ + - + + : 16 : if VUNLIKELY (is_resp_type && !is_sync_type) { + + ] 62 : 2 : return Bytes(); 63 : : } 64 : : 65 : 14 : return object_->loan(static_cast<uint64_t>(conf_.hash_code), size); 66 : : } 67 : : 68 : 1 : bool ShmServerImpl::return_loan(const Bytes& bytes) { return object_->release(bytes); } 69 : : 70 : 17 : const Conf* ShmServerImpl::get_conf() const { return &conf_; } 71 : : 72 : 2 : const AbstractNode* ShmServerImpl::get_abstract_node() const { return object_.get(); } 73 : : 74 : 0 : bool ShmServerImpl::has_clients() const { return object_->has_clients(); } 75 : : 76 : 18 : bool ShmServerImpl::listen(ReqRespCallback&& callback) { 77 : 18 : object_->register_req_resp_callback(this, std::move(callback)); 78 : 18 : object_->start(); 79 : : 80 : 18 : return true; 81 : : } 82 : : 83 : 16 : bool ShmServerImpl::reply(uint64_t req_id, const Bytes& resp_data, bool is_sync) { 84 : : (void)req_id; 85 : : 86 [ + + ]: 16 : if VUNLIKELY (!is_sync) { 87 [ + - + - ]: 4 : VLOG_W("Function [reply] is not supported."); 88 : 2 : return false; 89 : : } 90 : : 91 : 14 : object_->reply(static_cast<uint64_t>(conf_.hash_code), resp_data); 92 : : 93 : 14 : return true; 94 : : } 95 : : 96 : : } // namespace vlink