remove C++ part files

remove the following files or folders:
erb
example/boost
example/cpp03
example/cpp11
example/x3
example/CMakeLists.txt
fuzz
include/msgpack/adaptor
include/msgpack/predef.h
include/msgpack/predef
include/msgpack/preprocessor
include/msgpack/v1
include/msgpack/v2
include/msgpack/v3
include/msgpack/*.hpp
include/msgpack.hpp
.gitmodules
external
make_file_list.sh
msgpack_vc8.sln
msgpack_vc8.vcproj
preprocess
QUICKSTART-CPP.md
test/*.cpp exclude test/*_c.cpp
.github/depends/boost.sh
ci/build_regression.sh
This commit is contained in:
yuangongji
2020-06-05 16:09:17 +08:00
parent 6e7deb8091
commit ee546036a1
820 changed files with 0 additions and 113794 deletions

View File

@@ -1,9 +0,0 @@
IF (NOT MSGPACK_CXX_ONLY)
ADD_SUBDIRECTORY (c)
ENDIF ()
IF (MSGPACK_ENABLE_CXX)
ADD_SUBDIRECTORY (cpp03)
ADD_SUBDIRECTORY (cpp11)
ADD_SUBDIRECTORY (boost)
ADD_SUBDIRECTORY (x3)
ENDIF ()

View File

@@ -1,56 +0,0 @@
IF (MSGPACK_BOOST)
LIST (APPEND exec_PROGRAMS
msgpack_variant_capitalize.cpp
msgpack_variant_mapbased.cpp
)
IF (MSGPACK_CXX11 OR MSGPACK_CXX17)
FIND_PACKAGE (Threads REQUIRED)
LIST (APPEND exec_PROGRAMS
asio_send_recv.cpp
)
IF (ZLIB_FOUND)
INCLUDE_DIRECTORIES (
${ZLIB_INCLUDE_DIRS}
)
LIST (APPEND exec_PROGRAMS
asio_send_recv_zlib.cpp
)
ENDIF ()
ENDIF ()
ENDIF ()
FOREACH (source_file ${exec_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
TARGET_LINK_LIBRARIES (${source_file_we}
${Boost_SYSTEM_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
)
IF (ZLIB_FOUND)
TARGET_LINK_LIBRARIES (${source_file_we}
${ZLIB_LIBRARIES}
)
ENDIF()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()

View File

@@ -1,104 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <sstream>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <msgpack.hpp>
int main() {
boost::asio::io_service ios;
std::uint16_t const port = 12345;
// Server
std::size_t const window_size = 10;
boost::asio::ip::tcp::acceptor ac(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
boost::asio::ip::tcp::socket ss(ios);
std::function<void()> do_accept;
std::function<void()> do_async_read_some;
msgpack::unpacker unp;
do_accept = [&] {
ac.async_accept(
ss,
[&]
(boost::system::error_code const& e) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
do_async_read_some = [&] {
unp.reserve_buffer(window_size);
ss.async_read_some(
boost::asio::buffer(unp.buffer(), window_size),
[&](boost::system::error_code const& e, std::size_t bytes_transferred) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
std::cout << bytes_transferred << " bytes read." << std::endl;
unp.buffer_consumed(bytes_transferred);
msgpack::object_handle oh;
while (unp.next(oh)) {
std::cout << oh.get() << std::endl;
// In order to finish the program,
// return if one complete msgpack is processed.
// In actual server, don't return here.
return;
}
do_async_read_some();
}
);
};
do_async_read_some();
}
);
};
do_accept();
// Client
auto host = "localhost";
boost::asio::ip::tcp::resolver r(ios);
#if BOOST_VERSION < 106600
boost::asio::ip::tcp::resolver::query q(host, boost::lexical_cast<std::string>(port));
auto it = r.resolve(q);
boost::asio::ip::tcp::resolver::iterator end;
#else // BOOST_VERSION < 106600
auto eps = r.resolve(host, boost::lexical_cast<std::string>(port));
auto it = eps.begin();
auto end = eps.end();
#endif // BOOST_VERSION < 106600
boost::asio::ip::tcp::socket cs(ios);
boost::asio::async_connect(
cs,
it,
end,
[&]
(boost::system::error_code const& e, boost::asio::ip::tcp::resolver::iterator) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
std::cout << __LINE__ << ":client connected" << std::endl;
msgpack::sbuffer sb;
msgpack::pack(sb, std::make_tuple(42, false, "hello world", 12.3456));
write(cs, boost::asio::buffer(sb.data(), sb.size()));
}
);
// Start
ios.run();
}

View File

@@ -1,176 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <sstream>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <msgpack.hpp>
#include <msgpack/zbuffer.hpp>
#include <zlib.h>
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
boost::asio::io_service ios;
std::uint16_t const port = 12345;
int num_of_zlib_data = 2;
int idx_zlib_data = 0;
// Server
std::size_t const window_size = 11;
boost::asio::ip::tcp::acceptor ac(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
boost::asio::ip::tcp::socket ss(ios);
std::function<void()> do_accept;
std::function<void()> do_async_read_some;
// zlib for decompress
z_stream strm;
auto zlib_init = [&] {
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = Z_NULL;
{
int zret = inflateInit(&strm);
if (zret != Z_OK) {
std::cout << "Zlib inflateInit() error = " << zret << std::endl;
}
}
};
zlib_init();
std::vector<char> buf(4); // buf size
msgpack::unpacker unp;
do_accept = [&] {
ac.async_accept(
ss,
[&]
(boost::system::error_code const& e) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
do_async_read_some = [&] {
ss.async_read_some(
boost::asio::buffer(buf),
[&](boost::system::error_code const& e, std::size_t bytes_transferred) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
std::cout << bytes_transferred << " bytes read." << std::endl;
print(std::string(std::string(&buf[0], buf.size())));
strm.avail_in = bytes_transferred;
do {
strm.next_in = reinterpret_cast<unsigned char*>(&buf[0]) + (bytes_transferred - strm.avail_in);
int zret;
unp.reserve_buffer(window_size);
strm.avail_out = window_size;
strm.next_out = reinterpret_cast<unsigned char*>(unp.buffer());
do {
zret = inflate(&strm, Z_NO_FLUSH);
assert(zret != Z_STREAM_ERROR);
switch (zret) {
case Z_NEED_DICT:
zret = Z_DATA_ERROR;
// fall through
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd(&strm);
std::cout << "Zlib inflate() error = " << zret << std::endl;
std::exit(-1);
}
std::size_t decompressed_size = window_size - strm.avail_out;
std::cout << decompressed_size << " bytes decompressed." << std::endl;
unp.buffer_consumed(decompressed_size);
msgpack::object_handle oh;
while (unp.next(oh)) {
std::cout << oh.get() << std::endl;
}
} while (strm.avail_out == 0);
if (zret == Z_STREAM_END) {
inflateEnd(&strm);
std::cout << "Zlib decompress finished." << std::endl;
++idx_zlib_data;
if (idx_zlib_data == num_of_zlib_data) {
std::cout << "All zlib decompress finished." << std::endl;
return;
}
zlib_init();
}
} while (strm.avail_in != 0);
do_async_read_some();
}
);
};
do_async_read_some();
}
);
};
do_accept();
// Client
auto host = "localhost";
boost::asio::ip::tcp::resolver r(ios);
#if BOOST_VERSION < 106600
boost::asio::ip::tcp::resolver::query q(host, boost::lexical_cast<std::string>(port));
auto it = r.resolve(q);
boost::asio::ip::tcp::resolver::iterator end;
#else // BOOST_VERSION < 106600
auto eps = r.resolve(host, boost::lexical_cast<std::string>(port));
auto it = eps.begin();
auto end = eps.end();
#endif // BOOST_VERSION < 106600
boost::asio::ip::tcp::socket cs(ios);
boost::asio::async_connect(
cs,
it,
end,
[&]
(boost::system::error_code const& e, boost::asio::ip::tcp::resolver::iterator) {
if (e) {
std::cout << __LINE__ << ":" << e.message() << std::endl;
return;
}
std::cout << __LINE__ << ":client connected" << std::endl;
for (int i = 0; i != num_of_zlib_data; ++i) {
msgpack::zbuffer zb;
msgpack::pack(zb, std::make_tuple(i, false, "hello world", 12.3456));
zb.flush(); // finalize zbuffer (don't forget it)
print(std::string(zb.data(), zb.size()));
write(cs, boost::asio::buffer(zb.data(), zb.size()));
}
}
);
// Start
ios.run();
}

View File

@@ -1,94 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <msgpack.hpp>
struct user {
std::string name;
int age;
std::string address;
MSGPACK_DEFINE(name, age, address);
};
struct proc:boost::static_visitor<void> {
void operator()(std::string& v) const {
std::cout << " match std::string& v" << std::endl;
std::cout << " v: " << v << std::endl;
std::cout << " capitalize" << std::endl;
for (std::string::iterator it = v.begin(), end = v.end();
it != end;
++it) {
*it = std::toupper(*it);
}
}
void operator()(std::vector<msgpack::type::variant>& v) const {
std::cout << "match vector (msgpack::type::ARRAY)" << std::endl;
std::vector<msgpack::type::variant>::iterator it = v.begin();
std::vector<msgpack::type::variant>::const_iterator end = v.end();
for (; it != end; ++it) {
boost::apply_visitor(*this, *it);
}
}
template <typename T>
void operator()(T const&) const {
std::cout << " match others" << std::endl;
}
};
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
std::stringstream ss1;
user u;
u.name = "Takatoshi Kondo";
u.age = 42;
u.address = "Tokyo, JAPAN";
std::cout << "Packing object." << std::endl;
msgpack::pack(ss1, u);
print(ss1.str());
msgpack::object_handle oh1 = msgpack::unpack(ss1.str().data(), ss1.str().size());
msgpack::object const& obj1 = oh1.get();
std::cout << "Unpacked msgpack object." << std::endl;
std::cout << obj1 << std::endl;
msgpack::type::variant v = obj1.as<msgpack::type::variant>();
std::cout << "Applying proc..." << std::endl;
boost::apply_visitor(proc(), v);
std::stringstream ss2;
std::cout << "Packing modified object." << std::endl;
msgpack::pack(ss2, v);
print(ss2.str());
msgpack::object_handle oh2 = msgpack::unpack(ss2.str().data(), ss2.str().size());
msgpack::object const& obj2 = oh2.get();
std::cout << "Modified msgpack object." << std::endl;
std::cout << obj2 << std::endl;
}

View File

@@ -1,97 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <sstream>
#include <iostream>
#include <msgpack.hpp>
struct user {
std::string name;
int age;
std::string address;
MSGPACK_DEFINE_MAP(name, age, address);
};
struct proc:boost::static_visitor<void> {
// msgpack::type::MAP is converted to std::multimap, not std::map.
void operator()(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>& v) const {
std::cout << "match map" << std::endl;
std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator it = v.begin();
std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::iterator end = v.end();
while(it != end) {
boost::string_ref const& key = it->first.as_boost_string_ref();
if (key == "name") {
boost::string_ref const& value = it->second.as_boost_string_ref();
if (value == "Takatoshi Kondo") {
// You can add values to msgpack::type::variant_ref.
v.insert(
std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::value_type(
"role",
"msgpack-c committer"
)
);
}
++it;
}
else if (key == "age") {
// You can remove key-value pair from msgpack::type::variant_ref
#if defined(MSGPACK_USE_CPP03)
# if MSGPACK_LIB_STD_CXX
v.erase(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::const_iterator(it++));
# else // MSGPACK_LIB_STD_CXX
v.erase(it++);
# endif // MSGPACK_LIB_STD_CXX
#else // defined(MSGPACK_USE_CPP03)
# if MSGPACK_LIB_STD_CXX
it = v.erase(std::multimap<msgpack::type::variant_ref, msgpack::type::variant_ref>::const_iterator(it));
# else // MSGPACK_LIB_STD_CXX
it = v.erase(it);
# endif // MSGPACK_LIB_STD_CXX
#endif // defined(MSGPACK_USE_CPP03)
}
else if (key == "address") {
// When you want to append string
// "Tokyo" -> "Tokyo, JAPAN"
// Use msgpack::type::variant instead of msgpack::type::variant_ref
// or do as follows:
boost::string_ref const& value = it->second.as_boost_string_ref();
it->second = std::string(&value.front(), value.size()) + ", JAPAN";
++it;
}
}
}
template <typename T>
void operator()(T const&) const {
std::cout << " match others" << std::endl;
}
};
int main() {
std::stringstream ss;
user u;
u.name = "Takatoshi Kondo";
u.age = 42;
u.address = "Tokyo";
msgpack::pack(ss, u);
std::string const& str = ss.str();
msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());
msgpack::object const& obj = oh.get();
std::cout << "Unpacked msgpack object." << std::endl;
std::cout << obj << std::endl;
msgpack::type::variant_ref v = obj.as<msgpack::type::variant_ref>();
std::cout << "Applying proc..." << std::endl;
boost::apply_visitor(proc(), v);
msgpack::zone z;
std::cout << "Applied msgpack object." << std::endl;
std::cout << msgpack::object(v, z) << std::endl;
}

View File

@@ -1,120 +0,0 @@
LIST (APPEND exec_PROGRAMS
class_intrusive.cpp
class_intrusive_map.cpp
class_non_intrusive.cpp
custom.cpp
enum.cpp
map_based_versionup.cpp
protocol_new.cpp
reuse_zone.cpp
simple.cpp
)
IF (MSGPACK_DEFAULT_API_VERSION EQUAL 1)
LIST (APPEND exec_PROGRAMS
protocol.cpp
)
ENDIF ()
IF (NOT MSVC)
LIST (APPEND with_pthread_PROGRAMS
stream.cpp
)
ENDIF ()
IF (MSGPACK_BOOST)
LIST (APPEND with_boost_lib_PROGRAMS
speed_test.cpp
speed_test_nested_array.cpp
)
ENDIF ()
FOREACH (source_file ${exec_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
FOREACH (source_file ${with_pthread_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
TARGET_LINK_LIBRARIES (${source_file_we}
${CMAKE_THREAD_LIBS_INIT}
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
FOREACH (source_file ${with_boost_lib_PROGRAMS})
INCLUDE_DIRECTORIES (
../include
${Boost_INCLUDE_DIRS}
)
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
TARGET_LINK_LIBRARIES (${source_file_we}
${Boost_TIMER_LIBRARY}
${Boost_CHRONO_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
IF (NOT MSVC AND NOT APPLE)
TARGET_LINK_LIBRARIES (${source_file_we}
rt
)
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -O3")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()

View File

@@ -1,104 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
// When you want to adapt map instead of array, you can enable these macro definition.
//
// #define MSGPACK_USE_DEFINE_MAP
#include <msgpack.hpp>
struct my_base1 {
int a;
MSGPACK_DEFINE(a);
};
inline bool operator==(my_base1 const& lhs, my_base1 const& rhs) {
return lhs.a == rhs.a;
}
struct my_base2 {
std::string b;
std::string c;
MSGPACK_DEFINE(b, c);
};
inline bool operator==(my_base2 const& lhs, my_base2 const& rhs) {
return lhs.b == rhs.b && lhs.c == rhs.c;
}
class my_class : public my_base1, private my_base2 {
public:
my_class() {} // When you want to convert from msgpack::object to my_class,
// my_class should be default constractible.
my_class(std::string const& name, int age):name_(name), age_(age) {}
void set_b(std::string const& str) { b = str; }
void set_c(std::string const& str) { c = str; }
friend bool operator==(my_class const& lhs, my_class const& rhs) {
return
static_cast<my_base1 const&>(lhs) == static_cast<my_base1 const&>(rhs) &&
static_cast<my_base2 const&>(lhs) == static_cast<my_base2 const&>(rhs) &&
lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
}
private:
std::string name_;
int age_;
public:
MSGPACK_DEFINE(name_, age_, MSGPACK_BASE(my_base1), MSGPACK_BASE(my_base2));
};
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
{ // pack, unpack
my_class my("John Smith", 42);
my.a = 123;
my.set_b("ABC");
my.set_c("DEF");
std::stringstream ss;
msgpack::pack(ss, my);
std::string const& str = ss.str();
print(str);
msgpack::object_handle oh =
msgpack::unpack(str.data(), str.size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
{ // create object with zone
my_class my("John Smith", 42);
my.a = 123;
my.set_b("ABC");
my.set_c("DEF");
msgpack::zone z;
msgpack::object obj(my, z);
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
}

View File

@@ -1,76 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <msgpack.hpp>
class my_class {
public:
my_class() {} // When you want to convert from msgpack::object to my_class,
// my_class should be default constractible.
// If you use C++11, you can adapt non-default constructible
// classes to msgpack::object.
// See https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_adaptor#non-default-constructible-class-support-c11-only
my_class(std::string const& name, int age):name_(name), age_(age) {}
friend bool operator==(my_class const& lhs, my_class const& rhs) {
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
}
private:
std::string name_;
int age_;
public:
MSGPACK_DEFINE_MAP(name_, age_);
};
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
{ // pack, unpack
my_class my("John Smith", 42);
std::stringstream ss;
msgpack::pack(ss, my);
print(ss.str());
std::string const& str = ss.str();
msgpack::object_handle oh =
msgpack::unpack(str.data(), str.size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
{ // create object with zone
my_class my("John Smith", 42);
msgpack::zone z;
msgpack::object obj(my, z);
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
}

View File

@@ -1,119 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <msgpack.hpp>
class my_class {
public:
my_class() {} // When you want to convert from msgpack::object to my_class,
// my_class should be default constractible.
my_class(std::string const& name, int age):name_(name), age_(age) {}
// my_class should provide getters for the data members you want to pack.
std::string const& get_name() const { return name_; }
int get_age() const { return age_; }
friend bool operator==(my_class const& lhs, my_class const& rhs) {
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
}
private:
std::string name_;
int age_;
};
// User defined class template specialization
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template<>
struct convert<my_class> {
msgpack::object const& operator()(msgpack::object const& o, my_class& v) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 2) throw msgpack::type_error();
v = my_class(
o.via.array.ptr[0].as<std::string>(),
o.via.array.ptr[1].as<int>());
return o;
}
};
template<>
struct pack<my_class> {
template <typename Stream>
packer<Stream>& operator()(msgpack::packer<Stream>& o, my_class const& v) const {
// packing member variables as an array.
o.pack_array(2);
o.pack(v.get_name());
o.pack(v.get_age());
return o;
}
};
template <>
struct object_with_zone<my_class> {
void operator()(msgpack::object::with_zone& o, my_class const& v) const {
o.type = type::ARRAY;
o.via.array.size = 2;
o.via.array.ptr = static_cast<msgpack::object*>(
o.zone.allocate_align(sizeof(msgpack::object) * o.via.array.size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
o.via.array.ptr[0] = msgpack::object(v.get_name(), o.zone);
o.via.array.ptr[1] = msgpack::object(v.get_age(), o.zone);
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
{ // pack, unpack
my_class my("John Smith", 42);
std::stringstream ss;
msgpack::pack(ss, my);
std::string const& str = ss.str();
print(str);
msgpack::object_handle oh =
msgpack::unpack(str.data(), str.size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
{ // create object with zone
my_class my("John Smith", 42);
msgpack::zone z;
msgpack::object obj(my, z);
std::cout << obj << std::endl;
assert(obj.as<my_class>() == my);
}
}

View File

@@ -1,67 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <msgpack.hpp>
#include <sstream>
#include <string>
#include <iostream>
class old_class {
public:
old_class() : value("default") { }
std::string value;
MSGPACK_DEFINE(value);
};
class new_class {
public:
new_class() : value("default"), flag(-1) { }
std::string value;
int flag;
MSGPACK_DEFINE(value, flag);
};
int main(void)
{
{
old_class oc;
new_class nc;
std::stringstream sbuf;
msgpack::pack(sbuf, oc);
msgpack::object_handle oh =
msgpack::unpack(sbuf.str().data(), sbuf.str().size());
msgpack::object obj = oh.get();
obj.convert(nc);
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
}
{
new_class nc;
old_class oc;
std::stringstream sbuf;
msgpack::pack(sbuf, nc);
msgpack::object_handle oh =
msgpack::unpack(sbuf.str().data(), sbuf.str().size());
msgpack::object obj = oh.get();
obj.convert(oc);
std::cout << obj << " value=" << oc.value << std::endl;
}
}

View File

@@ -1,59 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <sstream>
#include <iostream>
#include <cassert>
#include <msgpack.hpp>
enum my_enum {
elem1,
elem2,
elem3
};
MSGPACK_ADD_ENUM(my_enum);
int main(void)
{
{ // pack, unpack
std::stringstream sbuf;
msgpack::pack(sbuf, elem1);
msgpack::pack(sbuf, elem2);
my_enum e3 = elem3;
msgpack::pack(sbuf, e3);
msgpack::object_handle oh;
std::size_t off = 0;
msgpack::unpack(oh, sbuf.str().data(), sbuf.str().size(), off);
std::cout << oh.get().as<my_enum>() << std::endl;
assert(oh.get().as<my_enum>() == elem1);
msgpack::unpack(oh, sbuf.str().data(), sbuf.str().size(), off);
std::cout << oh.get().as<my_enum>() << std::endl;
assert(oh.get().as<my_enum>() == elem2);
msgpack::unpack(oh, sbuf.str().data(), sbuf.str().size(), off);
std::cout << oh.get().as<my_enum>() << std::endl;
assert(oh.get().as<my_enum>() == elem3);
}
{ // create object without zone
msgpack::object obj(elem2);
std::cout << obj.as<my_enum>() << std::endl;
assert(obj.as<my_enum>() == elem2);
}
{ // create object with zone
msgpack::zone z;
msgpack::object objz(elem3, z);
std::cout << objz.as<my_enum>() << std::endl;
assert(objz.as<my_enum>() == elem3);
}
}

View File

@@ -1,112 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
#include <msgpack.hpp>
struct base1 {
base1():a("default") {}
std::string a;
MSGPACK_DEFINE_MAP(a);
};
struct v1 : base1 {
v1():name("default"), age(0) {}
std::string name;
int age;
MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base1), name, age);
};
struct base2 {
base2():a("default") {}
std::string a;
MSGPACK_DEFINE_MAP(a);
};
// Removed: base1, name
// Added : base2, address
struct v2 : base2 {
v2(): age(0), address("default") {}
int age;
std::string address;
MSGPACK_DEFINE_MAP(MSGPACK_BASE_MAP(base2), age, address);
};
// The member variable "age" is in common between v1 and v2.
void print(std::string const& buf) {
for (std::string::const_iterator it = buf.begin(), end = buf.end();
it != end;
++it) {
std::cout
<< std::setw(2)
<< std::hex
<< std::setfill('0')
<< (static_cast<int>(*it) & 0xff)
<< ' ';
}
std::cout << std::dec << std::endl;
}
int main() {
{ // pack v1, unpack, convert to v2
v1 v;
v.a = "ABC";
v.name = "John Smith";
v.age = 35;
std::stringstream ss;
msgpack::pack(ss, v);
print(ss.str());
std::string const& str = ss.str();
msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
v2 newv = obj.as<v2>();
std::cout << "v2::a " << newv.a << std::endl;
std::cout << "v2::age " << newv.age << std::endl;
std::cout << "v2::address " << newv.address << std::endl;
// "age" is set from v1
assert(newv.a == "default");
assert(newv.age == 35);
assert(newv.address == "default");
}
{ // create v2 object with zone, convert to v1
v2 v;
v.a = "DEF";
v.age = 42;
v.address = "Tokyo";
msgpack::zone z;
msgpack::object obj(v, z);
std::cout << obj << std::endl;
v1 newv = obj.as<v1>();
std::cout << "v1::a " << newv.a << std::endl;
std::cout << "v1::name " << newv.name << std::endl;
std::cout << "v1::age " << newv.age << std::endl;
// "age" is set from v2
assert(newv.a == "default");
assert(newv.name == "default");
assert(newv.age == 42);
}
}

View File

@@ -1,97 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
// This example uses obsolete APIs
// See protocol_new.cpp
namespace myprotocol {
using namespace msgpack::type;
using msgpack::define;
struct Get : define< tuple<uint32_t, std::string> > {
Get() { }
Get(uint32_t f, const std::string& k) :
define_type(msgpack_type(f, k)) { }
uint32_t& flags() { return msgpack::type::get<0>(*this); }
std::string& key() { return msgpack::type::get<1>(*this); }
};
struct Put : define< tuple<uint32_t, std::string, raw_ref> > {
Put() { }
Put(uint32_t f, const std::string& k, const char* valref, uint32_t vallen) :
define_type(msgpack_type( f, k, raw_ref(valref,vallen) )) { }
uint32_t& flags() { return msgpack::type::get<0>(*this); }
std::string& key() { return msgpack::type::get<1>(*this); }
raw_ref& value() { return msgpack::type::get<2>(*this); }
};
struct MultiGet : define< std::vector<Get> > {
};
}
int main(void)
{
// send Get request
std::stringstream stream;
{
myprotocol::Get req;
req.flags() = 0;
req.key() = "key0";
msgpack::pack(stream, req);
}
stream.seekg(0);
// receive Get request
{
std::string buffer(stream.str());
msgpack::object_handle oh =
msgpack::unpack(buffer.data(), buffer.size());
msgpack::object o = oh.get();
myprotocol::Get req;
o.convert(req);
std::cout << "received: " << o << std::endl;
}
stream.str("");
// send MultiGet request
{
myprotocol::MultiGet req;
req.push_back( myprotocol::Get(1, "key1") );
req.push_back( myprotocol::Get(2, "key2") );
req.push_back( myprotocol::Get(3, "key3") );
msgpack::pack(stream, req);
}
stream.seekg(0);
// receive MultiGet request
{
std::string buffer(stream.str());
msgpack::object_handle oh =
msgpack::unpack(buffer.data(), buffer.size());
msgpack::object o = oh.get();
myprotocol::MultiGet req;
o.convert(req);
std::cout << "received: " << o << std::endl;
}
}

View File

@@ -1,84 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
// This example uses obsolete APIs
// See protocol_new.cpp
namespace myprotocol {
struct Get {
Get() {}
Get(uint32_t f, const std::string& k) : flags(f), key(k) {}
uint32_t flags;
std::string key;
MSGPACK_DEFINE(flags, key);
};
typedef std::vector<Get> MultiGet;
}
int main(void)
{
// send Get request
std::stringstream stream;
{
myprotocol::Get req;
req.flags = 0;
req.key = "key0";
msgpack::pack(stream, req);
}
stream.seekg(0);
// receive Get request
{
std::string buffer(stream.str());
msgpack::object_handle oh =
msgpack::unpack(buffer.data(), buffer.size());
msgpack::object o = oh.get();
myprotocol::Get req;
o.convert(req);
std::cout << "received: " << o << std::endl;
}
stream.str("");
// send MultiGet request
{
myprotocol::MultiGet req;
req.push_back( myprotocol::Get(1, "key1") );
req.push_back( myprotocol::Get(2, "key2") );
req.push_back( myprotocol::Get(3, "key3") );
msgpack::pack(stream, req);
}
stream.seekg(0);
// receive MultiGet request
{
std::string buffer(stream.str());
msgpack::object_handle oh =
msgpack::unpack(buffer.data(), buffer.size());
msgpack::object o = oh.get();
myprotocol::MultiGet req;
o.convert(req);
std::cout << "received: " << o << std::endl;
}
}

View File

@@ -1,43 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <sstream>
#include <cassert>
#include <string>
#include <vector>
#include <msgpack.hpp>
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(42);
std::string s("ABC");
std::stringstream ss;
msgpack::pack(ss, v);
msgpack::pack(ss, s);
msgpack::zone z;
std::size_t offset = 0;
// msgpack array is constructed on z.
std::string const& ps = ss.str();
msgpack::object obj = msgpack::unpack(z, ps.data(), ps.size(), offset);
std::cout << obj << std::endl;
assert(obj.as<std::vector<int> >() == v);
// msgpack str is constructed on z.
std::string const& str = msgpack::unpack(z, ps.data(), ps.size(), offset).as<std::string>();
std::cout << str << std::endl;
assert(str == s);
}

View File

@@ -1,44 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
int main(void)
{
msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
// serialize the object into the buffer.
// any classes that implements write(const char*,size_t) can be a buffer.
std::stringstream buffer;
msgpack::pack(buffer, src);
// send the buffer ...
buffer.seekg(0);
// deserialize the buffer into msgpack::object instance.
std::string str(buffer.str());
msgpack::object_handle oh = msgpack::unpack(str.data(), str.size());
// deserialized object is valid during the msgpack::object_handle instance alive.
msgpack::object deserialized = oh.get();
// msgpack::object supports ostream.
std::cout << deserialized << std::endl;
// convert msgpack::object instance into the original type.
// if the type is mismatched, it throws msgpack::type_error exception.
msgpack::type::tuple<int, bool, std::string> dst;
deserialized.convert(dst);
return 0;
}

View File

@@ -1,63 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2013-2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
// export LD_LIBRARY_PATH=path_to_boost_lib
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <boost/timer/timer.hpp>
void test_map_pack_unpack() {
std::cout << "[TEST][map_pack_unpack]" << std::endl;
// setup
std::cout << "Setting up map data..." << std::endl;
std::map<int, int> m1;
int const num = 30000000L;
for (int i = 0; i < num; ++i) m1[i] = i;
std::cout << "Start packing..." << std::endl;
std::stringstream buffer;
{
boost::timer::cpu_timer timer;
msgpack::pack(buffer, m1);
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Pack finished..." << std::endl;
buffer.seekg(0);
std::string str(buffer.str());
msgpack::object_handle oh;
std::cout << "Start unpacking...by void unpack(object_handle& oh, const char* data, size_t len)" << std::endl;
{
boost::timer::cpu_timer timer;
msgpack::unpack(oh, str.data(), str.size());
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Unpack finished..." << std::endl;
std::map<int, int> m2;
std::cout << "Start converting..." << std::endl;
{
boost::timer::cpu_timer timer;
oh.get().convert(m2);
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Convert finished..." << std::endl;
}
int main(void)
{
test_map_pack_unpack();
}

View File

@@ -1,86 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2013-2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// g++ -std=c++11 -O3 -g -Ipath_to_msgpack_src -Ipath_to_boost speed_test.cc -Lpath_to_boost_lib -lboost_timer -lboost_system
// export LD_LIBRARY_PATH=path_to_boost_lib
#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/timer/timer.hpp>
template <typename T, std::size_t level>
struct vecvec {
typedef std::vector<typename vecvec<T, level - 1>::type> type;
static void fill(type& v, std::size_t num_of_elems, T const& val) {
for (std::size_t elem = 0; elem < num_of_elems; ++elem) {
typename vecvec<T, level - 1>::type child;
vecvec<T, level - 1>::fill(child, num_of_elems, val);
v.push_back(child);
}
}
};
template <typename T>
struct vecvec<T, 0> {
typedef std::vector<T> type;
static void fill(type& v, std::size_t num_of_elems, T const& val) {
for (std::size_t elem = 0; elem < num_of_elems; ++elem) {
v.push_back(val);
}
}
};
void test_array_of_array() {
std::cout << "[TEST][array_of_array]" << std::endl;
// setup
int const depth = 4;
std::cout << "Setting up array data..." << std::endl;
vecvec<int, depth>::type v1;
vecvec<int, depth>::fill(v1, 3, 42);
std::cout << "Start packing..." << std::endl;
std::stringstream buffer;
{
boost::timer::cpu_timer timer;
msgpack::pack(buffer, v1);
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Pack finished..." << std::endl;
buffer.seekg(0);
std::string str(buffer.str());
msgpack::object_handle oh;
std::cout << "Start unpacking...by void unpack(object_handle& oh, const char* data, size_t len)" << std::endl;
{
boost::timer::cpu_timer timer;
msgpack::unpack(oh, str.data(), str.size());
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Unpack finished..." << std::endl;
vecvec<int, depth>::type v2;
std::cout << "Start converting..." << std::endl;
{
boost::timer::cpu_timer timer;
oh.get().convert(v2);
std::string result = timer.format();
std::cout << result << std::endl;
}
std::cout << "Convert finished..." << std::endl;
}
int main(void)
{
test_array_of_array();
}

View File

@@ -1,148 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <msgpack.hpp>
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <io.h>
#include <fcntl.h>
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#endif // _MSC_VER || __MINGW32__
class Server {
public:
Server(int sock) : m_sock(sock) { }
~Server() { }
typedef msgpack::unique_ptr<msgpack::zone> unique_zone;
void socket_readable()
{
m_pac.reserve_buffer(1024);
ssize_t count =
read(m_sock, m_pac.buffer(), m_pac.buffer_capacity());
if(count <= 0) {
if(count == 0) {
throw std::runtime_error("connection closed");
}
if(errno == EAGAIN || errno == EINTR) {
return;
}
throw std::runtime_error(strerror(errno));
}
m_pac.buffer_consumed(count);
msgpack::object_handle oh;
while (m_pac.next(oh)) {
msgpack::object msg = oh.get();
unique_zone& life = oh.zone();
process_message(msg, life);
}
if(m_pac.message_size() > 10*1024*1024) {
throw std::runtime_error("message is too large");
}
}
private:
void process_message(msgpack::object msg, unique_zone&)
{
std::cout << "message reached: " << msg << std::endl;
}
private:
int m_sock;
msgpack::unpacker m_pac;
};
static void* run_server(void* arg)
{
try {
Server* srv = reinterpret_cast<Server*>(arg);
while(true) {
srv->socket_readable();
}
return NULL;
} catch (std::exception& e) {
std::cerr << "error while processing client packet: "
<< e.what() << std::endl;
return NULL;
} catch (...) {
std::cerr << "error while processing client packet: "
<< "unknown error" << std::endl;
return NULL;
}
}
struct fwriter {
fwriter(int fd) : m_fp( fdopen(fd, "w") ) { }
void write(const char* buf, size_t buflen)
{
size_t count = fwrite(buf, buflen, 1, m_fp);
if(count < 1) {
std::cout << buflen << std::endl;
std::cout << count << std::endl;
throw std::runtime_error(strerror(errno));
}
}
void flush() { fflush(m_fp); }
void close() { fclose(m_fp); }
private:
FILE* m_fp;
};
int main(void)
{
int pair[2];
if (pipe(pair) != 0) return -1;
// run server thread
Server srv(pair[0]);
pthread_t thread;
pthread_create(&thread, NULL,
run_server, reinterpret_cast<void*>(&srv));
// client thread:
fwriter writer(pair[1]);
msgpack::packer<fwriter> pk(writer);
typedef msgpack::type::tuple<std::string, std::string, std::string> put_t;
typedef msgpack::type::tuple<std::string, std::string> get_t;
put_t req1("put", "apple", "red");
put_t req2("put", "lemon", "yellow");
get_t req3("get", "apple");
pk.pack(req1);
pk.pack(req2);
pk.pack(req3);
writer.flush();
writer.close();
pthread_join(thread, NULL);
}

View File

@@ -1,43 +0,0 @@
IF (MSGPACK_CXX11 OR MSGPACK_CXX17)
INCLUDE_DIRECTORIES (
../include
)
LIST (APPEND exec_PROGRAMS
container.cpp
non_def_con_class.cpp
)
IF ("${MSGPACK_DEFAULT_API_VERSION}" GREATER "1")
LIST (APPEND exec_PROGRAMS
socket_stream_example.cpp
)
ENDIF ()
FOREACH (source_file ${exec_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
ENDIF ()

View File

@@ -1,158 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <sstream>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <forward_list>
#include <string>
#include <msgpack.hpp>
void array() {
std::array<int, 5> a { { 1, 2, 3, 4, 5 } };
std::stringstream ss;
msgpack::pack(ss, a);
msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
assert((obj.as<std::array<int, 5>>()) == a);
}
void tuple() {
std::tuple<bool, std::string, int> t(true, "ABC", 42);
std::stringstream ss;
msgpack::pack(ss, t);
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size());
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(t)>() == t);
}
void unordered_map() {
std::unordered_map<std::string, int> m { {"ABC", 1}, {"DEF", 3} };
std::stringstream ss;
msgpack::pack(ss, m);
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size());
msgpack::object obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(m)>() == m);
}
void unordered_set() {
std::unordered_set<std::string> s { "ABC", "DEF" };
std::stringstream ss;
msgpack::pack(ss, s);
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size());
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(s)>() == s);
}
void forward_list() {
using type = std::forward_list<std::string>;
type f { "ABC", "DEF" };
std::stringstream ss;
msgpack::pack(ss, f);
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size());
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<type>() == f);
}
void combi() {
std::array<int, 5> a { { 1, 2, 3, 4, 5 } };
std::tuple<bool, std::string, int> t {true, "ABC", 42};
std::unordered_map<std::string, int> m { {"ABC", 1}, {"DEF", 3} };
std::unordered_set<std::string> s { "ABC", "DEF" };
std::forward_list<std::string> f { "ABC", "DEF" };
std::stringstream ss;
msgpack::pack(ss, a);
msgpack::pack(ss, t);
msgpack::pack(ss, m);
msgpack::pack(ss, s);
msgpack::pack(ss, f);
std::size_t offset = 0;
std::cout << "offset: " << offset << std::endl;
{
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size(), offset);
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(a)>() == a);
}
std::cout << "offset: " << offset << std::endl;
{
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size(), offset);
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(t)>() == t);
}
std::cout << "offset: " << offset << std::endl;
{
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size(), offset);
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(m)>() == m);
}
std::cout << "offset: " << offset << std::endl;
{
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size(), offset);
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(s)>() == s);
}
std::cout << "offset: " << offset << std::endl;
{
auto const& str = ss.str();
auto oh = msgpack::unpack(str.data(), str.size(), offset);
auto obj = oh.get();
std::cout << obj << std::endl;
assert(obj.as<decltype(f)>() == f);
}
std::cout << "offset: " << offset << std::endl;
}
int main() {
array();
tuple();
unordered_map();
unordered_set();
forward_list();
combi();
}

View File

@@ -1,51 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <cassert>
#include <memory>
#include <iostream>
#include <msgpack.hpp>
struct my {
my() = delete;
// target class should be either copyable or movable (or both).
my(my const&) = delete;
my(my&&) = default;
my(int a):a(a) {}
int a;
MSGPACK_DEFINE(a);
};
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
template<>
struct as<my> {
my operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
if (o.via.array.size != 1) throw msgpack::type_error();
return my(o.via.array.ptr[0].as<int>());
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack
int main() {
my m1(42);
msgpack::zone z;
msgpack::object obj(m1, z);
std::cout << obj << std::endl;
assert(m1.a == obj.as<my>().a);
}

View File

@@ -1,157 +0,0 @@
#include <iostream>
#include <sstream>
#include <msgpack.hpp>
struct json_like_visitor : msgpack::v2::null_visitor {
json_like_visitor(std::string& s):m_s(s), m_ref(false) {} // m_ref is false by default
bool visit_nil() {
m_s += "null";
return true;
}
bool visit_boolean(bool v) {
if (v) m_s += "true";
else m_s += "false";
return true;
}
bool visit_positive_integer(uint64_t v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_negative_integer(int64_t v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_str(const char* v, uint32_t size) {
// I omit escape process.
m_s += '"' + std::string(v, size) + '"';
return true;
}
bool start_array(uint32_t /*num_elements*/) {
m_s += "[";
return true;
}
bool end_array_item() {
m_s += ",";
return true;
}
bool end_array() {
m_s.erase(m_s.size() - 1, 1); // remove the last ','
m_s += "]";
return true;
}
bool start_map(uint32_t /*num_kv_pairs*/) {
m_s += "{";
return true;
}
bool end_map_key() {
m_s += ":";
return true;
}
bool end_map_value() {
m_s += ",";
return true;
}
bool end_map() {
m_s.erase(m_s.size() - 1, 1); // remove the last ','
m_s += "}";
return true;
}
void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
std::cerr << "parse error"<<std::endl;
}
void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
std::cout << "insufficient bytes"<<std::endl;
}
std::string& m_s;
// These two functions are required by parser.
void set_referenced(bool ref) { m_ref = ref; }
bool referenced() const { return m_ref; }
bool m_ref;
};
struct do_nothing {
void operator()(char* /*buffer*/) {
}
};
class json_like_printer : public msgpack::parser<json_like_printer, do_nothing>,
public json_like_visitor {
typedef parser<json_like_printer, do_nothing> parser_t;
public:
json_like_printer(std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE)
:parser_t(do_nothing_, initial_buffer_size),
json_like_visitor(json_str_) {
}
json_like_visitor& visitor() { return *this; }
void print() { std::cout << json_str_ << std::endl; json_str_.clear();}
private:
do_nothing do_nothing_;
std::string json_str_;
};
template <typename T>
struct ref_buffer {
ref_buffer(T& t):t(t) {}
void write(char const* ptr, std::size_t len) {
if (len > t.buffer_capacity()) {
t.reserve_buffer(len - t.buffer_capacity());
}
std::memcpy(t.buffer(), ptr, len);
t.buffer_consumed(len);
}
T& t;
};
#define BUFFERING_SIZE_MAX 100
//simulates streamed content (a socket for example)
bool produce( std::stringstream & ss, char* buff, std::size_t& size)
{
ss.read(buff, BUFFERING_SIZE_MAX);
size = static_cast<std::size_t>(ss.gcount());
return (size > 0);
}
//shows how you can treat data
void consume( const char* buff, const std::size_t size,
ref_buffer<json_like_printer> & rb,
json_like_printer & jp
)
{
rb.write(buff,size);
while( jp.next() )
{
//here we print the data, you could do any wanted processing
jp.print();
}
}
int main() {
std::vector<std::vector<int>> vvi1 { { 1,2,3,4,5}, { 6,7,8,9,10} };
std::vector<std::vector<int>> vvi2 { { 11,12,13,14,15}, { 16,17,18,19,20} };
std::stringstream ss;
msgpack::pack(ss, vvi1);
msgpack::pack(ss, vvi2);
char buffer[BUFFERING_SIZE_MAX];
std::size_t size = 0;
json_like_printer jp(1); // set initial buffer size explicitly
ref_buffer<json_like_printer> rb(jp);
while( produce(ss,buffer,size) )
{
consume(buffer, size, rb, jp);
}
}

