Coverage Report

Created: 2023-06-06 06:17

/src/uWebSockets/src/AsyncSocketData.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Authored by Alex Hultman, 2018-2021.
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_ASYNCSOCKETDATA_H
19
#define UWS_ASYNCSOCKETDATA_H
20
21
#include <string>
22
23
namespace uWS {
24
25
struct BackPressure {
26
    std::string buffer;
27
    unsigned int pendingRemoval = 0;
28
216k
    BackPressure(BackPressure &&other) {
29
216k
        buffer = std::move(other.buffer);
30
216k
        pendingRemoval = other.pendingRemoval;
31
216k
    }
32
1.50M
    BackPressure() = default;
33
228k
    void append(const char *data, size_t length) {
34
228k
        buffer.append(data, length);
35
228k
    }
36
121k
    void erase(unsigned int length) {
37
121k
        pendingRemoval += length;
38
        /* Always erase a minimum of 1/32th the current backpressure */
39
121k
        if (pendingRemoval > (buffer.length() >> 5)) {
40
83.6k
            buffer.erase(0, pendingRemoval);
41
83.6k
            pendingRemoval = 0;
42
83.6k
        }
43
121k
    }
44
3.19M
    size_t length() {
45
3.19M
        return buffer.length() - pendingRemoval;
46
3.19M
    }
47
5.03k
    void clear() {
48
5.03k
        pendingRemoval = 0;
49
5.03k
        buffer.clear();
50
5.03k
    }
51
0
    void reserve(size_t length) {
52
0
        buffer.reserve(length + pendingRemoval);
53
0
    }
54
46.1k
    void resize(size_t length) {
55
46.1k
        buffer.resize(length + pendingRemoval);
56
46.1k
    }
57
218k
    const char *data() {
58
218k
        return buffer.data() + pendingRemoval;
59
218k
    }
60
0
    size_t size() {
61
0
        return length();
62
0
    }
63
    /* The total length, incuding pending removal */
64
201k
    size_t totalLength() {
65
201k
        return buffer.length();
66
201k
    }
67
};
68
69
/* Depending on how we want AsyncSocket to function, this will need to change */
70
71
template <bool SSL>
72
struct AsyncSocketData {
73
    /* This will do for now */
74
    BackPressure buffer;
75
76
    /* Allow move constructing us */
77
108k
    AsyncSocketData(BackPressure &&backpressure) : buffer(std::move(backpressure)) {
78
79
108k
    }
80
81
    /* Or emppty */
82
1.50M
    AsyncSocketData() = default;
83
};
84
85
}
86
87
#endif // UWS_ASYNCSOCKETDATA_H