Coverage Report

Created: 2023-06-06 06:17

/src/uWebSockets/src/LoopData.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_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
1.65k
    LoopData() {
50
1.65k
        updateDate();
51
1.65k
    }
52
53
1.65k
    ~LoopData() {
54
        /* If we have had App.ws called with compression we need to clear this */
55
1.65k
        if (zlibContext) {
56
0
            delete zlibContext;
57
0
            delete inflationStream;
58
0
            delete deflationStream;
59
0
        }
60
1.65k
        delete [] corkBuffer;
61
1.65k
    }
62
63
2.04M
    void updateDate() {
64
2.04M
        time_t now = time(0);
65
2.04M
        struct tm tstruct = {};
66
#ifdef _WIN32
67
        /* Micro, fucking soft never follows spec. */
68
        gmtime_s(&tstruct, &now);
69
#else
70
2.04M
        gmtime_r(&now, &tstruct);
71
2.04M
#endif
72
2.04M
        static const char wday_name[][4] = {
73
2.04M
            "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
74
2.04M
        };
75
2.04M
        static const char mon_name[][4] = {
76
2.04M
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77
2.04M
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
78
2.04M
        };
79
2.04M
        snprintf(date, 32, "%.3s, %.2u %.3s %.4u %.2u:%.2u:%.2u GMT",
80
2.04M
            wday_name[tstruct.tm_wday],
81
2.04M
            tstruct.tm_mday % 99,
82
2.04M
            mon_name[tstruct.tm_mon],
83
2.04M
            (1900 + tstruct.tm_year) % 9999,
84
2.04M
            tstruct.tm_hour % 99,
85
2.04M
            tstruct.tm_min % 99,
86
2.04M
            tstruct.tm_sec % 99);
87
2.04M
    }
88
89
    char date[32];
90
91
    /* Be silent */
92
    bool noMark = false;
93
94
    /* Good 16k for SSL perf. */
95
    static const unsigned int CORK_BUFFER_SIZE = 16 * 1024;
96
97
    /* Cork data */
98
    char *corkBuffer = new char[CORK_BUFFER_SIZE];
99
    unsigned int corkOffset = 0;
100
    void *corkedSocket = nullptr;
101
102
    /* Per message deflate data */
103
    ZlibContext *zlibContext = nullptr;
104
    InflationStream *inflationStream = nullptr;
105
    DeflationStream *deflationStream = nullptr;
106
107
    us_timer_t *dateTimer;
108
};
109
110
}
111
112
#endif // UWS_LOOPDATA_H