1 /** 2 * Copyright 2022 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_TCP_TCP_SERVER_H_ 18 #define MINDSPORE_CCSRC_DISTRIBUTED_RPC_TCP_TCP_SERVER_H_ 19 20 #include <string> 21 #include <memory> 22 23 #include "include/backend/distributed/rpc/rpc_server_base.h" 24 #include "distributed/rpc/tcp/tcp_comm.h" 25 #include "utils/ms_utils.h" 26 #include "include/backend/visible.h" 27 28 namespace mindspore { 29 namespace distributed { 30 namespace rpc { 31 class TCPComm; 32 33 class BACKEND_EXPORT TCPServer : public RPCServerBase { 34 public: 35 explicit TCPServer(bool enable_ssl = false, const ServerPortRange &port_range = {}) RPCServerBase(enable_ssl,port_range)36 : RPCServerBase(enable_ssl, port_range), tcp_comm_(nullptr) {} 37 ~TCPServer() override = default; 38 39 // Init the tcp server using the specified url. 40 bool Initialize(const std::string &url, const MemAllocateCallback &allocate_cb = {}) override; 41 42 // Init the tcp server using local IP and random port. 43 bool Initialize(const MemAllocateCallback &allocate_cb = {}) override; 44 45 // Destroy the tcp server. 46 void Finalize() override; 47 48 // Set the message processing handler. 49 void SetMessageHandler(const MessageHandler &handler, uint32_t func_id = 0) override; 50 51 // Return the IP and port binded by this server. 52 std::string GetIP() const override; 53 uint32_t GetPort() const override; 54 55 private: 56 bool InitializeImpl(const std::string &url, const MemAllocateCallback &allocate_cb); 57 58 bool StartSocketWithinPortRange(const MemAllocateCallback &allocate_cb); 59 60 // The basic TCP communication component used by the server. 61 std::unique_ptr<TCPComm> tcp_comm_; 62 63 DISABLE_COPY_AND_ASSIGN(TCPServer); 64 }; 65 } // namespace rpc 66 } // namespace distributed 67 } // namespace mindspore 68 69 #endif 70