Commit ba4698d4 authored by Martin Vítek's avatar Martin Vítek

Working WebSocket Tx

parent e8b36195
......@@ -261,6 +261,7 @@ namespace MagWebServer
return nullptr;
}
if (frame_1->PAYLOAD_LEN < 126)
{
payload_len = frame_1->PAYLOAD_LEN;
......@@ -280,16 +281,79 @@ namespace MagWebServer
payload_index = 10 + sizeof(masking_key);
}
std::string payload;
payload.reserve(payload_len);
for (uint64_t i=0; i<payload_len; ++i)
if (frame_1->OPCODE == TEXT_FRAME)
{
std::string payload;
payload.reserve(payload_len);
for (uint64_t i=0; i<payload_len; ++i)
{
payload.push_back(buffer[payload_index + i] ^ masking_key.bytes[i%4]);
}
console->info("WebSocket get: {:s}", payload);
//TODO: Do something with payload
return assemble_websocket_message("OK!");
}
else if (frame_1->OPCODE == PING_FRAME)
{
payload.push_back(buffer[payload_index + i] ^ masking_key.bytes[i%4]);
}
console->info("WebSocket get: {:s}", payload);
else console->warn("Can't process other then text messages");
return nullptr;
}
std::vector<char>* MagWebServer::assemble_websocket_message(const std::string& message)
{
auto* data = new std::vector<char>;
websocket_frame_1 frame_1 = {0};
frame_1.FIN = 1;
frame_1.OPCODE = TEXT_FRAME;
frame_1.MASK = 0;
data->push_back(frame_1.bytes[0]);
const auto len = message.length();
if (len < 126)
{
frame_1.PAYLOAD_LEN = len;
data->push_back(frame_1.bytes[1]);
}
else if (len >= 126 && len < 65535)
{
frame_1.PAYLOAD_LEN = 126;
data->push_back(frame_1.bytes[1]);
auto* p = (uint8_t*)&len;
data->push_back(p[0]);
data->push_back(p[1]);
}
else
{
frame_1.PAYLOAD_LEN = 127;
data->push_back(frame_1.bytes[1]);
auto* p = (uint8_t*)&len;
for (std::size_t i=0; i<8; ++i)
{
data->push_back(p[i]);
}
}
data->insert(data->end(), message.begin(), message.end());
return data;
}
}
......@@ -42,15 +42,20 @@ namespace MagWebServer
};
#pragma pack(push, 1)
struct websocket_frame_1
union websocket_frame_1
{
uint8_t OPCODE : 4;
uint8_t RSV3 : 1;
uint8_t RSV2 : 1;
uint8_t RSV1 : 1;
uint8_t FIN : 1;
uint8_t PAYLOAD_LEN : 7;
uint8_t MASK : 1;
struct
{
uint8_t OPCODE : 4;
uint8_t RSV3 : 1;
uint8_t RSV2 : 1;
uint8_t RSV1 : 1;
uint8_t FIN : 1;
uint8_t PAYLOAD_LEN : 7;
uint8_t MASK : 1;
};
uint8_t bytes[2];
};
#pragma pack(pop)
......@@ -69,6 +74,7 @@ namespace MagWebServer
std::vector<char>* process_http(const buffer_t& buffer, const size_t bytes);
std::vector<char>* process_websocket(const buffer_t& buffer, const size_t bytes);
std::vector<char>* assemble_websocket_message(const std::string& message);
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment