asio_send_recv.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // MessagePack for C++ example
  2. //
  3. // Copyright (C) 2017 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 <string>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <boost/asio.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <msgpack.hpp>
  15. int main() {
  16. boost::asio::io_service ios;
  17. std::uint16_t const port = 12345;
  18. // Server
  19. std::size_t const window_size = 10;
  20. boost::asio::ip::tcp::acceptor ac(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
  21. boost::asio::ip::tcp::socket ss(ios);
  22. std::function<void()> do_accept;
  23. std::function<void()> do_async_read_some;
  24. msgpack::unpacker unp;
  25. do_accept = [&] {
  26. ac.async_accept(
  27. ss,
  28. [&]
  29. (boost::system::error_code const& e) {
  30. if (e) {
  31. std::cout << __LINE__ << ":" << e.message() << std::endl;
  32. return;
  33. }
  34. unp.reserve_buffer(window_size);
  35. do_async_read_some = [&] {
  36. ss.async_read_some(
  37. boost::asio::buffer(unp.buffer(), window_size),
  38. [&](boost::system::error_code const& e, std::size_t bytes_transferred) {
  39. if (e) {
  40. std::cout << __LINE__ << ":" << e.message() << std::endl;
  41. return;
  42. }
  43. std::cout << bytes_transferred << " bytes read." << std::endl;
  44. unp.buffer_consumed(bytes_transferred);
  45. msgpack::object_handle oh;
  46. while (unp.next(oh)) {
  47. std::cout << oh.get() << std::endl;
  48. // In order to finish the program,
  49. // return if one complete msgpack is processed.
  50. // In actual server, don't return here.
  51. return;
  52. }
  53. do_async_read_some();
  54. }
  55. );
  56. };
  57. do_async_read_some();
  58. }
  59. );
  60. };
  61. do_accept();
  62. // Client
  63. auto host = "localhost";
  64. boost::asio::ip::tcp::resolver r(ios);
  65. #if BOOST_VERSION < 106600
  66. boost::asio::ip::tcp::resolver::query q(host, boost::lexical_cast<std::string>(port));
  67. auto it = r.resolve(q);
  68. boost::asio::ip::tcp::resolver::iterator end;
  69. #else // BOOST_VERSION < 106600
  70. auto eps = r.resolve(host, boost::lexical_cast<std::string>(port));
  71. auto it = eps.begin();
  72. auto end = eps.end();
  73. #endif // BOOST_VERSION < 106600
  74. boost::asio::ip::tcp::socket cs(ios);
  75. boost::asio::async_connect(
  76. cs,
  77. it,
  78. end,
  79. [&]
  80. (boost::system::error_code const& e, boost::asio::ip::tcp::resolver::iterator) {
  81. if (e) {
  82. std::cout << __LINE__ << ":" << e.message() << std::endl;
  83. return;
  84. }
  85. std::cout << __LINE__ << ":client connected" << std::endl;
  86. msgpack::sbuffer sb;
  87. msgpack::pack(sb, std::make_tuple(42, false, "hello world", 12.3456));
  88. write(cs, boost::asio::buffer(sb.data(), sb.size()));
  89. }
  90. );
  91. // Start
  92. ios.run();
  93. }