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_subscriber_impl.h" 25 : : 26 : : #include <utility> 27 : : 28 : : #include "./base/helpers.h" 29 : : 30 : : namespace vlink { 31 : : 32 : : // ShmSubscriberImpl 33 [ + - ]: 65 : ShmSubscriberImpl::ShmSubscriberImpl(const ShmConf& conf) : conf_(conf) {} 34 : : 35 : 65 : void ShmSubscriberImpl::init() { 36 [ + + + - : 65 : static auto& factory = ShmFactory::get(); + - - - ] 37 : : 38 : 65 : conf_.hash_code = Helpers::get_hash_code(conf_.event); 39 : : 40 [ + + ]: 65 : if (init_impl_type == kGetter) { 41 [ + - ]: 1 : if (conf_.history == 0) { 42 : 1 : conf_.history = 1; 43 : : } 44 : : 45 [ + - ]: 2 : object_ = factory.get_object<Object>( 46 [ + - ]: 3 : {kGetter, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait, conf_.event, this}); 47 : : } else { 48 [ + - + - ]: 128 : object_ = factory.get_object<Object>( 49 : 192 : {kImplType, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait, std::string{}, nullptr}); 50 : : } 51 : : 52 : 65 : object_->add_impl(this); 53 : : 54 : 65 : object_->set_latency_and_lost_enabled(is_latency_and_lost_enabled_.load(std::memory_order_acquire)); 55 : 65 : } 56 : : 57 : 65 : void ShmSubscriberImpl::deinit() { 58 : 65 : detach(); 59 : : 60 [ + - ]: 65 : if (object_) { 61 : 65 : object_->remove_impl(this); 62 : : } 63 : 65 : } 64 : : 65 : 4 : bool ShmSubscriberImpl::suspend() { 66 : 4 : has_suspend.store(true, std::memory_order_release); 67 : : 68 : 4 : return true; 69 : : } 70 : : 71 : 7 : bool ShmSubscriberImpl::resume() { 72 : 7 : has_suspend.store(false, std::memory_order_release); 73 : : 74 : 7 : return true; 75 : : } 76 : : 77 : 9 : bool ShmSubscriberImpl::is_suspend() const { return has_suspend.load(std::memory_order_acquire); } 78 : : 79 : 66 : bool ShmSubscriberImpl::is_support_loan() const { return true; } 80 : : 81 : 4279 : const Conf* ShmSubscriberImpl::get_conf() const { return &conf_; } 82 : : 83 : 9 : const AbstractNode* ShmSubscriberImpl::get_abstract_node() const { return object_.get(); } 84 : : 85 : 62 : bool ShmSubscriberImpl::listen(MsgCallback&& callback) { 86 : 62 : object_->register_msg_callback(this, std::move(callback)); 87 : 62 : object_->subscribe(); 88 : : 89 : 62 : return true; 90 : : } 91 : : 92 : 2 : void ShmSubscriberImpl::set_latency_and_lost_enabled(bool enable) { 93 : 2 : is_latency_and_lost_enabled_.store(enable, std::memory_order_release); 94 : : 95 [ + - ]: 2 : if (object_) { 96 : 2 : object_->set_latency_and_lost_enabled(enable); 97 : : } 98 : 2 : } 99 : : 100 : 2 : bool ShmSubscriberImpl::is_latency_and_lost_enabled() const { 101 [ + - ]: 2 : if (object_) { 102 : 2 : return object_->is_latency_and_lost_enabled(); 103 : : } 104 : : 105 : 0 : return is_latency_and_lost_enabled_.load(std::memory_order_acquire); 106 : : } 107 : : 108 : 1 : SampleLostInfo ShmSubscriberImpl::get_lost() const { 109 [ + - + - : 1 : if (object_ && object_->is_latency_and_lost_enabled()) { + - ] 110 : 1 : return SampleLostInfo{object_->get_calculate_sample().get_total(), object_->get_calculate_sample().get_lost()}; 111 : : } 112 : : 113 : 0 : return SampleLostInfo(); 114 : : } 115 : : 116 : : } // namespace vlink