BVB Source Codes

uWebSockets Show lws.cpp Source code

Return Download uWebSockets: download lws.cpp Source code - Download uWebSockets Source code - Type:.cpp
  1. /* this is an echo server in libwebsockets, it makes use of some of the optimizations found in 碌WS, like the thin Queue for minimal memory footprint */
  2.  
  3. #include <libwebsockets.h>
  4. #include <iostream>
  5. #include <new>
  6. using namespace std;
  7.  
  8. struct Message {
  9.     char *data;
  10.     size_t length;
  11.     Message *nextMessage = nullptr;
  12. };
  13.  
  14. struct Queue {
  15.     Message *head = nullptr, *tail = nullptr;
  16.     void pop()
  17.     {
  18.         Message *nextMessage;
  19.         if ((nextMessage = head->nextMessage)) {
  20.             delete [] (char *) head;
  21.             head = nextMessage;
  22.         } else {
  23.             delete [] (char *) head;
  24.             head = tail = nullptr;
  25.         }
  26.     }
  27.  
  28.     bool empty() {
  29.         return head == nullptr;
  30.     }
  31.  
  32.     Message *front()
  33.     {
  34.         return head;
  35.     }
  36.  
  37.     void push(Message *message)
  38.     {
  39.         if (tail) {
  40.             tail->nextMessage = message;
  41.             tail = message;
  42.         } else {
  43.             head = message;
  44.             tail = message;
  45.         }
  46.     }
  47. };
  48.  
  49. struct SocketExtension {
  50.     string buffer;
  51.     Queue messages;
  52. };
  53.  
  54. int callback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len)
  55. {
  56.     SocketExtension *ext = (SocketExtension *) user;
  57.  
  58.     switch (reason) {
  59.  
  60.     case LWS_CALLBACK_SERVER_WRITEABLE:
  61.     {
  62.         do {
  63.             if (ext->messages.empty())
  64.                 break;
  65.  
  66.             Message *messagePtr = ext->messages.front();
  67.             lws_write(wsi, (unsigned char *) messagePtr->data, messagePtr->length, LWS_WRITE_BINARY);
  68.  
  69.             ext->messages.pop();
  70.         } while(!ext->messages.empty() && !lws_partial_buffered(wsi));
  71.  
  72.         if (!ext->messages.empty()) {
  73.             lws_callback_on_writable(wsi);
  74.         }
  75.  
  76.         break;
  77.     }
  78.  
  79.     case LWS_CALLBACK_ESTABLISHED:
  80.     {
  81.         new (ext) SocketExtension;
  82.         break;
  83.     }
  84.  
  85.     case LWS_CALLBACK_CLOSED:
  86.     {
  87.         ext->~SocketExtension();
  88.         break;
  89.     }
  90.  
  91.     case LWS_CALLBACK_RECEIVE:
  92.     {
  93.         ext->buffer.append((char *) in, len);
  94.         if (!lws_remaining_packet_payload(wsi)) {
  95.  
  96.             Message *messagePtr = (Message *) new char[sizeof(Message) + ext->buffer.length() + LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING];
  97.             messagePtr->length = ext->buffer.length();
  98.             messagePtr->data = ((char *) messagePtr) + sizeof(Message) + LWS_SEND_BUFFER_PRE_PADDING;
  99.             messagePtr->nextMessage = nullptr;
  100.             memcpy(messagePtr->data, ext->buffer.data(), ext->buffer.length());
  101.  
  102.             bool wasEmpty = ext->messages.empty();
  103.             ext->messages.push(messagePtr);
  104.             ext->buffer.clear();
  105.  
  106.             if (wasEmpty) {
  107.                 lws_callback_on_writable(wsi);
  108.             }
  109.         }
  110.         break;
  111.     }
  112.     }
  113.     return 0;
  114. }
  115.  
  116. int main(int argc, char *argv[])
  117. {
  118.     lws_set_log_level(0, nullptr);
  119.  
  120.     lws_protocols protocols[] = {
  121.         {"default", callback, sizeof(SocketExtension)},
  122.         {nullptr, nullptr, 0}
  123.     };
  124.  
  125.     lws_context_creation_info info = {};
  126.     info.port = 3000;
  127.  
  128.     info.protocols = protocols;
  129.     info.gid = info.uid = -1;
  130.     info.options |= LWS_SERVER_OPTION_LIBEV;
  131.  
  132.     lws_context *context;
  133.     if (!(context = lws_create_context(&info))) {
  134.         cout << "ERR_LISTEN" << endl;
  135.         return -1;
  136.     }
  137.  
  138.     lws_ev_initloop(context, ev_default_loop(), 0);
  139.     ev_run(ev_default_loop());
  140. }
  141.  
  142.  
downloadlws.cpp 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