// // server.cpp // ~~~~~~~~~~ // // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // /* *该文件编写了一个简单的echo服务器。采用异步I/O的形式。特点在于自己编写了内存池,和会话管理。 **/ #include <cstdlib> #include <iostream> #include <boost/aligned_storage.hpp> #include <boost/array.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/noncopyable.hpp> #include <boost/shared_ptr.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; // Class to manage the memory to be used for handler-based custom allocation. // It contains a single block of memory which may be returned for allocation // requests. If the memory is in use when an allocation request is made, the // allocator delegates allocation to the global heap. //相当于内存池功能,避免大量异步I/O时内存new delete的开销。 class handler_allocator : private boost::noncopyable { public: //每次session只做了一次实例化。可以在该构造函数加入输出验证。 handler_allocator() : in_use_(false) { } void* allocate(std::size_t size) { if (!in_use_ && size < storage_.size) { in_use_ = true; return storage_.address(); } else { return ::operator new(size); } } void deallocate(void* pointer) { if (pointer == storage_.address()) { in_use_ = false; } else { ::operator delete(pointer); } } private: // Storage space used for handler-based custom memory allocation. //保证对齐的内存空间。 boost::aligned_storage<1024> storage_; // Whether the handler-based custom allocation storage has been used. bool in_use_; }; // Wrapper class template for handler objects to allow handler memory // allocation to be customised. Calls to operator() are forwarded to the // encapsulated handler. //函数对象,handle的再次封装,封装了内存分配和释放 template <typename Handler> class custom_alloc_handler { //通过该构造函数可以发现,每次异步读写操作会实例化一次 public: custom_alloc_handler(handler_allocator& a, Handler h) : allocator_(a), handler_(h) { } //实际上由内部的handle执行。外部只是一层函数对象进行包装。 template <typename Arg1> void operator()(Arg1 arg1) { handler_(arg1); } template <typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2) { handler_(arg1, arg2); } /*为handler默认的分配函数http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/asio_handler_allocate.html *异步操作需要分配一个临时对象,这对象和handle相对应。这些对象默认实现方式是new delete *所有handle相关的临时对象,将在调用该handle之前执行deallocated解分配。所以允许对对象的内存重用。 **/ friend void* asio_handler_allocate(std::size_t size, custom_alloc_handler<Handler>* this_handler) { return this_handler->allocator_.allocate(size);//自定义分配对异步临时对象内存 } //注意参数 friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/, custom_alloc_handler<Handler>* this_handler) { this_handler->allocator_.deallocate(pointer);//释放内存 } private: //注意这里是引用,而不是对象。 handler_allocator& allocator_; Handler handler_; }; // Helper function to wrap a handler object to add custom allocation.工厂类,模板参数独立于任何类型 template <typename Handler> inline custom_alloc_handler<Handler> make_custom_alloc_handler( handler_allocator& a, Handler h) { return custom_alloc_handler<Handler>(a, h);//返回的是一个函数对象。 } //会话类 //有关enable_shared_from_this当类对象被 shared_ptr 管理时。需要在类自己定义的函数里把当前类对象作为参数传给其他函数时, //不能返回裸指针这时需要传递一个 shared_ptr ,否则就不能保持 //shared_ptr 管理这个类对象的语义。http://www.2cto.com/kf/201212/175430.html http://blog.sina.com.cn/s/blog_62cd38470100g3dd.html class session : public boost::enable_shared_from_this<session> { public: session(boost::asio::io_service& io_service) : socket_(io_service) { } tcp::socket& socket() { return socket_; } void start() { //当数据读取完成后,执行后面的handle。第二个参数为一个函数handle。这个make_custom_alloc_handler怎么使用模板的参数? socket_.async_read_some(boost::asio::buffer(data_), make_custom_alloc_handler(allocator_, boost::bind(&session::handle_read,// bind会返回函数对象.主要函数成员是这样绑定的。要有个函数成员取地址和一个对象的占位符 shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)));//占位符,从该参数传递错误码和已读个数。 } void handle_read(const boost::system::error_code& error, size_t bytes_transferred) { if (!error)//如果没有发生错误,那么就异步写 { //sleep(10);经测试好像还是存在排队等待的现象 boost::asio::async_write(socket_, boost::asio::buffer(data_, bytes_transferred), make_custom_alloc_handler(allocator_, boost::bind(&session::handle_write, shared_from_this(), boost::asio::placeholders::error))); } } void handle_write(const boost::system::error_code& error) { //如果不发生错误,那么就执行异步写。 if (!error) { socket_.async_read_some(boost::asio::buffer(data_), make_custom_alloc_handler(allocator_, boost::bind(&session::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred))); } } private: // The socket used to communicate with the client. tcp::socket socket_; // Buffer used to store data received from the client.缓冲区 boost::array<char, 1024> data_; // The allocator to use for handler-based custom memory allocation. //每次会话所分配的内存池 handler_allocator allocator_; }; typedef boost::shared_ptr<session> session_ptr; class server { public: server(boost::asio::io_service& io_service, short port) : io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) { session_ptr new_session(new session(io_service_)); acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); } void handle_accept(session_ptr new_session, const boost::system::error_code& error) { if (!error) { new_session->start(); } new_session.reset(new session(io_service_));//重置指针 acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); } private: boost::asio::io_service& io_service_; tcp::acceptor acceptor_; }; int main(int argc, char* argv[]) { try { if (argc != 2) { std::cerr << "Usage: server <port>\n"; return 1; } boost::asio::io_service io_service; using namespace std; // For atoi. server s(io_service, atoi(argv[1])); io_service.run(); } catch (std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } return 0; }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。