SNode.C
Loading...
Searching...
No Matches
web::websocket::Transmitter Class Referenceabstract

#include <Transmitter.h>

Inheritance diagram for web::websocket::Transmitter:
Collaboration diagram for web::websocket::Transmitter:

Public Member Functions

 Transmitter ()=delete
 
 Transmitter (const Transmitter &)=delete
 
Transmitteroperator= (const Transmitter &)=delete
 
virtual ~Transmitter ()
 

Protected Member Functions

 Transmitter (bool masking)
 
void sendMessage (uint8_t opCode, const char *message, std::size_t messageLength)
 
void sendMessageStart (uint8_t opCode, const char *message, std::size_t messageLength)
 
void sendMessageFrame (const char *message, std::size_t messageLength)
 
void sendMessageEnd (const char *message, std::size_t messageLength)
 
std::size_t getPayloadTotalSent () const
 

Protected Attributes

bool closeSent = false
 

Private Member Functions

void send (bool end, uint8_t opCode, const char *message, std::size_t messageLength)
 
void sendFrame (bool fin, uint8_t opCode, const char *payload, uint64_t payloadLength)
 
void sendFrameData (uint8_t data) const
 
void sendFrameData (uint16_t data) const
 
void sendFrameData (uint32_t data) const
 
void sendFrameData (uint64_t data) const
 
void sendFrameData (const char *frame, uint64_t frameLength) const
 
virtual void sendFrameChunk (const char *data, std::size_t dataLength) const =0
 

Private Attributes

std::random_device randomDevice
 
std::uniform_int_distribution< uint32_t > distribution {0, UINT32_MAX}
 
bool masking = false
 
std::size_t payloadTotalSent = 0
 

Detailed Description

Definition at line 55 of file Transmitter.h.

Constructor & Destructor Documentation

◆ Transmitter() [1/3]

web::websocket::Transmitter::Transmitter ( )
delete

◆ Transmitter() [2/3]

web::websocket::Transmitter::Transmitter ( const Transmitter )
delete

◆ ~Transmitter()

web::websocket::Transmitter::~Transmitter ( )
virtual

Definition at line 64 of file Transmitter.cpp.

64 {
65 }

◆ Transmitter() [3/3]

web::websocket::Transmitter::Transmitter ( bool  masking)
protected

Definition at line 60 of file Transmitter.cpp.

References masking.

Referenced by web::websocket::SubProtocolContext::SubProtocolContext().

Here is the caller graph for this function:

Member Function Documentation

◆ getPayloadTotalSent()

std::size_t web::websocket::Transmitter::getPayloadTotalSent ( ) const
protected

Definition at line 83 of file Transmitter.cpp.

83 {
84 return payloadTotalSent;
85 }

References payloadTotalSent.

Referenced by web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >::getPayloadTotalSent().

Here is the caller graph for this function:

◆ operator=()

Transmitter & web::websocket::Transmitter::operator= ( const Transmitter )
delete

◆ send()

void web::websocket::Transmitter::send ( bool  end,
uint8_t  opCode,
const char *  message,
std::size_t  messageLength 
)
private

Definition at line 87 of file Transmitter.cpp.

87 {
88 std::size_t messageOffset = 0;
89
90 do {
91 const std::size_t sendFrameLength =
92 (messageLength - messageOffset <= WSMAXFRAMEPAYLOADLENGTH) ? messageLength - messageOffset : WSMAXFRAMEPAYLOADLENGTH;
93
94 const bool fin = (sendFrameLength == messageLength - messageOffset) && end;
95
96 sendFrame(fin, opCode, message + messageOffset, sendFrameLength);
97
98 messageOffset += sendFrameLength;
99
100 opCode = 0; // continuation
101 } while (messageLength - messageOffset > 0);
102
103 payloadTotalSent += messageLength;
104 }
#define WSMAXFRAMEPAYLOADLENGTH
void sendFrame(bool fin, uint8_t opCode, const char *payload, uint64_t payloadLength)

References payloadTotalSent, and sendFrame().

Referenced by sendMessage(), sendMessageEnd(), sendMessageFrame(), and sendMessageStart().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFrame()

void web::websocket::Transmitter::sendFrame ( bool  fin,
uint8_t  opCode,
const char *  payload,
uint64_t  payloadLength 
)
private

Definition at line 106 of file Transmitter.cpp.

106 {
107 uint64_t length = 0;
108
109 if (payloadLength < 126) {
110 length = payloadLength;
111 } else if (payloadLength < 0x10000) {
112 length = 126;
113 } else {
114 length = 127;
115 }
116
117 char header[2];
118 header[0] = static_cast<char>((fin ? 0b10000000 : 0) | opCode);
119
120 header[1] = static_cast<char>((masking ? 0b10000000 : 0) | length);
121
122 sendFrameData(header, 2);
123
124 switch (length) {
125 case 126:
126 sendFrameData(static_cast<uint16_t>(payloadLength));
127 break;
128 case 127:
129 sendFrameData(payloadLength);
130 break;
131 }
132
133 union MaskingKey {
134 uint32_t keyAsValue;
135 char keyAsBytes[4];
136 };
137
138 MaskingKey maskingKeyAsArray = {.keyAsValue = distribution(randomDevice)};
139
140 if (payloadLength > 0) {
141 LOG(TRACE) << "WebSocket send: Frame data\n" << utils::hexDump(payload, payloadLength, 32, true);
142 }
143
144 if (masking) {
145 sendFrameData(maskingKeyAsArray.keyAsBytes, 4);
146
147 for (uint64_t i = 0; i < payloadLength; i++) {
148 *(const_cast<char*>(payload) + i) = static_cast<char>(*(payload + i) ^ *(maskingKeyAsArray.keyAsBytes + i % 4));
149 }
150 }
151
152 sendFrameData(payload, payloadLength);
153
154 if (masking) {
155 for (uint64_t i = 0; i < payloadLength; i++) {
156 *(const_cast<char*>(payload) + i) = static_cast<char>(*(payload + i) ^ *(maskingKeyAsArray.keyAsBytes + i % 4));
157 }
158 }
159 }
std::random_device randomDevice
Definition Transmitter.h:88
void sendFrameData(uint8_t data) const
std::uniform_int_distribution< uint32_t > distribution
Definition Transmitter.h:89
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:58

