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