39 lines
1.1 KiB
C++
39 lines
1.1 KiB
C++
|
#include <http/websocket_message.hpp>
|
||
|
|
||
|
namespace base::http {
|
||
|
WebSocketMessage::WebSocketMessage(const std::string& str) {
|
||
|
payload.emplace<Text>(str);
|
||
|
}
|
||
|
|
||
|
WebSocketMessage::WebSocketMessage(const std::vector<u8>& data) {
|
||
|
payload.emplace<Binary>(data);
|
||
|
}
|
||
|
|
||
|
WebSocketMessage::WebSocketMessage(const std::span<u8>& data) {
|
||
|
auto vec = std::vector<u8> {};
|
||
|
vec.resize(data.size());
|
||
|
std::memcpy(vec.data(), data.data(), data.size());
|
||
|
payload.emplace<Binary>(vec);
|
||
|
}
|
||
|
|
||
|
WebSocketMessage::Type WebSocketMessage::GetType() const {
|
||
|
if(std::holds_alternative<Text>(payload))
|
||
|
return Type::Text;
|
||
|
else if(std::holds_alternative<Binary>(payload))
|
||
|
return Type::Binary;
|
||
|
|
||
|
BASE_CHECK(false, "Shouldn't get here.");
|
||
|
}
|
||
|
|
||
|
const std::string& WebSocketMessage::AsText() const {
|
||
|
BASE_ASSERT(GetType() == Type::Text, "WebSocketMessage isn't holding a Text message");
|
||
|
|
||
|
return std::get<Text>(payload).data;
|
||
|
}
|
||
|
|
||
|
const std::vector<u8>& WebSocketMessage::AsBinary() const {
|
||
|
BASE_ASSERT(GetType() == Type::Binary, "WebSocketMessage isn't holding a Binary message");
|
||
|
return std::get<Binary>(payload).data;
|
||
|
}
|
||
|
} // namespace base::http
|