References distribution, utils::hexDump(), masking, randomDevice, sendFrameData(), sendFrameData(), and sendFrameData().

Referenced by send().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFrameChunk()

virtual void web::websocket::Transmitter::sendFrameChunk ( const char *  data,
std::size_t  dataLength 
) const
privatepure virtual

◆ sendFrameData() [1/5]

void web::websocket::Transmitter::sendFrameData ( const char *  frame,
uint64_t  frameLength 
) const
private

Definition at line 188 of file Transmitter.cpp.

188 {
189 if (!closeSent) {
190 uint64_t frameOffset = 0;
191
192 do {
193 const std::size_t sendChunkLen =
194 (frameLength - frameOffset <= SIZE_MAX) ? static_cast<std::size_t>(frameLength - frameOffset) : SIZE_MAX;
195
196 sendFrameChunk(frame + frameOffset, sendChunkLen);
197
198 frameOffset += sendChunkLen;
199 } while (frameLength - frameOffset > 0);
200 }
201 }
virtual void sendFrameChunk(const char *data, std::size_t dataLength) const =0

References closeSent, and sendFrameChunk().

Referenced by sendFrame(), sendFrameData(), sendFrameData(), sendFrameData(), and sendFrameData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFrameData() [2/5]

void web::websocket::Transmitter::sendFrameData ( uint16_t  data) const
private

Definition at line 167 of file Transmitter.cpp.

167 {
168 if (!closeSent) {
169 uint16_t sendData = htobe16(data);
170 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint16_t));
171 }
172 }

References closeSent, and sendFrameData().

Referenced by sendFrame().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFrameData() [3/5]

void web::websocket::Transmitter::sendFrameData ( uint32_t  data) const
private

Definition at line 174 of file Transmitter.cpp.

174 {
175 if (!closeSent) {
176 uint32_t sendData = htobe32(data);
177 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint32_t));
178 }
179 }

References closeSent, and sendFrameData().

Here is the call graph for this function:

◆ sendFrameData() [4/5]

void web::websocket::Transmitter::sendFrameData ( uint64_t  data) const
private

Definition at line 181 of file Transmitter.cpp.

181 {
182 if (!closeSent) {
183 uint64_t sendData = htobe64(data);
184 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint64_t));
185 }
186 }

References closeSent, and sendFrameData().

Referenced by sendFrame().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendFrameData() [5/5]

void web::websocket::Transmitter::sendFrameData ( uint8_t  data) const
private

Definition at line 161 of file Transmitter.cpp.

161 {
162 if (!closeSent) {
163 sendFrameData(reinterpret_cast<char*>(&data), sizeof(uint8_t));
164 }
165 }

References closeSent, and sendFrameData().

Here is the call graph for this function:

◆ sendMessage()

void web::websocket::Transmitter::sendMessage ( uint8_t  opCode,
const char *  message,
std::size_t  messageLength 
)
protected

Definition at line 67 of file Transmitter.cpp.

67 {
68 send(true, opCode, message, messageLength);
69 }
void send(bool end, uint8_t opCode, const char *message, std::size_t messageLength)

References send().

Referenced by web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >::sendMessage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendMessageEnd()

void web::websocket::Transmitter::sendMessageEnd ( const char *  message,
std::size_t  messageLength 
)
protected

Definition at line 79 of file Transmitter.cpp.

79 {
80 send(true, 0, message, messageLength);
81 }

References send().

Referenced by web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >::sendMessageEnd().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendMessageFrame()

void web::websocket::Transmitter::sendMessageFrame ( const char *  message,
std::size_t  messageLength 
)
protected

Definition at line 75 of file Transmitter.cpp.

75 {
76 send(false, 0, message, messageLength);
77 }

References send().

Referenced by web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >::sendMessageFrame().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendMessageStart()

void web::websocket::Transmitter::sendMessageStart ( uint8_t  opCode,
const char *  message,
std::size_t  messageLength 
)
protected

Definition at line 71 of file Transmitter.cpp.

71 {
72 send(false, opCode, message, messageLength);
73 }

References send().

Referenced by web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >::sendMessageStart().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ closeSent

bool web::websocket::Transmitter::closeSent = false
protected

◆ distribution

std::uniform_int_distribution<uint32_t> web::websocket::Transmitter::distribution {0, UINT32_MAX}
private

Definition at line 89 of file Transmitter.h.

89{0, UINT32_MAX};

Referenced by sendFrame().

◆ masking

bool web::websocket::Transmitter::masking = false
private

Definition at line 91 of file Transmitter.h.

Referenced by sendFrame(), and Transmitter().

◆ payloadTotalSent

std::size_t web::websocket::Transmitter::payloadTotalSent = 0
private

Definition at line 97 of file Transmitter.h.

Referenced by getPayloadTotalSent(), and send().

◆ randomDevice

std::random_device web::websocket::Transmitter::randomDevice
private

Definition at line 88 of file Transmitter.h.

Referenced by sendFrame().


The documentation for this class was generated from the following files: