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_publisher_impl.h" 25 : : 26 : : #include <utility> 27 : : 28 : : #include "./base/helpers.h" 29 : : 30 : : namespace vlink { 31 : : 32 : : // ShmPublisherImpl 33 [ + - ]: 43 : ShmPublisherImpl::ShmPublisherImpl(const ShmConf& conf) : conf_(conf) {} 34 : : 35 : 43 : void ShmPublisherImpl::init() { 36 [ + + + - : 43 : static auto& factory = ShmFactory::get(); + - - - ] 37 : : 38 : 43 : conf_.hash_code = Helpers::get_hash_code(conf_.event); 39 : : 40 : : object_ = 41 [ + - ]: 43 : factory.get_object<Object>({kImplType, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait}); 42 : : 43 : 43 : object_->add_impl(this); 44 : : 45 [ + - + - ]: 43 : object_->register_sub_connect_callback(this, [this](bool) { 46 : 39 : auto* message_loop = get_message_loop(); 47 : : 48 [ + + ]: 39 : if (message_loop) { 49 [ + - + - ]: 2 : message_loop->post_task([this]() { PublisherImpl::update_subscribers(); }); 50 : : } else { 51 : 38 : PublisherImpl::update_subscribers(); 52 : : } 53 : 39 : }); 54 : : 55 : 43 : PublisherImpl::update_subscribers(); 56 : 43 : } 57 : : 58 : 43 : void ShmPublisherImpl::deinit() { 59 : 43 : object_->remove_impl(this); 60 : : 61 : 43 : detach(); 62 : 43 : } 63 : : 64 : 44 : bool ShmPublisherImpl::is_support_loan() const { return true; } 65 : : 66 : 128 : Bytes ShmPublisherImpl::loan(int64_t size) { return object_->loan(static_cast<uint64_t>(conf_.hash_code), size); } 67 : : 68 : 2 : bool ShmPublisherImpl::return_loan(const Bytes& bytes) { return object_->release(bytes); } 69 : : 70 : 0 : const Conf* ShmPublisherImpl::get_conf() const { return &conf_; } 71 : : 72 : 7 : const AbstractNode* ShmPublisherImpl::get_abstract_node() const { return object_.get(); } 73 : : 74 : 390 : bool ShmPublisherImpl::has_subscribers() const { return object_->has_subscribers(); } 75 : : 76 : 2 : void ShmPublisherImpl::detect_subscribers(ConnectCallback&& callback) { 77 : 2 : object_->enable_detect_timer(); 78 : : 79 : 2 : PublisherImpl::detect_subscribers(std::move(callback)); 80 : 2 : } 81 : : 82 : 38 : bool ShmPublisherImpl::wait_for_subscribers(std::chrono::milliseconds timeout) { 83 : 38 : object_->enable_detect_timer(); 84 : : 85 : 38 : return PublisherImpl::wait_for_subscribers(timeout); 86 : : } 87 : : 88 : 142 : bool ShmPublisherImpl::write(const Bytes& msg_data) { 89 : 142 : return object_->publish(static_cast<uint64_t>(conf_.hash_code), msg_data); 90 : : } 91 : : 92 : : } // namespace vlink