mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-13 22:50:19 +02:00
Moved examples.
Added intrusive and non-intrusive versions of packing and unpacking examples.
This commit is contained in:
53
example/cpp03/class_intrusive.cpp
Normal file
53
example/cpp03/class_intrusive.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#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) {}
|
||||
|
||||
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(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::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
my_class my("John Smith", 42);
|
||||
std::stringstream ss;
|
||||
msgpack::pack(ss, my);
|
||||
|
||||
print(ss.str());
|
||||
|
||||
msgpack::unpacked unp;
|
||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||
msgpack::object obj = unp.get();
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
96
example/cpp03/class_non_intrusive.cpp
Normal file
96
example/cpp03/class_non_intrusive.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
#include <msgpack_fwd.hpp>
|
||||
|
||||
|
||||
// declarations
|
||||
class my_class;
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
|
||||
|
||||
object const& operator>> (object const& o, my_class& v);
|
||||
|
||||
template <typename Stream>
|
||||
packer<Stream>& operator<< (packer<Stream>& o, my_class const& v);
|
||||
|
||||
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
|
||||
} // namespace msgpack
|
||||
|
||||
#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) {}
|
||||
|
||||
// When you want to define a non-intrusive adaptor functions,
|
||||
// member variables should be public.
|
||||
std::string name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
inline bool operator==(my_class const& lhs, my_class const& rhs) {
|
||||
return lhs.name_ == rhs.name_ && lhs.age_ == rhs.age_;
|
||||
}
|
||||
|
||||
// definitions
|
||||
|
||||
namespace msgpack {
|
||||
|
||||
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
|
||||
|
||||
inline object const& operator>> (object const& o, my_class& v) {
|
||||
if (o.type != msgpack::type::ARRAY) throw msgpack::type_error();
|
||||
if (o.via.array.size != 2) throw msgpack::type_error();
|
||||
o.via.array.ptr[0].convert(v.name_);
|
||||
o.via.array.ptr[1].convert(v.age_);
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
template <typename Stream>
|
||||
inline packer<Stream>& operator<< (packer<Stream>& o, my_class const& v) {
|
||||
// packing member variables as an array.
|
||||
o.pack_array(2);
|
||||
o.pack(v.name_);
|
||||
o.pack(v.age_);
|
||||
return o;
|
||||
}
|
||||
|
||||
} // 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::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
my_class my("John Smith", 42);
|
||||
std::stringstream ss;
|
||||
msgpack::pack(ss, my);
|
||||
|
||||
print(ss.str());
|
||||
|
||||
msgpack::unpacked unp;
|
||||
msgpack::unpack(unp, ss.str().data(), ss.str().size());
|
||||
msgpack::object obj = unp.get();
|
||||
assert(obj.as<my_class>() == my);
|
||||
}
|
58
example/cpp03/custom.cpp
Normal file
58
example/cpp03/custom.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <msgpack.hpp>
|
||||
#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;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, oc);
|
||||
|
||||
msgpack::unpacked result;
|
||||
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
||||
msgpack::object obj = result.get();
|
||||
|
||||
obj.convert(&nc);
|
||||
|
||||
std::cout << obj << " value=" << nc.value << " flag=" << nc.flag << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
new_class nc;
|
||||
old_class oc;
|
||||
|
||||
msgpack::sbuffer sbuf;
|
||||
msgpack::pack(sbuf, nc);
|
||||
|
||||
msgpack::unpacked result;
|
||||
msgpack::unpack(result, sbuf.data(), sbuf.size());
|
||||
msgpack::object obj = result.get();
|
||||
|
||||
obj.convert(&oc);
|
||||
|
||||
std::cout << obj << " value=" << oc.value << std::endl;
|
||||
}
|
||||
}
|
||||
|
87
example/cpp03/protocol.cpp
Normal file
87
example/cpp03/protocol.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
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::unpacked result;
|
||||
msgpack::unpack(result, buffer.data(), buffer.size());
|
||||
msgpack::object o = result.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::unpacked result;
|
||||
msgpack::unpack(result, buffer.data(), buffer.size());
|
||||
msgpack::object o = result.get();
|
||||
|
||||
|
||||
myprotocol::MultiGet req;
|
||||
o.convert(req);
|
||||
std::cout << "received: " << o << std::endl;
|
||||
}
|
||||
}
|
||||
|
38
example/cpp03/simple.cpp
Normal file
38
example/cpp03/simple.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#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::unpacked result;
|
||||
|
||||
msgpack::unpack(result, str.data(), str.size());
|
||||
|
||||
// deserialized object is valid during the msgpack::unpacked instance alive.
|
||||
msgpack::object deserialized = result.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;
|
||||
}
|
||||
|
55
example/cpp03/speed_test.cpp
Normal file
55
example/cpp03/speed_test.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
// 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::unpacked unpacked;
|
||||
std::cout << "Start unpacking...by void unpack(unpacked& result, const char* data, size_t len)" << std::endl;
|
||||
{
|
||||
boost::timer::cpu_timer timer;
|
||||
msgpack::unpack(unpacked, 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;
|
||||
unpacked.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();
|
||||
}
|
||||
|
78
example/cpp03/speed_test_nested_array.cpp
Normal file
78
example/cpp03/speed_test_nested_array.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
// 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 (int 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 (int 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 = 16;
|
||||
std::cout << "Setting up array data..." << std::endl;
|
||||
typename 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::unpacked unpacked;
|
||||
std::cout << "Start unpacking...by void unpack(unpacked& result, const char* data, size_t len)" << std::endl;
|
||||
{
|
||||
boost::timer::cpu_timer timer;
|
||||
msgpack::unpack(unpacked, str.data(), str.size());
|
||||
std::string result = timer.format();
|
||||
std::cout << result << std::endl;
|
||||
}
|
||||
std::cout << "Unpack finished..." << std::endl;
|
||||
typename vecvec<int, depth>::type v2;
|
||||
std::cout << "Start converting..." << std::endl;
|
||||
{
|
||||
boost::timer::cpu_timer timer;
|
||||
unpacked.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();
|
||||
}
|
||||
|
133
example/cpp03/stream.cpp
Normal file
133
example/cpp03/stream.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <msgpack.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server(int sock) : m_sock(sock) { }
|
||||
|
||||
~Server() { }
|
||||
|
||||
typedef msgpack::unique_ptr<msgpack::zone> auto_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::unpacked result;
|
||||
while (m_pac.next(&result)) {
|
||||
msgpack::object msg = result.get();
|
||||
auto_zone& life = result.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, auto_zone& life)
|
||||
{
|
||||
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];
|
||||
pipe(pair);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user