Coverage Report

Created: 2026-03-02 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/uWebSockets/src/WebSocketContextData.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_WEBSOCKETCONTEXTDATA_H
19
#define UWS_WEBSOCKETCONTEXTDATA_H
20
21
#include "Loop.h"
22
#include "AsyncSocket.h"
23
24
#include "MoveOnlyFunction.h"
25
#include <string_view>
26
#include <vector>
27
28
#include "WebSocketProtocol.h"
29
#include "TopicTree.h"
30
#include "WebSocketData.h"
31
32
namespace uWS {
33
34
/* Type queued up when publishing */
35
struct TopicTreeMessage {
36
    std::string message;
37
    /*OpCode*/ int opCode;
38
    bool compress;
39
};
40
struct TopicTreeBigMessage {
41
    std::string_view message;
42
    /*OpCode*/ int opCode;
43
    bool compress;
44
};
45
46
template <bool, bool, typename> struct WebSocket;
47
48
/* todo: this looks identical to WebSocketBehavior, why not just std::move that entire thing in? */
49
50
template <bool SSL, typename USERDATA>
51
struct WebSocketContextData {
52
private:
53
54
public:
55
56
    /* This one points to the App's shared topicTree */
57
    TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree;
58
59
    /* The callbacks for this context */
60
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *)> openHandler = nullptr;
61
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, OpCode)> messageHandler = nullptr;
62
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, OpCode)> droppedHandler = nullptr;
63
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *)> drainHandler = nullptr;
64
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, int, int)> subscriptionHandler = nullptr;
65
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, int, std::string_view)> closeHandler = nullptr;
66
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view)> pingHandler = nullptr;
67
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view)> pongHandler = nullptr;
68
69
    /* Settings for this context */
70
    size_t maxPayloadLength = 0;
71
72
    /* We do need these for async upgrade */
73
    CompressOptions compression;
74
75
    /* There needs to be a maxBackpressure which will force close everything over that limit */
76
    size_t maxBackpressure = 0;
77
    bool closeOnBackpressureLimit;
78
    bool resetIdleTimeoutOnSend;
79
    bool sendPingsAutomatically;
80
    unsigned short maxLifetime;
81
82
    /* These are calculated on creation */
83
    std::pair<unsigned short, unsigned short> idleTimeoutComponents;
84
85
    /* This is run once on start-up */
86
33.8k
    void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
87
33.8k
        unsigned short margin = 4;
88
        /* 4, 8 or 16 seconds margin based on idleTimeout */
89
59.0k
        while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {
90
25.2k
            margin = (unsigned short) (margin << 1);
91
25.2k
        }
92
33.8k
        idleTimeoutComponents = {
93
33.8k
            idleTimeout - (sendPingsAutomatically ? margin : 0), /* reduce normal idleTimeout if it is extended by ping-timeout */
94
33.8k
            margin /* ping-timeout - also used for end() timeout */
95
33.8k
        };
96
33.8k
    }
EpollEchoServerPubSub.cpp:uWS::WebSocketContextData<true, test()::PerSocketData>::calculateIdleTimeoutCompnents(unsigned short)
Line
Count
Source
86
5.83k
    void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
87
5.83k
        unsigned short margin = 4;
88
        /* 4, 8 or 16 seconds margin based on idleTimeout */
89
17.5k
        while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {
90
11.6k
            margin = (unsigned short) (margin << 1);
91
11.6k
        }
92
5.83k
        idleTimeoutComponents = {
93
5.83k
            idleTimeout - (sendPingsAutomatically ? margin : 0), /* reduce normal idleTimeout if it is extended by ping-timeout */
94
5.83k
            margin /* ping-timeout - also used for end() timeout */
95
5.83k
        };
96
5.83k
    }
EpollHelloWorld.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::calculateIdleTimeoutCompnents(unsigned short)
Line
Count
Source
86
13.5k
    void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
87
13.5k
        unsigned short margin = 4;
88
        /* 4, 8 or 16 seconds margin based on idleTimeout */
89
27.1k
        while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {
90
13.5k
            margin = (unsigned short) (margin << 1);
91
13.5k
        }
92
13.5k
        idleTimeoutComponents = {
93
13.5k
            idleTimeout - (sendPingsAutomatically ? margin : 0), /* reduce normal idleTimeout if it is extended by ping-timeout */
94
13.5k
            margin /* ping-timeout - also used for end() timeout */
95
13.5k
        };
96
13.5k
    }
EpollEchoServer.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::calculateIdleTimeoutCompnents(unsigned short)
Line
Count
Source
86
14.3k
    void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
87
14.3k
        unsigned short margin = 4;
88
        /* 4, 8 or 16 seconds margin based on idleTimeout */
89
14.3k
        while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {
90
0
            margin = (unsigned short) (margin << 1);
91
0
        }
92
14.3k
        idleTimeoutComponents = {
93
14.3k
            idleTimeout - (sendPingsAutomatically ? margin : 0), /* reduce normal idleTimeout if it is extended by ping-timeout */
94
14.3k
            margin /* ping-timeout - also used for end() timeout */
95
14.3k
        };
96
14.3k
    }
97
98
33.8k
    ~WebSocketContextData() {
99
100
33.8k
    }
EpollEchoServerPubSub.cpp:uWS::WebSocketContextData<true, test()::PerSocketData>::~WebSocketContextData()
Line
Count
Source
98
5.83k
    ~WebSocketContextData() {
99
100
5.83k
    }
EpollHelloWorld.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::~WebSocketContextData()
Line
Count
Source
98
13.5k
    ~WebSocketContextData() {
99
100
13.5k
    }
EpollEchoServer.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::~WebSocketContextData()
Line
Count
Source
98
14.3k
    ~WebSocketContextData() {
99
100
14.3k
    }
101
102
33.8k
    WebSocketContextData(TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree) : topicTree(topicTree) {
103
104
33.8k
    }
EpollEchoServerPubSub.cpp:uWS::WebSocketContextData<true, test()::PerSocketData>::WebSocketContextData(uWS::TopicTree<uWS::TopicTreeMessage, uWS::TopicTreeBigMessage>*)
Line
Count
Source
102
5.83k
    WebSocketContextData(TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree) : topicTree(topicTree) {
103
104
5.83k
    }
EpollHelloWorld.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::WebSocketContextData(uWS::TopicTree<uWS::TopicTreeMessage, uWS::TopicTreeBigMessage>*)
Line
Count
Source
102
13.5k
    WebSocketContextData(TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree) : topicTree(topicTree) {
103
104
13.5k
    }
EpollEchoServer.cpp:uWS::WebSocketContextData<false, test()::PerSocketData>::WebSocketContextData(uWS::TopicTree<uWS::TopicTreeMessage, uWS::TopicTreeBigMessage>*)
Line
Count
Source
102
14.3k
    WebSocketContextData(TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree) : topicTree(topicTree) {
103
104
14.3k
    }
105
};
106
107
}
108
109
#endif // UWS_WEBSOCKETCONTEXTDATA_H