#include #include #include #include class session { public: session(asio::basic_demuxer& d) : file_(d, "/tmp/teste.txt", O_CREAT | O_RDWR | O_APPEND) { std::cout << "session::session" << std::endl; } ~session() { std::cout << "session::~session" << std::endl; } asio::stream_file& file() { return file_; } void start() { std::cout << "session::start" << std::endl; memset(data_, 0, max_length); file_.async_write(data_, max_length, 0, boost::bind(&session::handle_write, this, _1, _2)); std::cout << "async_read, finished start" << std::endl; } void handle_write(const asio::error& error, size_t bytes_writed) { std::cout << "session::handle_write" << std::endl; if(!error && bytes_writed > 0) { std::cout << "bytes: " << bytes_writed << std::endl; } else if(error) { std::cout << "error != 0" << std::endl; std::cout << error << std::endl; } else { std::cout << "0 bytes writed" << std::endl; } delete this; } private: asio::stream_file file_; static const int max_length = 512; char data_[max_length]; }; class server { public: server(asio::basic_demuxer& d) : demuxer_(d) { session* new_session = new session(demuxer_); new_session->start(); } private: asio::basic_demuxer& demuxer_; }; int main(int argc, char*argv[]) { try { asio::basic_demuxer d; server s(d); d.run(); } catch (asio::error& e) { std::cerr << e.what() << std::endl; } catch (std::exception& e) { std::cerr << "exception: " << e.what() << std::endl; } return 0; }