1 /** 2 * Copyright 2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_DISTRIBUTED_RPC_RPC_CLIENT_BASE_H_ 18 #define MINDSPORE_CCSRC_DISTRIBUTED_RPC_RPC_CLIENT_BASE_H_ 19 20 #include <string> 21 #include <memory> 22 23 #include "include/backend/distributed/constants.h" 24 25 namespace mindspore { 26 namespace distributed { 27 namespace rpc { 28 class RPCClientBase { 29 public: RPCClientBase(bool enable_ssl)30 explicit RPCClientBase(bool enable_ssl) : enable_ssl_(enable_ssl) {} 31 virtual ~RPCClientBase() = default; 32 33 // Build or destroy the rpc client. Initialize()34 virtual bool Initialize() { return true; } Finalize()35 virtual void Finalize() {} 36 37 // Connect to the specified server. 38 // Function free_cb binds with client's each connection. It frees the real memory after message is sent to the peer. 39 virtual bool Connect( 40 const std::string &dst_url, size_t retry_count = 60, const MemFreeCallback &free_cb = [](void *data) { 41 MS_ERROR_IF_NULL(data); 42 delete static_cast<char *>(data); 43 return true; 44 }) { 45 return true; 46 } 47 48 // Check if the connection to dst_url has been established. IsConnected(const std::string & dst_url)49 virtual bool IsConnected(const std::string &dst_url) { return false; } 50 51 // Disconnect from the specified server. 52 virtual bool Disconnect(const std::string &dst_url, size_t timeout_in_sec = 5) { return true; } 53 54 // Send the message from the source to the destination synchronously and return the byte size by this method call. 55 virtual bool SendSync(std::unique_ptr<MessageBase> &&msg, size_t *const send_bytes = nullptr) { return true; } 56 57 // Send the message from the source to the destination asynchronously. SendAsync(std::unique_ptr<MessageBase> && msg)58 virtual void SendAsync(std::unique_ptr<MessageBase> &&msg) {} 59 60 virtual MessageBase *ReceiveSync(std::unique_ptr<MessageBase> &&msg, uint32_t timeout = 30) { return nullptr; } 61 62 // Force the data in the send buffer to be sent out. Flush(const std::string & dst_url)63 virtual bool Flush(const std::string &dst_url) { return true; } 64 65 protected: 66 bool enable_ssl_; 67 }; 68 } // namespace rpc 69 } // namespace distributed 70 } // namespace mindspore 71 72 #endif // MINDSPORE_CCSRC_DISTRIBUTED_RPC_RPC_CLIENT_BASE_H_ 73