chronoxor / CppServer

Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
MIT License
1.44k stars 284 forks source link

not handle websocket close status code #86

Open 315567599 opened 1 year ago

315567599 commented 1 year ago

void WebSocket::PrepareSendFrame(uint8_t opcode, bool mask, const void* buffer, size_t size, int status) { // Clear the previous WebSocket send buffer _ws_send_buffer.clear();

// Append WebSocket frame opcode
_ws_send_buffer.push_back(opcode);

// Append WebSocket frame size
if (size <= 125)
    _ws_send_buffer.push_back((size & 0xFF) | (mask ? 0x80 : 0));
else if (size <= 65535)
{
    _ws_send_buffer.push_back(126 | (mask ? 0x80 : 0));
    _ws_send_buffer.push_back((size >> 8) & 0xFF);
    _ws_send_buffer.push_back(size & 0xFF);
}
else
{
    _ws_send_buffer.push_back(127 | (mask ? 0x80 : 0));
    for (int i = 7; i >= 0; --i)
        _ws_send_buffer.push_back((size >> (8 * i)) & 0xFF);
}

if (mask)
{
    // Append WebSocket frame mask
    _ws_send_buffer.push_back(_ws_send_mask[0]);
    _ws_send_buffer.push_back(_ws_send_mask[1]);
    _ws_send_buffer.push_back(_ws_send_mask[2]);
    _ws_send_buffer.push_back(_ws_send_mask[3]);
}

// Resize WebSocket frame buffer
size_t offset = _ws_send_buffer.size();
_ws_send_buffer.resize(offset + size);

// Mask WebSocket frame content
const uint8_t* data = (const uint8_t*)buffer;
for (size_t i = 0; i < size; ++i)
    _ws_send_buffer[offset + i] = data[i] ^ _ws_send_mask[i % 4];

}