Created
September 6, 2011 09:25
-
-
Save dot/1197094 to your computer and use it in GitHub Desktop.
messagepack with raw_ref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -g -O0 -Wall -fPIC -I/usr/local/include/ -L/usr/local/lib/ -lmsgpack main.cc -o main | |
#include <iostream> | |
#include <msgpack.hpp> | |
struct Result { | |
public: | |
msgpack::type::raw_ref message; | |
Result() {} | |
MSGPACK_DEFINE( message); | |
}; | |
int main(int argc, char** argv) { | |
if ( argc != 2 ) { | |
std::cout << "Usage: " << argv[0] << " [message]" << std::endl; | |
exit(1); | |
} | |
try { | |
std::cout << "inputed..." << argv[1] << std::endl; | |
Result r; | |
size_t len = strlen(argv[1]); | |
char* m = (char *)malloc(len + 128); | |
sprintf(m, "hello %s", argv[1]); | |
r.message = msgpack::type::raw_ref(m, strlen(m) + 1); | |
msgpack::sbuffer sbuf; | |
msgpack::pack(sbuf, r); | |
free(m); | |
msgpack::unpacked msg; | |
msgpack::unpack(&msg, sbuf.data(), sbuf.size()); | |
msgpack::object obj = msg.get(); | |
Result r2; | |
obj.convert(&r2); | |
std::cout << "length: " << r2.message.size << std::endl; | |
std::cout << "message: " << r2.message.ptr << std::endl; | |
} catch (std::exception& exc) { | |
std::cerr << "failed: " << exc.what() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment