protocol_new.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2015 KONDO Takatoshi
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. #include <msgpack.hpp>
  10. #include <string>
  11. #include <iostream>
  12. #include <sstream>
  13. // This example uses obsolete APIs
  14. // See protocol_new.cpp
  15. namespace myprotocol {
  16. struct Get {
  17. Get() {}
  18. Get(uint32_t f, const std::string& k) : flags(f), key(k) {}
  19. uint32_t flags;
  20. std::string key;
  21. MSGPACK_DEFINE(flags, key);
  22. };
  23. typedef std::vector<Get> MultiGet;
  24. }
  25. int main(void)
  26. {
  27. // send Get request
  28. std::stringstream stream;
  29. {
  30. myprotocol::Get req;
  31. req.flags = 0;
  32. req.key = "key0";
  33. msgpack::pack(stream, req);
  34. }
  35. stream.seekg(0);
  36. // receive Get request
  37. {
  38. std::string buffer(stream.str());
  39. msgpack::object_handle oh =
  40. msgpack::unpack(buffer.data(), buffer.size());
  41. msgpack::object o = oh.get();
  42. myprotocol::Get req;
  43. o.convert(req);
  44. std::cout << "received: " << o << std::endl;
  45. }
  46. stream.str("");
  47. // send MultiGet request
  48. {
  49. myprotocol::MultiGet req;
  50. req.push_back( myprotocol::Get(1, "key1") );
  51. req.push_back( myprotocol::Get(2, "key2") );
  52. req.push_back( myprotocol::Get(3, "key3") );
  53. msgpack::pack(stream, req);
  54. }
  55. stream.seekg(0);
  56. // receive MultiGet request
  57. {
  58. std::string buffer(stream.str());
  59. msgpack::object_handle oh =
  60. msgpack::unpack(buffer.data(), buffer.size());
  61. msgpack::object o = oh.get();
  62. myprotocol::MultiGet req;
  63. o.convert(req);
  64. std::cout << "received: " << o << std::endl;
  65. }
  66. }