1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00

Update websocket.cpp

This commit is contained in:
Victor Stewart 2019-11-30 21:46:10 -04:00 committed by GitHub
parent 5a7f6f3cb7
commit 654a2ebcd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -617,8 +617,13 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
*/ */
uint8_t opcode, masking_key[4]; uint8_t opcode, masking_key[4];
U_DUMP("len = %lu", len);
// 0xffff == 65535
uint32_t header_length = (len > 125U ? 2U : 0) + (len > 0xffff ? 8U : 0); uint32_t header_length = (len > 125U ? 2U : 0) + (len > 0xffff ? 8U : 0);
U_DUMP("header_length = %lu", header_length);
if (isServer) header_length += 2U; if (isServer) header_length += 2U;
else else
{ {
@ -628,8 +633,12 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
uint32_t ncount = header_length + len; uint32_t ncount = header_length + len;
UString tmp(ncount), compressed; // 1 MB
unsigned char* header = (unsigned char*)tmp.data(); static UString buffer(1048576U);
buffer.setEmpty();
buffer.reserve(ncount);
unsigned char* header = (unsigned char*)buffer.data();
switch (type) switch (type)
{ {
@ -643,6 +652,8 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
opcode = U_WS_OPCODE_TEXT; opcode = U_WS_OPCODE_TEXT;
# ifdef USE_LIBBROTLI # ifdef USE_LIBBROTLI
UString compressed;
if (compressed = UStringExt::brotli(data, len, (U_PARALLELIZATION_CHILD ? BROTLI_MAX_QUALITY : UHTTP::brotli_level_for_dynamic_content))) if (compressed = UStringExt::brotli(data, len, (U_PARALLELIZATION_CHILD ? BROTLI_MAX_QUALITY : UHTTP::brotli_level_for_dynamic_content)))
{ {
opcode = U_WS_OPCODE_BROTLI; opcode = U_WS_OPCODE_BROTLI;
@ -685,13 +696,13 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 4: case 4:
{ {
header[1] = 126; header[1] = 126;
u_put_unalignedp16(header+2, htons(len)); u_put_unalignedp16(header+2, htons((uint16_t)len));
break; break;
} }
case 12: case 12:
{ {
header[1] = 127; header[1] = 127;
u_put_unalignedp64(header+2, htonl(len)); u_put_unalignedp32(header+2, htonl(len));
break; break;
} }
// client + len < 125 // client + len < 125
@ -721,6 +732,7 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
default: break; // never reached default: break; // never reached
} }
switch (header_length) switch (header_length)
{ {
// server // server
@ -728,10 +740,7 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 4: case 4:
case 12: case 12:
{ {
for (uint32_t i = 0; i < len; ++i) memcpy(header + header_length, data, len);
{
header[2+i] = data[i];
}
break; break;
} }
// client // client
@ -739,9 +748,10 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 8: case 8:
case 16: case 16:
{ {
// we should SIMD this
for (uint32_t i = 0; i < len; ++i) for (uint32_t i = 0; i < len; ++i)
{ {
header[6+i] = (data[i] ^ masking_key[i % 4]) & 0xff; header[header_length + i] = (data[i] ^ masking_key[i % 4]) & 0xff;
} }
break; break;
} }