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 "./intra_client_impl.h" 25 : : 26 : : #include <utility> 27 : : 28 : : #include "./base/elapsed_timer.h" 29 : : #include "./base/helpers.h" 30 : : 31 : : namespace vlink { 32 : : 33 : : // IntraClientImpl 34 [ + - ]: 61 : IntraClientImpl::IntraClientImpl(const IntraConf& conf) : conf_(conf) { 35 [ + + ]: 61 : if (conf.type == "direct") { 36 : 27 : type_ = IntraType::kDirect; 37 : : } 38 : 61 : } 39 : : 40 : 58 : void IntraClientImpl::init() { 41 [ + + + - : 58 : static auto& factory = IntraFactory::get(); + - - - ] 42 : : 43 : 58 : conf_.hash_code = Helpers::get_hash_code(conf_.event); 44 : : 45 [ + - ]: 58 : object_ = factory.get_object<Object>({kImplType, conf_.address, conf_.pipeline, type_, conf_.hash_code}); 46 : : 47 : 58 : object_->add_impl(this); 48 : : 49 [ + - + - ]: 71 : object_->register_server_connect_callback(this, [this](bool) { ClientImpl::update_connected(); }); 50 : : 51 : 58 : ClientImpl::update_connected(); 52 : 58 : } 53 : : 54 : 58 : void IntraClientImpl::deinit() { 55 : 58 : detach(); 56 : 58 : object_->remove_impl(this); 57 : 58 : } 58 : : 59 : 58 : void IntraClientImpl::interrupt() { 60 : 58 : ClientImpl::interrupt(); 61 : : 62 : 58 : ack_manager_.clear(); 63 : 58 : } 64 : : 65 : 0 : const Conf* IntraClientImpl::get_conf() const { return &conf_; } 66 : : 67 : 1 : const AbstractNode* IntraClientImpl::get_abstract_node() const { return object_.get(); } 68 : : 69 : 182 : bool IntraClientImpl::is_connected() const { return !object_->req_resp_map_is_empty(); } 70 : : 71 : 99 : bool IntraClientImpl::call(const Bytes& req_data, MsgCallback&& callback, std::chrono::milliseconds timeout) { 72 [ + + ]: 99 : if VUNLIKELY (!callback) { 73 [ + - ]: 15 : return object_->call(this, type_, conf_.hash_code, req_data); 74 : : } 75 : : 76 [ + + ]: 84 : if (timeout.count() != 0) { 77 : 53 : ack_manager_.reset_interrupted(); 78 : : 79 : 53 : ElapsedTimer timer; 80 : 53 : timer.start(); 81 : : 82 [ + - + + ]: 53 : if (!wait_for_connected(timeout)) { 83 : 4 : return false; 84 : : } 85 : : 86 : 49 : auto elapsed = timer.get(); 87 : : 88 [ + + - + : 49 : if (timeout.count() > 0 && elapsed >= timeout.count()) { - + ] 89 : 0 : return false; 90 : : } 91 : : 92 : 49 : auto ack_request = ack_manager_.create_request(); 93 : : 94 : 49 : auto ack_function = [ack_request, callback = std::move(callback)](const Bytes& resp_data) mutable { 95 [ + - ]: 95 : AckManager::notify(ack_request, [&callback, &resp_data]() { callback(resp_data); }); 96 : 146 : }; 97 : : 98 [ + - ]: 147 : return ack_manager_.process( 99 : 98 : ack_request, timeout.count() - elapsed, [this, &req_data, ack_function = std::move(ack_function)]() mutable { 100 [ + - ]: 49 : return object_->call(this, type_, conf_.hash_code, req_data, std::move(ack_function), true); 101 : 49 : }); 102 : 53 : } 103 : : 104 : 31 : return object_->call(this, type_, conf_.hash_code, req_data, std::move(callback)); 105 : : } 106 : : 107 : : } // namespace vlink