Today I want to talk about how the network communication library in boost is designed and used, and because it has been working with the network recently, Big Data processing and machine learning are inseparable from the final use of the network for on-line deployment. First look at all the source code bar.
#include <cstdlib> #include <iostream> #include <memory> #include <utility> #include <boost/ asio.hpp> #include <stdint.h> #include "data.h" #include <ostream> #include <boost/archive/binary_ iarchive.hpp> #include <boost/archive/binary_oarchive.hpp>using boost::asio::ip::tcp;class session:public Std::enable_shared_from_this < session >{public:session (Boost::asio::ip::tcp::socket socket): socket_ (std:: Move (socket)) {}void start () {Do_read ();} Private:void do_read () {Auto self (shared_from_this ());//int32_t Data_length;boost::asio::async_read (socket_, boost: : Asio::buffer (&data_length, 4), [This, self] (boost::system::error_code EC, std::size_t length) {if (!EC) {Do_read_ Body ();}});} void Do_read_body () {Auto self (shared_from_this ()); Boost::asio::async_read (Socket_,buf_, Boost::asio::transfer_ Exactly (Data_length), [This, self] (boost::system::error_code EC, std::size_t length) {std::cout << length< <std::endl;if (!EC) {processmessage ();d o_read ();} Else{std::cout << "error" << Std::endl;});}; void ProcessMessage () {boost::archive::binary_iarchive ia (buf_); Translate_data Message;ia >> message;std::cout << message.width<<std::endl;}; int32_t data_length;boost::asio::ip::tcp::socket socket_;boost::asio::streambuf buf_;}; Class Server{public:server (boost::asio::io_service& io_service, short port): Acceptor_ (Io_service, Boost::asio:: Ip::tcp::endpoint (TCP::V4 (), port), socket_ (Io_service) {do_accept ();} Private:void do_accept () {acceptor_.async_accept (Socket_,[this] (boost::system::error_code EC) {if (!EC) {Std::make_ Shared<session> (Std::move (socket_))->start (); Do_accept ();});} Boost::asio::ip::tcp::acceptor acceptor_;boost::asio::ip::tcp::socket socket_;}; int main (int argc, char* argv[]) {try{/*if (argc! = 2) {std::cerr << "Usage:async_tcp_echo_server <port>\n"; re Turn 1;} */boost::asio::io_service io_service;server S (io_service, 8888); Io_service.run ();} catch (std::exception& e) {Std::cerr << "Exception:" << e.what () << "\ n";} return 0;}
This is the service area of all the source code.
Data is the format of the message, translate_data the definition of the class, which can be serialized using boost. The entire message is handled smoothly, allowing us to only care about the data flow and ignore how the server receives the message asynchronously. Of course, the data format in my Demo sample code is simple, and the int at the beginning of the packet indicates how many bytes the data has. Simply put out my message code together:
#include <boost/serialization/array.hpp> #include <boost/serialization/vector.hpp>struct measurementz{ Double m_a[3];d ouble m_w[3];d ouble m_t;friend class Boost::serialization::access;template<class Archive>void Serialize (archive& ar, const unsigned int version) {AR & Boost::serialization::make_array (m_a, 3 * sizeof (double) AR & Boost::serialization::make_array (m_w, 3 * sizeof (double)); Ar & m_t;}}; struct Translate_data{int width;int height;double mt;int n;unsigned char* buffer;//640*480;std::vector< measurementz> z;std::vector<double> quaterniond;//4std::vector<double> Vector3d;//3double M_rM_t;}; Boost_serialization_split_free (translate_data) namespace Boost{namespace serialization{/** SERIALIZATION support for Cv::mat */template<class archive>void Save (Archive & AR, const translate_data& m, const unsigned int version ) {ar & M.width;ar & M.height;ar & M.mt;ar & m.n;const size_t data_size = m.width * m.height*sizeof (unsigned char), AR & Boost::serialization::make_array (M.buffer, data_size), AR & M.z;ar & M. Quaterniond;ar & M.vector3d;ar & m.m_rm_t;} /** serialization support for Cv::mat */template <class archive>void load (Archive & AR, translate_data& m, c onst unsigned int version) {ar & M.width;ar & M.height;ar & M.mt;ar & m.n;const size_t data_size = m.width * m.height*sizeof (unsigned char); m.buffer = new unsigned Char[m.width*m.height];ar & Boost::serialization::make_ Array (M.buffer, data_size), AR & M.z;ar & M.quaterniond;ar & M.vector3d;ar & m.m_rm_t;}}};
The whole project is easy, please use the C++11 standard to complete.
Boost Asioserver Use