Skip to content

Commit e269cde

Browse files
committed
refactor: unmask websocket frame without reallocation
1 parent 913be66 commit e269cde

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
build
2+
projects/build*
3+
projects/qt-creator/*.user

src/server/protocol/WebSocket.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,21 @@ namespace HttpClient
112112
cur_pos += sizeof(uint32_t);
113113
}
114114

115-
const uint8_t align = (recv_size - cur_pos) % sizeof(uint32_t);
116-
117-
frame.reserve(recv_size - cur_pos + align);
115+
frame.reserve(recv_size - cur_pos);
118116

119117
frame.assign(buf.cbegin() + cur_pos, buf.cbegin() + recv_size);
120118

121119
if (is_mask_set) {
122-
if (align) {
123-
frame.insert(frame.cend(), align, 0);
124-
}
120+
const size_t aligned = frame.size() - (frame.size() % sizeof(mask));
125121

126-
uint32_t *addr = reinterpret_cast<uint32_t *>(frame.data() );
122+
uint32_t * const addr = reinterpret_cast<uint32_t *>(frame.data() );
127123

128-
for (size_t i = 0; i < frame.size() / sizeof(uint32_t); ++i) {
124+
for (size_t i = 0; i < aligned / sizeof(uint32_t); ++i) {
129125
addr[i] ^= mask;
130126
}
131127

132-
if (align) {
133-
frame.erase(frame.cend() - align, frame.cend() );
128+
for (size_t i = aligned; i < frame.size(); ++i) {
129+
frame[i] ^= reinterpret_cast<char *>(&mask)[i % sizeof(mask)];
134130
}
135131
}
136132

0 commit comments

Comments
 (0)