ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

108 lines
2.7 KiB

  1. #include <booster/shared_ptr.h>
  2. #include <booster/enable_shared_from_this.h>
  3. #include <booster/aio/io_service.h>
  4. #include <booster/aio/buffer.h>
  5. #include <booster/aio/endpoint.h>
  6. #include <booster/aio/acceptor.h>
  7. #include <booster/aio/stream_socket.h>
  8. #include <iostream>
  9. #include <functional>
  10. class echo_session : public booster::enable_shared_from_this<echo_session> {
  11. public:
  12. echo_session(booster::aio::io_service &s) : socket_(s)
  13. {
  14. }
  15. void run()
  16. {
  17. socket_.async_read_some(
  18. booster::aio::buffer(buffer_,sizeof(buffer_)),
  19. std::bind(&echo_session::on_read,shared_from_this(),
  20. std::placeholders::_1,
  21. std::placeholders::_2));
  22. }
  23. void on_read(booster::system::error_code const &e,size_t tr)
  24. {
  25. if(e) return;
  26. socket_.async_write(booster::aio::buffer(buffer_,tr),
  27. std::bind(&echo_session::on_written,shared_from_this(),std::placeholders::_1));
  28. }
  29. void on_written(booster::system::error_code const &e)
  30. {
  31. if(e) return;
  32. run();
  33. }
  34. private:
  35. friend class echo_acceptor;
  36. char buffer_[1024];
  37. booster::aio::stream_socket socket_;
  38. };
  39. class echo_acceptor {
  40. public:
  41. echo_acceptor(booster::aio::io_service &srv) : acceptor_(srv)
  42. {
  43. booster::aio::endpoint ep("0.0.0.0",8080);
  44. acceptor_.open(ep.family());
  45. acceptor_.set_option(booster::aio::acceptor::reuse_address,true);
  46. acceptor_.bind(ep);
  47. acceptor_.listen(10);
  48. }
  49. void run()
  50. {
  51. new_session_.reset(new echo_session(acceptor_.get_io_service()));
  52. acceptor_.async_accept(new_session_->socket_,
  53. std::bind(&echo_acceptor::on_accepted,this,std::placeholders::_1));
  54. }
  55. void on_accepted(booster::system::error_code const &e)
  56. {
  57. if(e) {
  58. std::cout << e.message() << std::endl;
  59. run();
  60. }
  61. else {
  62. new_session_->run();
  63. run();
  64. }
  65. }
  66. private:
  67. booster::aio::acceptor acceptor_;
  68. booster::shared_ptr<echo_session> new_session_;
  69. };
  70. int main()
  71. {
  72. try {
  73. booster::aio::io_service srv;
  74. #if !defined(_WIN32) && !defined(__CYGWIN__)
  75. booster::aio::basic_io_device stdin_device(srv);
  76. stdin_device.attach(0);
  77. std::cout << "Press any key to stop" << std::endl;
  78. stdin_device.on_readable(std::bind(&booster::aio::io_service::stop,&srv));
  79. #endif
  80. echo_acceptor acc(srv);
  81. acc.run();
  82. srv.run();
  83. }
  84. catch(std::exception const &e) {
  85. std::cerr<<e.what()<<std::endl;
  86. }
  87. }
  88. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4