content_stream.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019 Google LLC
  2. //
  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. // https://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. #ifndef dap_content_stream_h
  15. #define dap_content_stream_h
  16. #include <deque>
  17. #include <memory>
  18. #include <string>
  19. #include <stdint.h>
  20. #include "dap/session.h"
  21. namespace dap {
  22. // Forward declarations
  23. class Reader;
  24. class Writer;
  25. class ContentReader {
  26. public:
  27. ContentReader() = default;
  28. ContentReader(const std::shared_ptr<Reader>&,
  29. const OnInvalidData on_invalid_data = kIgnore);
  30. ContentReader& operator=(ContentReader&&) noexcept;
  31. bool isOpen();
  32. void close();
  33. std::string read();
  34. private:
  35. bool scan(const uint8_t* seq, size_t len);
  36. bool scan(const char* str);
  37. bool match(const uint8_t* seq, size_t len);
  38. bool match(const char* str);
  39. char matchAny(const char* chars);
  40. bool buffer(size_t bytes);
  41. std::string badHeader();
  42. std::shared_ptr<Reader> reader;
  43. std::deque<uint8_t> buf;
  44. OnInvalidData on_invalid_data;
  45. };
  46. class ContentWriter {
  47. public:
  48. ContentWriter() = default;
  49. ContentWriter(const std::shared_ptr<Writer>&);
  50. ContentWriter& operator=(ContentWriter&&) noexcept;
  51. bool isOpen();
  52. void close();
  53. bool write(const std::string&) const;
  54. private:
  55. std::shared_ptr<Writer> writer;
  56. };
  57. } // namespace dap
  58. #endif // dap_content_stream_h