SNode.C
Loading...
Searching...
No Matches
Receiver.h
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef WEB_WEBSOCKET_RECEVIER_H
21#define WEB_WEBSOCKET_RECEVIER_H
22
23namespace core::socket::stream {
24
25 class SocketConnection;
26
27}
28
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
30
31#include <cstddef>
32#include <cstdint>
33
34#endif /* DOXYGEN_SHOULD_SKIP_THIS */
35
36#ifndef MAX_PAYLOAD_JUNK_LEN
37#define MAX_PAYLOAD_JUNK_LEN 16384
38#endif
39
40namespace web::websocket {
41
42 class Receiver {
43 public:
44 Receiver(core::socket::stream::SocketConnection* socketConnection);
45
46 Receiver(const Receiver&) = delete;
47 Receiver& operator=(const Receiver&) = delete;
48
49 virtual ~Receiver();
50
51 std::size_t receive();
52
53 private:
54 union MaskingKey {
55 uint32_t key;
56 char keyAsArray[4];
57 };
58
59 std::size_t readOpcode();
60 std::size_t readLength();
61 std::size_t readELength();
62 std::size_t readMaskingKey();
63 std::size_t readPayload();
64
65 virtual void onMessageStart(int opCode) = 0;
66 virtual void onMessageData(const char* chunk, uint64_t chunkLen) = 0;
67 virtual void onMessageEnd() = 0;
68 virtual void onMessageError(uint16_t errnum) = 0;
69
70 std::size_t readFrameData(char* chunk, std::size_t chunkLen);
71
72 void reset();
73
74 // Parser state
76
77 bool fin = false;
78 bool continuation = false;
79 bool masked = false;
80
81 uint8_t opCode = 0;
82
83 uint8_t elengthNumBytes = 0;
85
86 uint64_t payLoadNumBytes = 0;
87 uint64_t payLoadNumBytesLeft = 0;
88
89 uint32_t maskingKey = 0;
91 uint8_t maskingKeyNumBytes = 4;
93
94 uint16_t errorState = 0;
95
96 char elengthChunk[8]{};
97 char maskingKeyChunk[4]{};
99
100 core::socket::stream::SocketConnection* socketConnection = nullptr;
101 };
102
103} // namespace web::websocket
104
105#endif // WEB_WEBSOCKET_RECEVIER_H
106
107// |Opcode | Meaning | Reference |
108// -+--------+-------------------------------------+-----------|
109// | 0 | Continuation Frame | RFC XXXX |
110// -+--------+-------------------------------------+-----------|
111// | 1 | Text Frame | RFC XXXX |
112// -+--------+-------------------------------------+-----------|
113// | 2 | Binary Frame | RFC XXXX |
114// -+--------+-------------------------------------+-----------|
115// | 8 | Connection Close Frame | RFC XXXX |
116// -+--------+-------------------------------------+-----------|
117// | 9 | Ping Frame | RFC XXXX |
118// -+--------+-------------------------------------+-----------|
119// | 10 | Pong Frame | RFC XXXX |
120// -+--------+-------------------------------------+-----------|
#define MAX_PAYLOAD_JUNK_LEN
Definition Receiver.h:37
virtual void onMessageEnd()=0
std::size_t receive()
Definition Receiver.cpp:43
virtual void onMessageStart(int opCode)=0
std::size_t readLength()
Definition Receiver.cpp:106
std::size_t readMaskingKey()
Definition Receiver.cpp:176
Receiver & operator=(const Receiver &)=delete
std::size_t readELength()
Definition Receiver.cpp:144
char payloadChunk[16384]
Definition Receiver.h:98
std::size_t readFrameData(char *chunk, std::size_t chunkLen)
Definition Receiver.cpp:257
virtual void onMessageData(const char *chunk, uint64_t chunkLen)=0
Receiver(core::socket::stream::SocketConnection *socketConnection)
Definition Receiver.cpp:36
virtual void onMessageError(uint16_t errnum)=0
core::socket::stream::SocketConnection * socketConnection
Definition Receiver.h:100
std::size_t readPayload()
Definition Receiver.cpp:201
Receiver(const Receiver &)=delete
std::size_t readOpcode()
Definition Receiver.cpp:80
MaskingKey maskingKeyAsArray
Definition Receiver.h:90
uint8_t maskingKeyNumBytesLeft
Definition Receiver.h:92
uint64_t payLoadNumBytesLeft
Definition Receiver.h:87