BVB Source Codes

uWebSockets Show Group.h Source code

Return Download uWebSockets: download Group.h Source code - Download uWebSockets Source code - Type:.h
  1. #ifndef GROUP_UWS_H
  2. #define GROUP_UWS_H
  3.  
  4. #include "WebSocket.h"
  5. #include "HTTPSocket.h"
  6. #include "Extensions.h"
  7. #include <functional>
  8. #include <stack>
  9.  
  10. namespace uWS {
  11.  
  12. enum ListenOptions {
  13.     TRANSFERS
  14. };
  15.  
  16. struct Hub;
  17.  
  18. template <bool isServer>
  19. struct WIN32_EXPORT Group : private uS::NodeData {
  20. protected:
  21.     friend struct Hub;
  22.     friend struct WebSocket<isServer>;
  23.     friend struct HttpSocket<false>;
  24.     friend struct HttpSocket<true>;
  25.  
  26.     std::function<void(WebSocket<isServer> *, HttpRequest)> connectionHandler;
  27.     std::function<void(WebSocket<isServer> *)> transferHandler;
  28.     std::function<void(WebSocket<isServer> *, char *message, size_t length, OpCode opCode)> messageHandler;
  29.     std::function<void(WebSocket<isServer> *, int code, char *message, size_t length)> disconnectionHandler;
  30.     std::function<void(WebSocket<isServer> *, char *, size_t)> pingHandler;
  31.     std::function<void(WebSocket<isServer> *, char *, size_t)> pongHandler;
  32.     std::function<void(HttpSocket<isServer> *)> httpConnectionHandler;
  33.     std::function<void(HttpResponse *, HttpRequest, char *, size_t, size_t)> httpRequestHandler;
  34.     std::function<void(HttpResponse *, char *, size_t, size_t)> httpDataHandler;
  35.     std::function<void(HttpResponse *)> httpCancelledRequestHandler;
  36.     std::function<void(HttpSocket<isServer> *)> httpDisconnectionHandler;
  37.     std::function<void(HttpSocket<isServer> *, HttpRequest)> httpUpgradeHandler;
  38.  
  39.     using errorType = typename std::conditional<isServer, int, void *>::type;
  40.     std::function<void(errorType)> errorHandler;
  41.  
  42.     unsigned int maxPayload;
  43.     Hub *hub;
  44.     int extensionOptions;
  45.     Timer *timer = nullptr, *httpTimer = nullptr;
  46.     std::string userPingMessage;
  47.     std::stack<Poll *> iterators;
  48.  
  49.     // todo: cannot be named user, collides with parent!
  50.     void *userData = nullptr;
  51.     static void timerCallback(Timer *timer);
  52.  
  53.     WebSocket<isServer> *webSocketHead = nullptr;
  54.     HttpSocket<isServer> *httpSocketHead = nullptr;
  55.  
  56.     void addWebSocket(WebSocket<isServer> *webSocket);
  57.     void removeWebSocket(WebSocket<isServer> *webSocket);
  58.  
  59.     // todo: remove these, template
  60.     void addHttpSocket(HttpSocket<isServer> *httpSocket);
  61.     void removeHttpSocket(HttpSocket<isServer> *httpSocket);
  62.  
  63.     Group(int extensionOptions, unsigned int maxPayload, Hub *hub, uS::NodeData *nodeData);
  64.     void stopListening();
  65.  
  66. public:
  67.     void onConnection(std::function<void(WebSocket<isServer> *, HttpRequest)> handler);
  68.     void onTransfer(std::function<void(WebSocket<isServer> *)> handler);
  69.     void onMessage(std::function<void(WebSocket<isServer> *, char *, size_t, OpCode)> handler);
  70.     void onDisconnection(std::function<void(WebSocket<isServer> *, int code, char *message, size_t length)> handler);
  71.     void onPing(std::function<void(WebSocket<isServer> *, char *, size_t)> handler);
  72.     void onPong(std::function<void(WebSocket<isServer> *, char *, size_t)> handler);
  73.     void onError(std::function<void(errorType)> handler);
  74.     void onHttpConnection(std::function<void(HttpSocket<isServer> *)> handler);
  75.     void onHttpRequest(std::function<void(HttpResponse *, HttpRequest, char *data, size_t length, size_t remainingBytes)> handler);
  76.     void onHttpData(std::function<void(HttpResponse *, char *data, size_t length, size_t remainingBytes)> handler);
  77.     void onHttpDisconnection(std::function<void(HttpSocket<isServer> *)> handler);
  78.     void onCancelledHttpRequest(std::function<void(HttpResponse *)> handler);
  79.     void onHttpUpgrade(std::function<void(HttpSocket<isServer> *, HttpRequest)> handler);
  80.  
  81.     // Thread safe
  82.     void broadcast(const char *message, size_t length, OpCode opCode);
  83.     void setUserData(void *user);
  84.     void *getUserData();
  85.  
  86.     // Not thread safe
  87.     void terminate();
  88.     void close(int code = 1000, char *message = nullptr, size_t length = 0);
  89.     void startAutoPing(int intervalMs, std::string userMessage = "");
  90.  
  91.     // same as listen(TRANSFERS), backwards compatible API for now
  92.     void addAsync() {
  93.         if (!async) {
  94.             NodeData::addAsync();
  95.         }
  96.     }
  97.  
  98.     void listen(ListenOptions listenOptions) {
  99.         if (listenOptions == TRANSFERS && !async) {
  100.             addAsync();
  101.         }
  102.     }
  103.  
  104.     template <class F>
  105.     void forEach(const F &cb) {
  106.         Poll *iterator = webSocketHead;
  107.         iterators.push(iterator);
  108.         while (iterator) {
  109.             Poll *lastIterator = iterator;
  110.             cb((WebSocket<isServer> *) iterator);
  111.             iterator = iterators.top();
  112.             if (lastIterator == iterator) {
  113.                 iterator = ((uS::Socket *) iterator)->next;
  114.                 iterators.top() = iterator;
  115.             }
  116.         }
  117.         iterators.pop();
  118.     }
  119.  
  120.     // duplicated code for now!
  121.     template <class F>
  122.     void forEachHttpSocket(const F &cb) {
  123.         Poll *iterator = httpSocketHead;
  124.         iterators.push(iterator);
  125.         while (iterator) {
  126.             Poll *lastIterator = iterator;
  127.             cb((HttpSocket<isServer> *) iterator);
  128.             iterator = iterators.top();
  129.             if (lastIterator == iterator) {
  130.                 iterator = ((uS::Socket *) iterator)->next;
  131.                 iterators.top() = iterator;
  132.             }
  133.         }
  134.         iterators.pop();
  135.     }
  136.  
  137.     static Group<isServer> *from(uS::Socket *s) {
  138.         return static_cast<Group<isServer> *>(s->getNodeData());
  139.     }
  140. };
  141.  
  142. }
  143.  
  144. #endif // GROUP_UWS_H
  145.  
downloadGroup.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