1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DPROF_LIBDPROF_DPROF_IPC_IPC_MESSAGE_H 17 #define DPROF_LIBDPROF_DPROF_IPC_IPC_MESSAGE_H 18 19 #include <vector> 20 #include <unordered_map> 21 #include <cstdint> 22 #include "macros.h" 23 24 namespace panda::dprof::ipc { 25 class Message { 26 public: 27 static const uint32_t MAX_DATA_SIZE = 1024 * 1024; // 1MB 28 29 enum class Id : uint8_t { 30 VERSION = 0x00, 31 APP_INFO = 0x01, 32 FEATURE_DATA = 0x02, 33 INVALID_ID = 0xff, 34 }; 35 36 Message() = default; 37 DEFAULT_MOVE_SEMANTIC(Message); 38 39 template <typename T> Message(Id id,T && data)40 Message(Id id, T &&data) : id_(id), data_(std::forward<T>(data)) 41 { 42 } 43 44 ~Message() = default; 45 GetId()46 Id GetId() const 47 { 48 return id_; 49 } 50 GetData()51 const uint8_t *GetData() const 52 { 53 return data_.data(); 54 } 55 GetSize()56 size_t GetSize() const 57 { 58 return data_.size(); 59 } 60 61 private: 62 Id id_ = Id::INVALID_ID; 63 std::vector<uint8_t> data_; 64 65 NO_COPY_SEMANTIC(Message); 66 }; 67 68 bool SendMessage(int fd, const Message &message); 69 int RecvMessage(int fd, Message &message); 70 } // namespace panda::dprof::ipc 71 72 #endif // DPROF_LIBDPROF_DPROF_IPC_IPC_MESSAGE_H 73