Coverage Report

Created: 2023-06-06 06:17

/src/uWebSockets/src/WebSocketContextData.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_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> *)> drainHandler = nullptr;
63
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view, int, int)> subscriptionHandler = nullptr;
64
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, int, std::string_view)> closeHandler = nullptr;
65
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view)> pingHandler = nullptr;
66
    MoveOnlyFunction<void(WebSocket<SSL, true, USERDATA> *, std::string_view)> pongHandler = nullptr;
67
68
    /* Settings for this context */
69
    size_t maxPayloadLength = 0;
70
71
    /* We do need these for async upgrade */
72
    CompressOptions compression;
73
74
    /* There needs to be a maxBackpressure which will force close everything over that limit */
75
    size_t maxBackpressure = 0;
76
    bool closeOnBackpressureLimit;
77
    bool resetIdleTimeoutOnSend;
78
    bool sendPingsAutomatically;
79
    unsigned short maxLifetime;
80
81
    /* These are calculated on creation */
82
    std::pair<unsigned short, unsigned short> idleTimeoutComponents;
83
84
    /* This is run once on start-up */
85
11.0k
    void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
86
11.0k
        unsigned short margin = 4;
87
        /* 4, 8 or 16 seconds margin based on idleTimeout */
88
11.0k
        while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {
89
0
            margin = (unsigned short) (margin << 1);
90
0
        }
91
11.0k
        idleTimeoutComponents = {
92
11.0k
            idleTimeout - (sendPingsAutomatically ? margin : 0), /* reduce normal idleTimeout if it is extended by ping-timeout */
93
11.0k
            margin /* ping-timeout - also used for end() timeout */
94
11.0k
        };
95
11.0k
    }
96
97
11.0k
    ~WebSocketContextData() {
98
99
11.0k
    }
100
101
11.0k
    WebSocketContextData(TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree) : topicTree(topicTree) {
102
103
11.0k
    }
104
};
105
106
}
107
108
#endif // UWS_WEBSOCKETCONTEXTDATA_H