/src/uWebSockets/src/LoopData.h
Line | Count | Source |
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_LOOPDATA_H |
19 | | #define UWS_LOOPDATA_H |
20 | | |
21 | | #include <thread> |
22 | | #include <functional> |
23 | | #include <vector> |
24 | | #include <mutex> |
25 | | #include <map> |
26 | | #include <ctime> |
27 | | #include <cstdint> |
28 | | |
29 | | #include "PerMessageDeflate.h" |
30 | | #include "MoveOnlyFunction.h" |
31 | | |
32 | | struct us_timer_t; |
33 | | |
34 | | namespace uWS { |
35 | | |
36 | | struct Loop; |
37 | | |
38 | | struct alignas(16) LoopData { |
39 | | friend struct Loop; |
40 | | private: |
41 | | std::mutex deferMutex; |
42 | | int currentDeferQueue = 0; |
43 | | std::vector<MoveOnlyFunction<void()>> deferQueues[2]; |
44 | | |
45 | | /* Map from void ptr to handler */ |
46 | | std::map<void *, MoveOnlyFunction<void(Loop *)>> postHandlers, preHandlers; |
47 | | |
48 | | public: |
49 | 7.19k | LoopData() { |
50 | 7.19k | updateDate(); |
51 | 7.19k | } |
52 | | |
53 | 7.19k | ~LoopData() { |
54 | | /* If we have had App.ws called with compression we need to clear this */ |
55 | 7.19k | if (zlibContext) { |
56 | 6.51k | delete zlibContext; |
57 | 6.51k | delete inflationStream; |
58 | 6.51k | delete deflationStream; |
59 | 6.51k | } |
60 | 7.19k | delete [] corkBuffer; |
61 | 7.19k | } |
62 | | |
63 | 2.52M | void updateDate() { |
64 | 2.52M | cacheTimepoint = time(0); |
65 | 2.52M | struct tm tstruct = {}; |
66 | | #ifdef _WIN32 |
67 | | /* Micro, fucking soft never follows spec. */ |
68 | | gmtime_s(&tstruct, &cacheTimepoint); |
69 | | #else |
70 | 2.52M | gmtime_r(&cacheTimepoint, &tstruct); |
71 | 2.52M | #endif |
72 | 2.52M | static const char wday_name[][4] = { |
73 | 2.52M | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
74 | 2.52M | }; |
75 | 2.52M | static const char mon_name[][4] = { |
76 | 2.52M | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
77 | 2.52M | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
78 | 2.52M | }; |
79 | 2.52M | snprintf(date, 32, "%.3s, %.2u %.3s %.4u %.2u:%.2u:%.2u GMT", |
80 | 2.52M | wday_name[tstruct.tm_wday], |
81 | 2.52M | tstruct.tm_mday % 99, |
82 | 2.52M | mon_name[tstruct.tm_mon], |
83 | 2.52M | (1900 + tstruct.tm_year) % 9999, |
84 | 2.52M | tstruct.tm_hour % 99, |
85 | 2.52M | tstruct.tm_min % 99, |
86 | 2.52M | tstruct.tm_sec % 99); |
87 | 2.52M | } |
88 | | |
89 | | char date[32]; |
90 | | time_t cacheTimepoint = 0; |
91 | | |
92 | | /* Be silent */ |
93 | | bool noMark = false; |
94 | | |
95 | | /* Good 16k for SSL perf. */ |
96 | | static const unsigned int CORK_BUFFER_SIZE = 16 * 1024; |
97 | | |
98 | | /* Cork data */ |
99 | | char *corkBuffer = new char[CORK_BUFFER_SIZE]; |
100 | | unsigned int corkOffset = 0; |
101 | | void *corkedSocket = nullptr; |
102 | | |
103 | | /* Per message deflate data */ |
104 | | ZlibContext *zlibContext = nullptr; |
105 | | InflationStream *inflationStream = nullptr; |
106 | | DeflationStream *deflationStream = nullptr; |
107 | | |
108 | | us_timer_t *dateTimer; |
109 | | }; |
110 | | |
111 | | } |
112 | | |
113 | | #endif // UWS_LOOPDATA_H |