1 /* 2 * Copyright (c) 2021 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 FRAMEHEADER_H 17 #define FRAMEHEADER_H 18 19 #include <cstdint> 20 #include "communicator_type_define.h" 21 22 namespace DistributedDB { 23 /* 24 * packetType: Bit0: FragmentFlag: 1: Fragmented 0: Not Fragmented 25 * Bit1~2: Reserved 26 * Bit3: Communication negotiation mark 0:support 1:not support 27 * Bit4~7: FrameType 28 */ 29 struct CommPhyHeader { 30 uint16_t magic = 0; // Magic code to discern byte stream 31 uint16_t version = 0; // Version to differentiate fields layout 32 uint32_t packetLen = 0; // Length of total packet, include CommHeader and Padding 33 uint64_t checkSum = 0; // Check sum of data that follows CommPhyHeader 34 uint64_t sourceId = 0; // Indicate where this packet from 35 uint32_t frameId = 0; // FrameId to identify frame 36 uint8_t packetType = 0; // Some bits works individually, the high four bits indicates frameType 37 uint8_t paddingLen = 0; // Unit byte, range from 0 to 7. 38 uint16_t dbIntVer = 0; // Auxiliary info to help recognize db version in the future 39 }; 40 41 /* 42 * Whether a physical packet contains CommPhyOptHeader depend on FragmentFlag of packetType in CommPhyHeader 43 */ 44 struct CommPhyOptHeader { 45 uint32_t frameLen = 0; // Indicate length of frame before fragmentation. Frame include CommHeader no padding 46 uint16_t fragCount = 0; // Indicate how many fragments this frame is divided into 47 uint16_t fragNo = 0; // Indicate which fragment this packet is. start from 0. 48 }; 49 50 /* 51 * Whether a physical packet contains CommDivergeHeader depend on FrameType of packetType in CommPhyHeader 52 */ 53 struct CommDivergeHeader { 54 uint16_t version = 0; // Version to differentiate fields layout 55 uint16_t reserved = 0; // Reserved for future usage 56 uint32_t payLoadLen = 0; // Indicate length of data that follows CommDivergeHeader 57 uint8_t commLabel[COMM_LABEL_LENGTH] = {0}; // Indicate which communicator to hand out this frame 58 }; 59 60 /* 61 * MessageHeader used to describe a message 62 */ 63 struct MessageHeader { 64 uint16_t version = 0; // Version to differentiate fields layout 65 uint16_t messageType = 0; // Distinguish request/response/notify 66 uint32_t messageId = 0; // Indicate message command 67 uint32_t sessionId = 0; // For matching request and response 68 uint32_t sequenceId = 0; // Sequence of message 69 uint32_t errorNo = 0; // Indicate no error when zero 70 uint32_t dataLen = 0; // Indicate length of data that follows MessageHeader 71 }; 72 } // namespace DistributedDB 73 74 #endif // FRAMEHEADER_H 75