| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #ifndef _HDFS_LIBHDFS3_CLIENT_PACKET_H_ |
| 29 | #define _HDFS_LIBHDFS3_CLIENT_PACKET_H_ |
| 30 | |
| 31 | #include <stdint.h> |
| 32 | #include <vector> |
| 33 | |
| 34 | #define HEART_BEAT_SEQNO -1 |
| 35 | |
| 36 | namespace Hdfs { |
| 37 | namespace Internal { |
| 38 | |
| 39 | class ConstPacketBuffer { |
| 40 | public: |
| 41 | ConstPacketBuffer(const char * buf, int size) : |
| 42 | buffer(buf), size(size) { |
| 43 | } |
| 44 | |
| 45 | const char * getBuffer() const { |
| 46 | return buffer; |
| 47 | } |
| 48 | |
| 49 | const int getSize() const { |
| 50 | return size; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | const char * buffer; |
| 55 | const int size; |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * buffer is pointed into like follows: |
| 60 | * (C is checksum data, D is payload data) |
| 61 | * |
| 62 | * [HHHHHCCCCC________________DDDDDDDDDDDDDDDD___] |
| 63 | * ^ ^ ^ ^ |
| 64 | * | checksumPos dataStart dataPos |
| 65 | * checksumStart |
| 66 | */ |
| 67 | class Packet { |
| 68 | public: |
| 69 | /** |
| 70 | * create a heart beat packet |
| 71 | */ |
| 72 | Packet(); |
| 73 | |
| 74 | /** |
| 75 | * create a new packet |
| 76 | */ |
| 77 | Packet(int pktSize, int chunksPerPkt, int64_t offsetInBlock, int64_t seqno, int checksumSize); |
| 78 | |
| 79 | void reset(int pktSize, int chunksPerPkt, int64_t offsetInBlock, int64_t seqno, int checksumSize); |
| 80 | |
| 81 | void addChecksum(uint32_t checksum); |
| 82 | |
| 83 | void addData(const char * buf, int size); |
| 84 | |
| 85 | void setSyncFlag(bool sync); |
| 86 | |
| 87 | void increaseNumChunks(); |
| 88 | |
| 89 | bool isFull(); |
| 90 | |
| 91 | bool isHeartbeat(); |
| 92 | |
| 93 | void setLastPacketInBlock(bool lastPacket); |
| 94 | |
| 95 | int getDataSize(); |
| 96 | |
| 97 | const ConstPacketBuffer getBuffer(); |
| 98 | |
| 99 | int64_t getLastByteOffsetBlock(); |
| 100 | |
| 101 | int64_t getSeqno() const { |
| 102 | return seqno; |
| 103 | } |
| 104 | |
| 105 | bool isLastPacketInBlock() const { |
| 106 | return lastPacketInBlock; |
| 107 | } |
| 108 | |
| 109 | int64_t getOffsetInBlock() const { |
| 110 | return offsetInBlock; |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | bool lastPacketInBlock; // is this the last packet in block |
| 115 | bool syncBlock; // sync block to disk? |
| 116 | int checksumPos; |
| 117 | int checksumSize; |
| 118 | int checksumStart; |
| 119 | int dataPos; |
| 120 | int dataStart; |
| 121 | int ; |
| 122 | int maxChunks; // max chunks in packet |
| 123 | int numChunks; // number of chunks currently in packet |
| 124 | int64_t offsetInBlock; // offset in block |
| 125 | int64_t seqno; // sequence number of packet in block |
| 126 | std::vector<char> buffer; |
| 127 | }; |
| 128 | |
| 129 | } |
| 130 | } |
| 131 | #endif /* _HDFS_LIBHDFS3_CLIENT_PACKET_H_ */ |
| 132 | |