socket_stream_example.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <iostream>
  2. #include <sstream>
  3. #include <msgpack.hpp>
  4. struct json_like_visitor : msgpack::v2::null_visitor {
  5. json_like_visitor(std::string& s):m_s(s), m_ref(false) {} // m_ref is false by default
  6. bool visit_nil() {
  7. m_s += "null";
  8. return true;
  9. }
  10. bool visit_boolean(bool v) {
  11. if (v) m_s += "true";
  12. else m_s += "false";
  13. return true;
  14. }
  15. bool visit_positive_integer(uint64_t v) {
  16. std::stringstream ss;
  17. ss << v;
  18. m_s += ss.str();
  19. return true;
  20. }
  21. bool visit_negative_integer(int64_t v) {
  22. std::stringstream ss;
  23. ss << v;
  24. m_s += ss.str();
  25. return true;
  26. }
  27. bool visit_str(const char* v, uint32_t size) {
  28. // I omit escape process.
  29. m_s += '"' + std::string(v, size) + '"';
  30. return true;
  31. }
  32. bool start_array(uint32_t /*num_elements*/) {
  33. m_s += "[";
  34. return true;
  35. }
  36. bool end_array_item() {
  37. m_s += ",";
  38. return true;
  39. }
  40. bool end_array() {
  41. m_s.erase(m_s.size() - 1, 1); // remove the last ','
  42. m_s += "]";
  43. return true;
  44. }
  45. bool start_map(uint32_t /*num_kv_pairs*/) {
  46. m_s += "{";
  47. return true;
  48. }
  49. bool end_map_key() {
  50. m_s += ":";
  51. return true;
  52. }
  53. bool end_map_value() {
  54. m_s += ",";
  55. return true;
  56. }
  57. bool end_map() {
  58. m_s.erase(m_s.size() - 1, 1); // remove the last ','
  59. m_s += "}";
  60. return true;
  61. }
  62. void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
  63. std::cerr << "parse error"<<std::endl;
  64. }
  65. void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
  66. std::cout << "insufficient bytes"<<std::endl;
  67. }
  68. std::string& m_s;
  69. // These two functions are required by parser.
  70. void set_referenced(bool ref) { m_ref = ref; }
  71. bool referenced() const { return m_ref; }
  72. bool m_ref;
  73. };
  74. struct do_nothing {
  75. void operator()(char* /*buffer*/) {
  76. }
  77. };
  78. class json_like_printer : public msgpack::parser<json_like_printer, do_nothing>,
  79. public json_like_visitor {
  80. typedef parser<json_like_printer, do_nothing> parser_t;
  81. public:
  82. json_like_printer(std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE)
  83. :parser_t(do_nothing_, initial_buffer_size),
  84. json_like_visitor(json_str_) {
  85. }
  86. json_like_visitor& visitor() { return *this; }
  87. void print() { std::cout << json_str_ << std::endl; json_str_.clear();}
  88. private:
  89. do_nothing do_nothing_;
  90. std::string json_str_;
  91. };
  92. template <typename T>
  93. struct ref_buffer {
  94. ref_buffer(T& t):t(t) {}
  95. void write(char const* ptr, std::size_t len) {
  96. if (len > t.buffer_capacity()) {
  97. t.reserve_buffer(len - t.buffer_capacity());
  98. }
  99. std::memcpy(t.buffer(), ptr, len);
  100. t.buffer_consumed(len);
  101. }
  102. T& t;
  103. };
  104. #define BUFFERING_SIZE_MAX 100
  105. //simulates streamed content (a socket for example)
  106. bool produce( std::stringstream & ss, char* buff, std::size_t& size)
  107. {
  108. ss.read(buff, BUFFERING_SIZE_MAX);
  109. size = static_cast<std::size_t>(ss.gcount());
  110. return (size > 0);
  111. }
  112. //shows how you can treat data
  113. void consume( const char* buff, const std::size_t size,
  114. ref_buffer<json_like_printer> & rb,
  115. json_like_printer & jp
  116. )
  117. {
  118. rb.write(buff,size);
  119. while( jp.next() )
  120. {
  121. //here we print the data, you could do any wanted processing
  122. jp.print();
  123. }
  124. }
  125. int main() {
  126. std::vector<std::vector<int>> vvi1 { { 1,2,3,4,5}, { 6,7,8,9,10} };
  127. std::vector<std::vector<int>> vvi2 { { 11,12,13,14,15}, { 16,17,18,19,20} };
  128. std::stringstream ss;
  129. msgpack::pack(ss, vvi1);
  130. msgpack::pack(ss, vvi2);
  131. char buffer[BUFFERING_SIZE_MAX];
  132. std::size_t size = 0;
  133. json_like_printer jp(1); // set initial buffer size explicitly
  134. ref_buffer<json_like_printer> rb(jp);
  135. while( produce(ss,buffer,size) )
  136. {
  137. consume(buffer, size, rb, jp);
  138. }
  139. }