Coverage Report

Created: 2025-08-28 06:18

/src/uWebSockets/src/AsyncSocketData.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Authored by Alex Hultman, 2018-2025.
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
664k
    BackPressure(BackPressure &&other) {
29
664k
        buffer = std::move(other.buffer);
30
664k
        pendingRemoval = other.pendingRemoval;
31
664k
    }
32
5.29M
    BackPressure() = default;
33
1.23M
    void append(const char *data, size_t length) {
34
1.23M
        buffer.append(data, length);
35
1.23M
    }
36
896k
    void erase(unsigned int length) {
37
896k
        pendingRemoval += length;
38
        /* Always erase a minimum of 1/32th the current backpressure */
39
896k
        if (pendingRemoval > (buffer.length() >> 5)) {
40
534k
            std::string(buffer.begin() + pendingRemoval, buffer.end()).swap(buffer);
41
534k
            pendingRemoval = 0;
42
534k
        }
43
896k
    }
44
14.3M
    size_t length() {
45
14.3M
        return buffer.length() - pendingRemoval;
46
14.3M
    }
47
    /* Only used in AsyncSocket::write - what about replacing it with the other functions like erase(length())? */
48
28.7k
    void clear() {
49
28.7k
        pendingRemoval = 0;
50
28.7k
        buffer.clear();
51
28.7k
        buffer.shrink_to_fit();
52
28.7k
    }
53
    /* Only used by AsyncSocket::write (optionally) before append */
54
0
    void reserve(size_t length) {
55
0
        buffer.reserve(length + pendingRemoval);
56
0
    }
57
    /* Only used by getSendBuffer as last resort */
58
212k
    void resize(size_t length) {
59
212k
        buffer.resize(length + pendingRemoval);
60
212k
    }
61
1.34M
    const char *data() {
62
1.34M
        return buffer.data() + pendingRemoval;
63
1.34M
    }
64
    /* The total length, incuding pending removal */
65
795k
    size_t totalLength() {
66
795k
        return buffer.length();
67
795k
    }
68
};
69
70
/* Depending on how we want AsyncSocket to function, this will need to change */
71
72
template <bool SSL>
73
struct AsyncSocketData {
74
    /* This will do for now */
75
    BackPressure buffer;
76
77
    /* Allow move constructing us */
78
332k
    AsyncSocketData(BackPressure &&backpressure) : buffer(std::move(backpressure)) {
79
80
332k
    }
81
82
    /* Or emppty */
83
5.29M
    AsyncSocketData() = default;
uWS::AsyncSocketData<true>::AsyncSocketData()
Line
Count
Source
83
1.32M
    AsyncSocketData() = default;
uWS::AsyncSocketData<false>::AsyncSocketData()
Line
Count
Source
83
3.96M
    AsyncSocketData() = default;
84
};
85
86
}
87
88
#endif // UWS_ASYNCSOCKETDATA_H