BVB Source Codes

uWebSockets Show Group.cpp Source code

Return Download uWebSockets: download Group.cpp Source code - Download uWebSockets Source code - Type:.cpp
  1. #include "Group.h"
  2. #include "Hub.h"
  3.  
  4. namespace uWS {
  5.  
  6. template <bool isServer>
  7. void Group<isServer>::setUserData(void *user) {
  8.     this->userData = user;
  9. }
  10.  
  11. template <bool isServer>
  12. void *Group<isServer>::getUserData() {
  13.     return userData;
  14. }
  15.  
  16. template <bool isServer>
  17. void Group<isServer>::timerCallback(Timer *timer) {
  18.     Group<isServer> *group = (Group<isServer> *) timer->getData();
  19.  
  20.     group->forEach([](uWS::WebSocket<isServer> *webSocket) {
  21.         if (webSocket->hasOutstandingPong) {
  22.             webSocket->terminate();
  23.         } else {
  24.             webSocket->hasOutstandingPong = true;
  25.         }
  26.     });
  27.  
  28.     if (group->userPingMessage.length()) {
  29.         group->broadcast(group->userPingMessage.data(), group->userPingMessage.length(), OpCode::TEXT);
  30.     } else {
  31.         group->broadcast(nullptr, 0, OpCode::PING);
  32.     }
  33. }
  34.  
  35. template <bool isServer>
  36. void Group<isServer>::startAutoPing(int intervalMs, std::string userMessage) {
  37.     timer = new Timer(loop);
  38.     timer->setData(this);
  39.     timer->start(timerCallback, intervalMs, intervalMs);
  40.     userPingMessage = userMessage;
  41. }
  42.  
  43. template <bool isServer>
  44. void Group<isServer>::addHttpSocket(HttpSocket<isServer> *httpSocket) {
  45.     if (httpSocketHead) {
  46.         httpSocketHead->prev = httpSocket;
  47.         httpSocket->next = httpSocketHead;
  48.     } else {
  49.         httpSocket->next = nullptr;
  50.         // start timer
  51.         httpTimer = new Timer(hub->getLoop());
  52.         httpTimer->setData(this);
  53.         httpTimer->start([](Timer *httpTimer) {
  54.             Group<isServer> *group = (Group<isServer> *) httpTimer->getData();
  55.             group->forEachHttpSocket([](HttpSocket<isServer> *httpSocket) {
  56.                 if (httpSocket->missedDeadline) {
  57.                     httpSocket->terminate();
  58.                 } else if (!httpSocket->outstandingResponsesHead) {
  59.                     httpSocket->missedDeadline = true;
  60.                 }
  61.             });
  62.         }, 1000, 1000);
  63.     }
  64.     httpSocketHead = httpSocket;
  65.     httpSocket->prev = nullptr;
  66. }
  67.  
  68. template <bool isServer>
  69. void Group<isServer>::removeHttpSocket(HttpSocket<isServer> *httpSocket) {
  70.     if (iterators.size()) {
  71.         iterators.top() = httpSocket->next;
  72.     }
  73.     if (httpSocket->prev == httpSocket->next) {
  74.         httpSocketHead = nullptr;
  75.         httpTimer->stop();
  76.         httpTimer->close();
  77.     } else {
  78.         if (httpSocket->prev) {
  79.             ((HttpSocket<isServer> *) httpSocket->prev)->next = httpSocket->next;
  80.         } else {
  81.             httpSocketHead = (HttpSocket<isServer> *) httpSocket->next;
  82.         }
  83.         if (httpSocket->next) {
  84.             ((HttpSocket<isServer> *) httpSocket->next)->prev = httpSocket->prev;
  85.         }
  86.     }
  87. }
  88.  
  89. template <bool isServer>
  90. void Group<isServer>::addWebSocket(WebSocket<isServer> *webSocket) {
  91.     if (webSocketHead) {
  92.         webSocketHead->prev = webSocket;
  93.         webSocket->next = webSocketHead;
  94.     } else {
  95.         webSocket->next = nullptr;
  96.     }
  97.     webSocketHead = webSocket;
  98.     webSocket->prev = nullptr;
  99. }
  100.  
  101. template <bool isServer>
  102. void Group<isServer>::removeWebSocket(WebSocket<isServer> *webSocket) {
  103.     if (iterators.size()) {
  104.         iterators.top() = webSocket->next;
  105.     }
  106.     if (webSocket->prev == webSocket->next) {
  107.         webSocketHead = nullptr;
  108.     } else {
  109.         if (webSocket->prev) {
  110.             ((WebSocket<isServer> *) webSocket->prev)->next = webSocket->next;
  111.         } else {
  112.             webSocketHead = (WebSocket<isServer> *) webSocket->next;
  113.         }
  114.         if (webSocket->next) {
  115.             ((WebSocket<isServer> *) webSocket->next)->prev = webSocket->prev;
  116.         }
  117.     }
  118. }
  119.  
  120. template <bool isServer>
  121. Group<isServer>::Group(int extensionOptions, unsigned int maxPayload, Hub *hub, uS::NodeData *nodeData) : uS::NodeData(*nodeData), maxPayload(maxPayload), hub(hub), extensionOptions(extensionOptions) {
  122.     connectionHandler = [](WebSocket<isServer> *, HttpRequest) {};
  123.     transferHandler = [](WebSocket<isServer> *) {};
  124.     messageHandler = [](WebSocket<isServer> *, char *, size_t, OpCode) {};
  125.     disconnectionHandler = [](WebSocket<isServer> *, int, char *, size_t) {};
  126.     pingHandler = pongHandler = [](WebSocket<isServer> *, char *, size_t) {};
  127.     errorHandler = [](errorType) {};
  128.     httpRequestHandler = [](HttpResponse *, HttpRequest, char *, size_t, size_t) {};
  129.     httpConnectionHandler = [](HttpSocket<isServer> *) {};
  130.     httpDisconnectionHandler = [](HttpSocket<isServer> *) {};
  131.     httpCancelledRequestHandler = [](HttpResponse *) {};
  132.     httpDataHandler = [](HttpResponse *, char *, size_t, size_t) {};
  133.  
  134.     this->extensionOptions |= CLIENT_NO_CONTEXT_TAKEOVER | SERVER_NO_CONTEXT_TAKEOVER;
  135. }
  136.  
  137. template <bool isServer>
  138. void Group<isServer>::stopListening() {
  139.     if (isServer) {
  140.         if (user) {
  141.             // todo: we should allow one group to listen to many ports!
  142.             uS::ListenSocket *listenSocket = (uS::ListenSocket *) user;
  143.  
  144.             if (listenSocket->timer) {
  145.                 listenSocket->timer->stop();
  146.                 listenSocket->timer->close();
  147.             }
  148.  
  149.             listenSocket->closeSocket<uS::ListenSocket>();
  150.  
  151.             // mark as stopped listening (extra care?)
  152.             user = nullptr;
  153.         }
  154.     }
  155.  
  156.     if (async) {
  157.         async->close();
  158.     }
  159. }
  160.  
  161. template <bool isServer>
  162. void Group<isServer>::onConnection(std::function<void (WebSocket<isServer> *, HttpRequest)> handler) {
  163.     connectionHandler = handler;
  164. }
  165.  
  166. template <bool isServer>
  167. void Group<isServer>::onTransfer(std::function<void (WebSocket<isServer> *)> handler) {
  168.     transferHandler = handler;
  169. }
  170.  
  171. template <bool isServer>
  172. void Group<isServer>::onMessage(std::function<void (WebSocket<isServer> *, char *, size_t, OpCode)> handler) {
  173.     messageHandler = handler;
  174. }
  175.  
  176. template <bool isServer>
  177. void Group<isServer>::onDisconnection(std::function<void (WebSocket<isServer> *, int, char *, size_t)> handler) {
  178.     disconnectionHandler = handler;
  179. }
  180.  
  181. template <bool isServer>
  182. void Group<isServer>::onPing(std::function<void (WebSocket<isServer> *, char *, size_t)> handler) {
  183.     pingHandler = handler;
  184. }
  185.  
  186. template <bool isServer>
  187. void Group<isServer>::onPong(std::function<void (WebSocket<isServer> *, char *, size_t)> handler) {
  188.     pongHandler = handler;
  189. }
  190.  
  191. template <bool isServer>
  192. void Group<isServer>::onError(std::function<void (typename Group::errorType)> handler) {
  193.     errorHandler = handler;
  194. }
  195.  
  196. template <bool isServer>
  197. void Group<isServer>::onHttpConnection(std::function<void (HttpSocket<isServer> *)> handler) {
  198.     httpConnectionHandler = handler;
  199. }
  200.  
  201. template <bool isServer>
  202. void Group<isServer>::onHttpRequest(std::function<void (HttpResponse *, HttpRequest, char *, size_t, size_t)> handler) {
  203.     httpRequestHandler = handler;
  204. }
  205.  
  206. template <bool isServer>
  207. void Group<isServer>::onHttpData(std::function<void(HttpResponse *, char *, size_t, size_t)> handler) {
  208.     httpDataHandler = handler;
  209. }
  210.  
  211. template <bool isServer>
  212. void Group<isServer>::onHttpDisconnection(std::function<void (HttpSocket<isServer> *)> handler) {
  213.     httpDisconnectionHandler = handler;
  214. }
  215.  
  216. template <bool isServer>
  217. void Group<isServer>::onCancelledHttpRequest(std::function<void (HttpResponse *)> handler) {
  218.     httpCancelledRequestHandler = handler;
  219. }
  220.  
  221. template <bool isServer>
  222. void Group<isServer>::onHttpUpgrade(std::function<void(HttpSocket<isServer> *, HttpRequest)> handler) {
  223.     httpUpgradeHandler = handler;
  224. }
  225.  
  226. template <bool isServer>
  227. void Group<isServer>::broadcast(const char *message, size_t length, OpCode opCode) {
  228.  
  229. #ifdef UWS_THREADSAFE
  230.     std::lock_guard<std::recursive_mutex> lockGuard(*asyncMutex);
  231. #endif
  232.  
  233.     typename WebSocket<isServer>::PreparedMessage *preparedMessage = WebSocket<isServer>::prepareMessage((char *) message, length, opCode, false);
  234.     forEach([preparedMessage](uWS::WebSocket<isServer> *ws) {
  235.         ws->sendPrepared(preparedMessage);
  236.     });
  237.     WebSocket<isServer>::finalizeMessage(preparedMessage);
  238. }
  239.  
  240. template <bool isServer>
  241. void Group<isServer>::terminate() {
  242.     forEach([](uWS::WebSocket<isServer> *ws) {
  243.         ws->terminate();
  244.     });
  245.     stopListening();
  246. }
  247.  
  248. template <bool isServer>
  249. void Group<isServer>::close(int code, char *message, size_t length) {
  250.     forEach([code, message, length](uWS::WebSocket<isServer> *ws) {
  251.         ws->close(code, message, length);
  252.     });
  253.     stopListening();
  254.     if (timer) {
  255.         timer->stop();
  256.         timer->close();
  257.     }
  258. }
  259.  
  260. template struct Group<true>;
  261. template struct Group<false>;
  262.  
  263. }
  264.  
downloadGroup.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