|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * Copyright (c) 2014 Rei <[email protected]> |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | + * SOFTWARE. |
| 20 | + * |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include <cstdint> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | +* This class encapsulates the messages received from the TUN/TAP or radio interface |
| 31 | +*/ |
| 32 | +class Message { |
| 33 | + public: |
| 34 | + Message() : |
| 35 | + payload_({0}), |
| 36 | + length_(0), |
| 37 | + seqNo_(0) {}; |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the length of the message in bytes. |
| 41 | + * @return The byte size of the message |
| 42 | + */ |
| 43 | + std::size_t getLength() { |
| 44 | + return length_; |
| 45 | + }; |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the sequence number of the message |
| 49 | + * @return The sequence number |
| 50 | + */ |
| 51 | + uint8_t getSeqNo() { |
| 52 | + return seqNo_; |
| 53 | + }; |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the payload of the message as a std::string |
| 57 | + * @return the payload as string |
| 58 | + */ |
| 59 | + std::string getPayloadStr() { |
| 60 | + return std::string(payload_.begin(), payload_.end()); |
| 61 | + }; |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the payload of the message as a char array |
| 65 | + * @return the payload as a c-string |
| 66 | + */ |
| 67 | + const char* getPayload() { |
| 68 | + const char* res = payload_.data(); |
| 69 | + return res; |
| 70 | + }; |
| 71 | + |
| 72 | + /** |
| 73 | + * Set the sequence number of the message |
| 74 | + * @param sn The new sequence number |
| 75 | + */ |
| 76 | + void setSeqNo(uint8_t sn) { |
| 77 | + seqNo_ = sn; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set the payload of the message |
| 82 | + * @param buffer char array as the payload |
| 83 | + * @param bsize size of the buffer |
| 84 | + */ |
| 85 | + void setPayload(char * buffer, std::size_t bsize) { |
| 86 | + payload_.clear(); |
| 87 | + payload_.assign(buffer, buffer + bsize); |
| 88 | + length_ = payload_.size(); |
| 89 | + }; |
| 90 | + |
| 91 | + private: |
| 92 | + std::vector<char> payload_; /**< Internal vector data structure to save the payload*/ |
| 93 | + std::size_t length_; /**< Current length of the payload */ |
| 94 | + uint8_t seqNo_; /**< Sequence number */ |
| 95 | + |
| 96 | +}; |
0 commit comments