Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is part of mvcpp - a web based model-view-controller.
- // mvcpp is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- // mvcpp is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with mvcpp. If not, see <http://www.gnu.org/licenses/>.
- //
- // (C) Copyright 2016 by Sebastian Büttner <sebastian.buettner@powerserverplus.net>
- #include <io/buffer.hpp>
- #include <io/binary_reader.hpp>
- #include <io/binary_writer.hpp>
- #include <iostream>
- #include <arpa/inet.h>
- struct my_header
- {
- uint16_t data_length;
- };
- namespace io
- {
- template<typename T, typename Traits>
- struct pack_traits<T, Traits, my_header>
- {
- static void pack(basic_binary_writer<T, Traits> &writer, const my_header &header)
- {
- writer << ::htons(header.data_length);
- }
- static void unpack(basic_binary_reader<T, Traits> &reader, my_header &header)
- {
- reader >> header.data_length;
- ::ntohs(header.data_length);
- }
- };
- }
- int main()
- {
- io::buffer buf;
- {
- auto writer = io::make_binary_writer(buf);
- my_header header{
- .data_length = 1024,
- };
- writer << header;
- }
- {
- auto reader = io::make_binary_reader(buf);
- my_header header;
- reader >> header;
- std::cout << "header.data_length = " << header.data_length << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement