BVB Source Codes

uWebSockets Show Asio.h Source code

Return Download uWebSockets: download Asio.h Source code - Download uWebSockets Source code - Type:.h
  1. #ifndef ASIO_H
  2. #define ASIO_H
  3.  
  4. #include <boost/asio.hpp>
  5.  
  6. typedef boost::asio::ip::tcp::socket::native_type uv_os_sock_t;
  7. static const int UV_READABLE = 1;
  8. static const int UV_WRITABLE = 2;
  9.  
  10. struct Loop : boost::asio::io_service {
  11.  
  12.     static Loop *createLoop(bool defaultLoop = true) {
  13.         return new Loop;
  14.     }
  15.  
  16.     void destroy() {
  17.         delete this;
  18.     }
  19.  
  20.     void run() {
  21.         boost::asio::io_service::run();
  22.     }
  23. };
  24.  
  25. struct Timer {
  26.     boost::asio::deadline_timer asio_timer;
  27.     void *data;
  28.  
  29.     Timer(Loop *loop) : asio_timer(*loop) {
  30.  
  31.     }
  32.  
  33.     void start(void (*cb)(Timer *), int first, int repeat) {
  34.         asio_timer.expires_from_now(boost::posix_time::milliseconds(first));
  35.         asio_timer.async_wait([this, cb, repeat](const boost::system::error_code &ec) {
  36.             if (ec != boost::asio::error::operation_aborted) {
  37.                 if (repeat) {
  38.                     start(cb, repeat, repeat);
  39.                 }
  40.                 cb(this);
  41.             }
  42.         });
  43.     }
  44.  
  45.     void setData(void *data) {
  46.         this->data = data;
  47.     }
  48.  
  49.     void *getData() {
  50.         return data;
  51.     }
  52.  
  53.     // bug: cancel does not cancel expired timers!
  54.     // it has to guarantee that the timer is not called after
  55.     // stop is called! ffs boost!
  56.     void stop() {
  57.         asio_timer.cancel();
  58.     }
  59.  
  60.     void close() {
  61.         asio_timer.get_io_service().post([this]() {
  62.             delete this;
  63.         });
  64.     }
  65. };
  66.  
  67. struct Async {
  68.     Loop *loop;
  69.     void (*cb)(Async *);
  70.     void *data;
  71.  
  72.     boost::asio::io_service::work asio_work;
  73.  
  74.     Async(Loop *loop) : loop(loop), asio_work(*loop) {
  75.     }
  76.  
  77.     void start(void (*cb)(Async *)) {
  78.         this->cb = cb;
  79.     }
  80.  
  81.     void send() {
  82.         loop->post([this]() {
  83.             cb(this);
  84.         });
  85.     }
  86.  
  87.     void close() {
  88.         loop->post([this]() {
  89.             delete this;
  90.         });
  91.     }
  92.  
  93.     void setData(void *data) {
  94.         this->data = data;
  95.     }
  96.  
  97.     void *getData() {
  98.         return data;
  99.     }
  100. };
  101.  
  102. struct Poll {
  103.     boost::asio::posix::stream_descriptor *socket;
  104.     void (*cb)(Poll *p, int status, int events);
  105.  
  106.     Poll(Loop *loop, uv_os_sock_t fd) {
  107.         socket = new boost::asio::posix::stream_descriptor(*loop, fd);
  108.         socket->non_blocking(true);
  109.     }
  110.  
  111.     bool isClosed() {
  112.         return !socket;
  113.     }
  114.  
  115.     boost::asio::ip::tcp::socket::native_type getFd() {
  116.         return socket ? socket->native_handle() : -1;
  117.     }
  118.  
  119.     void setCb(void (*cb)(Poll *p, int status, int events)) {
  120.         this->cb = cb;
  121.     }
  122.  
  123.     void (*getCb())(Poll *, int, int) {
  124.         return cb;
  125.     }
  126.  
  127.     void reInit(Loop *loop, uv_os_sock_t fd) {
  128.         delete socket;
  129.         socket = new boost::asio::posix::stream_descriptor(*loop, fd);
  130.         socket->non_blocking(true);
  131.     }
  132.  
  133.     void start(Loop *, Poll *self, int events) {
  134.         if (events & UV_READABLE) {
  135.             socket->async_read_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
  136.                 if (ec != boost::asio::error::operation_aborted) {
  137.                     self->start(nullptr, self, UV_READABLE);
  138.                     self->cb(self, ec ? -1 : 0, UV_READABLE);
  139.                 }
  140.             });
  141.         }
  142.  
  143.         if (events & UV_WRITABLE) {
  144.             socket->async_write_some(boost::asio::null_buffers(), [self](boost::system::error_code ec, std::size_t) {
  145.                 if (ec != boost::asio::error::operation_aborted) {
  146.                     self->start(nullptr, self, UV_WRITABLE);
  147.                     self->cb(self, ec ? -1 : 0, UV_WRITABLE);
  148.                 }
  149.             });
  150.         }
  151.     }
  152.  
  153.     void change(Loop *, Poll *self, int events) {
  154.         socket->cancel();
  155.         start(nullptr, self, events);
  156.     }
  157.  
  158.     bool fastTransfer(Loop *loop, Loop *newLoop, int events) {
  159.         return false;
  160.     }
  161.  
  162.     // todo: asio is thread safe, use it!
  163.     bool threadSafeChange(Loop *loop, Poll *self, int events) {
  164.         return false;
  165.     }
  166.  
  167.     void stop(Loop *) {
  168.         socket->cancel();
  169.     }
  170.  
  171.     // this is not correct, but it works for now
  172.     // think about transfer - should allow one to not delete
  173.     // but in this case it doesn't matter at all
  174.     void close(Loop *loop, void (*cb)(Poll *)) {
  175.         socket->release();
  176.         socket->get_io_service().post([cb, this]() {
  177.             cb(this);
  178.         });
  179.         delete socket;
  180.         socket = nullptr;
  181.     }
  182. };
  183.  
  184. #endif // ASIO_H
  185.  
downloadAsio.h Source code - Download uWebSockets Source code
Related Source Codes/Software:
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top