MsgPack + ASIO
2013-06-14
Choi Wonseok
MSGPACK
MessagePack is an efficient binary serialization format.
msgpack C++ API
Data
#include "msgpack.hpp"class cpx { double re; double im; public: MSGPACK_DEFINE(re, im); ... };
MSGPACK c++ API
Pack (Serialization)
cpx data;data.set_re(1.0); data.set_im(2.13); msgpack::sbuffer sbuf; msgpack::pack(sbuf, data);
MSGPACK C++ API
Unpack (Deserialization)
msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
cpx rdata;
obj.convert(&rdata);Boost asio
Boost.Asio is a cross-platform c++ library for network and low-level I/O programming...
Boost asio anatomy

The io_service represents your program's link to the operating system's I/O services.
boost asio anatomy

The io_service signals to the operating system that it should start an asynchronous
connect.
Boost asio anatomy

the io_service
dequeues the result of the operation, translates it into an
error_code, and then passes it to your completion handler. Boost asio buffer
buffers can be simply expressed
as a tuple consisting of a pointer and a size in bytes.
streams
Many I/O objects in Boost.Asio are stream-oriented. This means that:
- There are no message boundaries. The data being transferred is a continuous sequence of bytes.
- Read or write operations may transfer fewer bytes than requested. This is referred to as a short read or short write. use: read(), async_read(), write(), async_write().
tcp client skeleton
#include "boost/asio.hpp"...using boost::asio::ip::tcp;...int main(int argc, char *argv[]){...boost::asio::io_service io_service;tcp::resolver resolver(io_service);tcp::resolver::query query(argv[1], argv[2]); // addr, porttcp::resolver::iterator endpoint_iter = relover.resolve(query);tcp::socket socket(io_service);boost::asio::connect(socket, endpoint_iter);...
practice
- build your program
- and test: mts <ipaddress> <port> <your name> <id> <gpa>
example code
MsgPack + ASIO
By Won Seok Choi
MsgPack + ASIO
- 4,137