/src/uWebSockets/src/WebSocketData.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Authored by Alex Hultman, 2018-2020. |
3 | | * Intellectual property of third-party. |
4 | | |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #ifndef UWS_WEBSOCKETDATA_H |
19 | | #define UWS_WEBSOCKETDATA_H |
20 | | |
21 | | #include "WebSocketProtocol.h" |
22 | | #include "AsyncSocketData.h" |
23 | | #include "PerMessageDeflate.h" |
24 | | #include "TopicTree.h" |
25 | | |
26 | | #include <string> |
27 | | |
28 | | namespace uWS { |
29 | | |
30 | | struct WebSocketData : AsyncSocketData<false>, WebSocketState<true> { |
31 | | /* This guy has a lot of friends - why? */ |
32 | | template <bool, bool, typename> friend struct WebSocketContext; |
33 | | template <bool, typename> friend struct WebSocketContextData; |
34 | | template <bool, bool, typename> friend struct WebSocket; |
35 | | template <bool> friend struct HttpContext; |
36 | | private: |
37 | | std::string fragmentBuffer; |
38 | | unsigned int controlTipLength = 0; |
39 | | bool isShuttingDown = 0; |
40 | | bool hasTimedOut = false; |
41 | | enum CompressionStatus : char { |
42 | | DISABLED, |
43 | | ENABLED, |
44 | | COMPRESSED_FRAME |
45 | | } compressionStatus; |
46 | | |
47 | | /* We might have a dedicated compressor */ |
48 | | DeflationStream *deflationStream = nullptr; |
49 | | /* And / or a dedicated decompressor */ |
50 | | InflationStream *inflationStream = nullptr; |
51 | | |
52 | | /* We could be a subscriber */ |
53 | | Subscriber *subscriber = nullptr; |
54 | | public: |
55 | 215k | WebSocketData(bool perMessageDeflate, CompressOptions compressOptions, BackPressure &&backpressure) : AsyncSocketData<false>(std::move(backpressure)), WebSocketState<true>() { |
56 | 215k | compressionStatus = perMessageDeflate ? ENABLED : DISABLED; |
57 | | |
58 | | /* Initialize the dedicated sliding window(s) */ |
59 | 215k | if (perMessageDeflate) { |
60 | 3.88k | if ((compressOptions & CompressOptions::_COMPRESSOR_MASK) != CompressOptions::SHARED_COMPRESSOR) { |
61 | 2.31k | deflationStream = new DeflationStream(compressOptions); |
62 | 2.31k | } |
63 | 3.88k | if ((compressOptions & CompressOptions::_DECOMPRESSOR_MASK) != CompressOptions::SHARED_DECOMPRESSOR) { |
64 | 0 | inflationStream = new InflationStream(compressOptions); |
65 | 0 | } |
66 | 3.88k | } |
67 | 215k | } |
68 | | |
69 | 215k | ~WebSocketData() { |
70 | 215k | if (deflationStream) { |
71 | 2.31k | delete deflationStream; |
72 | 2.31k | } |
73 | | |
74 | 215k | if (inflationStream) { |
75 | 0 | delete inflationStream; |
76 | 0 | } |
77 | | |
78 | 215k | if (subscriber) { |
79 | 0 | delete subscriber; |
80 | 0 | } |
81 | 215k | } |
82 | | }; |
83 | | |
84 | | } |
85 | | |
86 | | #endif // UWS_WEBSOCKETDATA_H |