Stream.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef STREAM_H
  2. #define STREAM_H
  3. #include <cstddef>
  4. #include <stdint.h>
  5. class Stream
  6. {
  7. public:
  8. Stream(size_t minSize = 1);
  9. virtual ~Stream();
  10. Stream(const Stream& other) = delete;
  11. Stream& operator=(const Stream& other) = delete;
  12. void readSocket(int socket);
  13. void sendToSocket(int socket);
  14. bool hasData() const;
  15. void write(const void* writeData, size_t length);
  16. void write(const char* writeData);
  17. void writeChar(char c);
  18. void writeShort(int16_t s);
  19. void writeInt(int32_t i);
  20. void writeLong(int64_t l);
  21. void writeUnsignedChar(uint8_t uc);
  22. void writeUnsignedShort(uint16_t us);
  23. void writeUnsignedInt(uint32_t ui);
  24. void writeUnsignedLong(uint64_t ul);
  25. bool read(void* buffer, size_t length);
  26. char readChar(char error);
  27. int16_t readShort(int16_t error);
  28. int32_t readInt(int32_t error);
  29. int64_t readLong(int64_t error);
  30. unsigned char readUnsignedChar(unsigned char error);
  31. uint16_t readUnsignedShort(uint16_t error);
  32. uint32_t readUnsignedInt(uint32_t error);
  33. uint64_t readUnsignedLong(uint64_t error);
  34. private:
  35. bool resize(size_t minSize);
  36. size_t getSize(size_t minSize) const;
  37. // limit to 50 mbyte
  38. static constexpr size_t MAX_SIZE = 1024 * 1024 * 50;
  39. size_t dataLength;
  40. unsigned char* data;
  41. size_t writeIndex = 0;
  42. size_t readIndex = 0;
  43. };
  44. #endif