View File

@@ -1,72 +0,0 @@
IF (MSGPACK_USE_X3_PARSE AND MSGPACK_DEFAULT_API_VERSION VERSION_GREATER 1)
INCLUDE_DIRECTORIES (
../include
)
LIST (APPEND exec_PROGRAMS
unpack.cpp
parse.cpp
)
IF (MSGPACK_BOOST)
LIST (APPEND with_boost_PROGRAMS
stream_unpack.cpp
)
ENDIF ()
FOREACH (source_file ${exec_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
FOREACH (source_file ${with_boost_PROGRAMS})
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
ADD_EXECUTABLE (
${source_file_we}
${source_file}
)
TARGET_INCLUDE_DIRECTORIES (${source_file_we}
PRIVATE
$<TARGET_PROPERTY:msgpackc-cxx,INTERFACE_INCLUDE_DIRECTORIES>
)
TARGET_LINK_LIBRARIES (${source_file_we}
${Boost_CONTEXT_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wall -Wextra")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags")
ENDIF ()
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX")
ENDIF ()
ENDIF ()
ENDFOREACH ()
ENDIF ()

View File

@@ -1,125 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <sstream>
#include <cassert>
// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp
// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE.
#include <msgpack.hpp>
struct json_like_visitor : msgpack::v2::null_visitor {
json_like_visitor(std::string& s):m_s(s) {}
bool visit_nil() {
m_s += "null";
return true;
}
bool visit_boolean(bool v) {
if (v) m_s += "true";
else m_s += "false";
return true;
}
bool visit_positive_integer(uint64_t v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_negative_integer(int64_t v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_float32(float v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_float64(double v) {
std::stringstream ss;
ss << v;
m_s += ss.str();
return true;
}
bool visit_str(const char* v, uint32_t size) {
m_s += '"' + std::string(v, size) + '"';
return true;
}
bool start_array(uint32_t /*num_elements*/) {
m_s += "[";
return true;
}
bool end_array_item() {
m_s += ",";
return true;
}
bool end_array() {
m_s.erase(m_s.size() - 1, 1); // remove the last ','
m_s += "]";
return true;
}
bool start_map(uint32_t /*num_kv_pairs*/) {
m_s += "{";
return true;
}
bool end_map_key() {
m_s += ":";
return true;
}
bool end_map_value() {
m_s += ",";
return true;
}
bool end_map() {
m_s.erase(m_s.size() - 1, 1); // remove the last ','
m_s += "}";
return true;
}
void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
}
void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
}
std::string& m_s;
};
int main() {
std::stringstream ss;
std::map<std::string, std::vector<int>> v1 {
{ "ABC", { 1, 2, 3 } },
{ "DEFG", { 4, 5 } }
};
std::vector<std::string> v2 {
"HIJ", "KLM", "NOP"
};
msgpack::pack(ss, v1);
msgpack::pack(ss, v2);
std::string const& buf = ss.str();
auto it = buf.begin();
auto end = buf.end();
{
std::string str;
bool ret = msgpack::parse(it, end, json_like_visitor(str));
// it is updated here.
assert(ret);
std::cout << str << std::endl;
}
{
std::string str;
bool ret = msgpack::parse(it, end, json_like_visitor(str));
// it is updated here.
assert(ret);
std::cout << str << std::endl;
}
}

View File

@@ -1,248 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <sstream>
#include <cassert>
#include <thread>
// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp
// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE.
//#define MSGPACK_USE_X3_PARSE
#include <msgpack.hpp>
#include <boost/asio.hpp>
#include <boost/coroutine2/all.hpp>
#if defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif // defined(__clang__)
#include <boost/spirit/home/support/multi_pass.hpp>
#if defined(__clang__)
#pragma GCC diagnostic pop
#endif // defined(__clang__)
namespace as = boost::asio;
namespace x3 = boost::spirit::x3;
namespace coro2 = boost::coroutines2;
using pull_type = coro2::asymmetric_coroutine<std::shared_ptr<std::vector<char>>>::pull_type;
// iterator fetching data from coroutine2.
class buffered_iterator : public std::iterator<std::input_iterator_tag, char> {
public:
using pointer_t = typename iterator::pointer;
using reference_t = typename iterator::reference;
explicit buffered_iterator(pull_type& source) noexcept
: source_{ &source } {
fetch_();
}
buffered_iterator() = default;
bool operator==(buffered_iterator const& other) const noexcept {
if (!other.source_ && !source_ && !other.buf_ && !buf_) return true;
return other.it_ == it_;
}
bool operator!=(buffered_iterator const& other) const noexcept {
return !(other == *this);
}
buffered_iterator & operator++() {
increment_();
return * this;
}
buffered_iterator operator++(int) = delete;
reference_t operator*() noexcept {
return *it_;
}
pointer_t operator->() noexcept {
return std::addressof(*it_);
}
private:
void fetch_() noexcept {
BOOST_ASSERT( nullptr != source_);
if (*source_) {
buf_ = source_->get();
it_ = buf_->begin();
}
else {
source_ = nullptr;
buf_.reset();
}
}
void increment_() {
BOOST_ASSERT( nullptr != source_);
BOOST_ASSERT(*source_);
if (++it_ == buf_->end()) {
(*source_)();
fetch_();
}
}
private:
pull_type* source_{ nullptr };
std::shared_ptr<std::vector<char>> buf_;
std::vector<char>::iterator it_;
};
// session class that corresponding to each client
class session : public std::enable_shared_from_this<session> {
public:
session(as::ip::tcp::socket socket)
: socket_(std::move(socket)) {
}
void start() {
sink_ = std::make_shared<coro2::asymmetric_coroutine<std::shared_ptr<std::vector<char>>>::push_type>(
[&, this](pull_type& source) {
// *1 is started when the first sink is called.
std::cout << "session started" << std::endl;
do_read();
source();
// use buffered_iterator here
// b is incremented in msgpack::unpack() and fetch data from sink
// via coroutine2 mechanism
auto b = boost::spirit::make_default_multi_pass(buffered_iterator(source));
auto e = boost::spirit::make_default_multi_pass(buffered_iterator());
// This is usually an infinity look, but for test, loop is finished when
// two message pack data is processed.
for (int i = 0; i != 2; ++i) {
auto oh = msgpack::unpack(b, e);
std::cout << oh.get() << std::endl;
}
}
);
// send dummy data to start *1
(*sink_)({});
}
private:
void do_read() {
std::cout << "session do_read() is called" << std::endl;
auto self(shared_from_this());
auto data = std::make_shared<std::vector<char>>(static_cast<std::size_t>(max_length));
socket_.async_read_some(
boost::asio::buffer(*data),
[this, self, data]
(boost::system::error_code ec, std::size_t length) {
if (!ec) {
data->resize(length);
(*sink_)(data);
do_read();
}
}
);
}
as::ip::tcp::socket socket_;
static constexpr std::size_t const max_length = 1024;
std::shared_ptr<coro2::asymmetric_coroutine<std::shared_ptr<std::vector<char>>>::push_type> sink_;
};
class server {
public:
server(
as::io_service& ios,
std::uint16_t port)
: acceptor_(ios, as::ip::tcp::endpoint(as::ip::tcp::v4(), port)),
socket_(ios) {
do_accept();
std::cout << "server start accept" << std::endl;
ios.run();
}
private:
void do_accept() {
acceptor_.async_accept(
socket_,
[this](boost::system::error_code ec) {
if (!ec) {
std::make_shared<session>(std::move(socket_))->start();
}
// for test, only one session is accepted.
// do_accept();
}
);
}
as::ip::tcp::acceptor acceptor_;
as::ip::tcp::socket socket_;
};
int main() {
std::thread srv(
[]{
boost::asio::io_service ios;
server s(ios, 12345);
}
);
std::thread cli(
[]{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "client start" << std::endl;
std::stringstream ss;
std::map<std::string, std::vector<int>> v1 {
{ "ABC", { 1, 2, 3 } },
{ "DEFG", { 4, 5 } }
};
std::vector<std::string> v2 {
"HIJ", "KLM", "NOP"
};
msgpack::pack(ss, v1);
msgpack::pack(ss, v2);
auto send_data = ss.str();
boost::asio::io_service ios;
as::ip::tcp::resolver::query q("127.0.0.1", "12345");
as::ip::tcp::resolver r(ios);
auto it = r.resolve(q);
std::cout << "client connect" << std::endl;
as::ip::tcp::socket s(ios);
as::connect(s, it);
std::size_t const size = 5;
std::size_t rest = send_data.size();
std::size_t index = 0;
while (rest != 0) {
std::cout << "client send data" << std::endl;
auto send_size = size < rest ? size : rest;
as::write(s, as::buffer(&send_data[index], send_size));
rest -= send_size;
index += send_size;
std::cout << "client wait" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
);
cli.join();
std::cout << "client joinded" << std::endl;
srv.join();
std::cout << "server joinded" << std::endl;
}

View File

@@ -1,43 +0,0 @@
// MessagePack for C++ example
//
// Copyright (C) 2017 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <sstream>
#include <cassert>
// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp
// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE.
#include <msgpack.hpp>
int main() {
std::stringstream ss;
std::map<std::string, std::vector<int>> v1 {
{ "ABC", { 1, 2, 3 } },
{ "DEFG", { 4, 5 } }
};
std::vector<std::string> v2 {
"HIJ", "KLM", "NOP"
};
msgpack::pack(ss, v1);
msgpack::pack(ss, v2);
std::string const& buf = ss.str();
auto it = buf.begin();
auto end = buf.end();
{
auto oh = msgpack::unpack(it, end);
// it is updated here.
assert(v1 == (oh.get().as<std::map<std::string, std::vector<int>>>()));
}
{
auto oh = msgpack::unpack(it, end);
assert(v2 == oh.get().as<std::vector<std::string>>());
}
}