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 [ + - ]: 55 : ShmSubscriberImpl::ShmSubscriberImpl(const ShmConf& conf) : conf_(conf) {} 34 : : 35 : 55 : void ShmSubscriberImpl::init() { 36 [ + + + - : 55 : static auto& factory = ShmFactory::get(); + - - - ] 37 : : 38 : 55 : conf_.hash_code = Helpers::get_hash_code(conf_.event); 39 : : 40 : : object_ = 41 [ + - ]: 55 : factory.get_object<Object>({kImplType, conf_.address, conf_.domain, conf_.depth, conf_.history, conf_.wait}); 42 : : 43 : 55 : object_->add_impl(this); 44 : : 45 : 55 : object_->set_latency_and_lost_enabled(is_latency_and_lost_enabled_.load(std::memory_order_acquire)); 46 : 55 : } 47 : : 48 : 55 : void ShmSubscriberImpl::deinit() { 49 : 55 : detach(); 50 : : 51 : 55 : object_->remove_impl(this); 52 : 55 : } 53 : : 54 : 1 : bool ShmSubscriberImpl::suspend() { return object_->suspend(); } 55 : : 56 : 1 : bool ShmSubscriberImpl::resume() { return object_->resume(); } 57 : : 58 : 3 : bool ShmSubscriberImpl::is_suspend() const { return object_->is_suspend(); } 59 : : 60 : 56 : bool ShmSubscriberImpl::is_support_loan() const { return true; } 61 : : 62 : 1 : bool ShmSubscriberImpl::return_loan(const Bytes& bytes) { return object_->release(bytes); } 63 : : 64 : 258 : const Conf* ShmSubscriberImpl::get_conf() const { return &conf_; } 65 : : 66 : 3 : const AbstractNode* ShmSubscriberImpl::get_abstract_node() const { return object_.get(); } 67 : : 68 : 52 : bool ShmSubscriberImpl::listen(MsgCallback&& callback) { 69 : 52 : object_->register_msg_callback(this, std::move(callback)); 70 : 52 : object_->subscribe(); 71 : : 72 : 52 : return true; 73 : : } 74 : : 75 : 2 : void ShmSubscriberImpl::set_latency_and_lost_enabled(bool enable) { 76 : 2 : is_latency_and_lost_enabled_.store(enable, std::memory_order_release); 77 : : 78 [ + - ]: 2 : if (object_) { 79 : 2 : object_->set_latency_and_lost_enabled(enable); 80 : : } 81 : 2 : } 82 : : 83 : 2 : bool ShmSubscriberImpl::is_latency_and_lost_enabled() const { 84 [ + - ]: 2 : if (object_) { 85 : 2 : return object_->is_latency_and_lost_enabled(); 86 : : } 87 : : 88 : 0 : return is_latency_and_lost_enabled_.load(std::memory_order_acquire); 89 : : } 90 : : 91 : 1 : SampleLostInfo ShmSubscriberImpl::get_lost() const { 92 [ + - + - : 1 : if (object_ && object_->is_latency_and_lost_enabled()) { + - ] 93 : 1 : return SampleLostInfo{object_->get_calculate_sample().get_total(), object_->get_calculate_sample().get_lost()}; 94 : : } 95 : : 96 : 0 : return SampleLostInfo(); 97 : : } 98 : : 99 : : } // namespace vlink