BVB Source Codes

uWebSockets Show WebSocket.h Source code

Return Download uWebSockets: download WebSocket.h Source code - Download uWebSockets Source code - Type:.h
  1. #ifndef WEBSOCKET_UWS_H
  2. #define WEBSOCKET_UWS_H
  3.  
  4. #include "WebSocketProtocol.h"
  5. #include "Socket.h"
  6.  
  7. namespace uWS {
  8.  
  9. template <bool isServer>
  10. struct Group;
  11.  
  12. template <bool isServer>
  13. struct HttpSocket;
  14.  
  15. template <const bool isServer>
  16. struct WIN32_EXPORT WebSocket : uS::Socket, WebSocketState<isServer> {
  17. protected:
  18.     std::string fragmentBuffer;
  19.     enum CompressionStatus : char {
  20.         DISABLED,
  21.         ENABLED,
  22.         COMPRESSED_FRAME
  23.     } compressionStatus;
  24.     unsigned char controlTipLength = 0, hasOutstandingPong = false;
  25.  
  26.     WebSocket(bool perMessageDeflate, uS::Socket *socket) : uS::Socket(std::move(*socket)) {
  27.         compressionStatus = perMessageDeflate ? CompressionStatus::ENABLED : CompressionStatus::DISABLED;
  28.     }
  29.  
  30.     static uS::Socket *onData(uS::Socket *s, char *data, size_t length);
  31.     static void onEnd(uS::Socket *s);
  32.     using uS::Socket::closeSocket;
  33.  
  34.     static bool refusePayloadLength(uint64_t length, WebSocketState<isServer> *webSocketState) {
  35.         WebSocket<isServer> *webSocket = static_cast<WebSocket<isServer> *>(webSocketState);
  36.         return length > Group<isServer>::from(webSocket)->maxPayload;
  37.     }
  38.  
  39.     static bool setCompressed(WebSocketState<isServer> *webSocketState) {
  40.         WebSocket<isServer> *webSocket = static_cast<WebSocket<isServer> *>(webSocketState);
  41.  
  42.         if (webSocket->compressionStatus == WebSocket<isServer>::CompressionStatus::ENABLED) {
  43.             webSocket->compressionStatus = WebSocket<isServer>::CompressionStatus::COMPRESSED_FRAME;
  44.             return true;
  45.         } else {
  46.             return false;
  47.         }
  48.     }
  49.  
  50.     static void forceClose(WebSocketState<isServer> *webSocketState) {
  51.         WebSocket<isServer> *webSocket = static_cast<WebSocket<isServer> *>(webSocketState);
  52.         webSocket->terminate();
  53.     }
  54.  
  55.     static bool handleFragment(char *data, size_t length, unsigned int remainingBytes, int opCode, bool fin, WebSocketState<isServer> *webSocketState);
  56.  
  57. public:
  58.     struct PreparedMessage {
  59.         char *buffer;
  60.         size_t length;
  61.         int references;
  62.         void(*callback)(void *webSocket, void *data, bool cancelled, void *reserved);
  63.     };
  64.  
  65.     // Not thread safe
  66.     void sendPrepared(PreparedMessage *preparedMessage, void *callbackData = nullptr);
  67.     static void finalizeMessage(PreparedMessage *preparedMessage);
  68.     void close(int code = 1000, const char *message = nullptr, size_t length = 0);
  69.     void transfer(Group<isServer> *group);
  70.  
  71.     // Thread safe
  72.     void terminate();
  73.     void ping(const char *message) {send(message, OpCode::PING);}
  74.     void send(const char *message, OpCode opCode = OpCode::TEXT) {send(message, strlen(message), opCode);}
  75.     void send(const char *message, size_t length, OpCode opCode, void(*callback)(WebSocket<isServer> *webSocket, void *data, bool cancelled, void *reserved) = nullptr, void *callbackData = nullptr);
  76.     static PreparedMessage *prepareMessage(char *data, size_t length, OpCode opCode, bool compressed, void(*callback)(WebSocket<isServer> *webSocket, void *data, bool cancelled, void *reserved) = nullptr);
  77.     static PreparedMessage *prepareMessageBatch(std::vector<std::string> &messages, std::vector<int> &excludedMessages,
  78.                                                 OpCode opCode, bool compressed, void(*callback)(WebSocket<isServer> *webSocket, void *data, bool cancelled, void *reserved) = nullptr);
  79.  
  80.     friend struct Hub;
  81.     friend struct Group<isServer>;
  82.     friend struct HttpSocket<isServer>;
  83.     friend struct uS::Socket;
  84.     friend class WebSocketProtocol<isServer, WebSocket<isServer>>;
  85. };
  86.  
  87. }
  88.  
  89. #endif // WEBSOCKET_UWS_H
  90.  
downloadWebSocket.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