1 /** 2 * Copyright 2020 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_PS_CORE_COMMUNICATOR_TCP_MESSAGE_HANDLER_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_TCP_MESSAGE_HANDLER_H_ 19 20 #include <functional> 21 #include <iostream> 22 #include <string> 23 #include <memory> 24 #include <vector> 25 26 #include "utils/log_adapter.h" 27 #include "ps/core/communicator/message.h" 28 #include "proto/comm.pb.h" 29 #include "proto/ps.pb.h" 30 #include "utils/convert_utils_base.h" 31 #include "ps/constants.h" 32 33 namespace mindspore { 34 namespace ps { 35 namespace core { 36 using messageReceive = 37 std::function<void(const std::shared_ptr<MessageMeta> &, const Protos &, const void *, size_t size)>; 38 constexpr int kHeaderLen = 16; 39 40 class TcpMessageHandler { 41 public: TcpMessageHandler()42 TcpMessageHandler() 43 : is_parsed_(false), message_buffer_(nullptr), remaining_length_(0), header_index_(-1), last_copy_len_(0) {} 44 virtual ~TcpMessageHandler() = default; 45 46 void SetCallback(const messageReceive &cb); 47 void ReceiveMessage(const void *buffer, size_t num); 48 49 private: 50 messageReceive message_callback_; 51 bool is_parsed_; 52 std::unique_ptr<unsigned char[]> message_buffer_; 53 size_t remaining_length_; 54 unsigned char header_[16]{0}; 55 int header_index_; 56 size_t last_copy_len_; 57 MessageHeader message_header_; 58 std::string mBuffer; 59 }; 60 } // namespace core 61 } // namespace ps 62 } // namespace mindspore 63 64 #endif // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_TCP_MESSAGE_HANDLER_H_ 65