/***
This file is part of snapcast
Copyright (C) 2014-2025 Johannes Pohl
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
***/
#pragma once
// local headers
#include "client_info.hpp"
#include "codec_header.hpp"
#include "hello.hpp"
#include "pcm_chunk.hpp"
#include "server_settings.hpp"
#include "time.hpp"
namespace msg
{
/// Cast a BaseMessage @message to type "ToType"
/// @return castest message or nullptr, if the cast failed
template
static std::unique_ptr message_cast(std::unique_ptr message)
{
auto* tmp = dynamic_cast(message.get());
if (tmp != nullptr)
{
message.release();
std::unique_ptr result(tmp);
return result;
}
return nullptr;
}
namespace factory
{
/// Create a message of type T from @p base_message beaser and payload @p buffer
template
static std::unique_ptr createMessage(const BaseMessage& base_message, char* buffer)
{
std::unique_ptr result = std::make_unique();
if (!result)
return nullptr;
result->deserialize(base_message, buffer);
return result;
}
/// Create a BaseMessage from @p base_message header and payload @p buffer
static std::unique_ptr createMessage(const BaseMessage& base_message, char* buffer)
{
std::unique_ptr result;
switch (base_message.type)
{
case message_type::kCodecHeader:
return createMessage(base_message, buffer);
case message_type::kHello:
return createMessage(base_message, buffer);
case message_type::kServerSettings:
return createMessage(base_message, buffer);
case message_type::kTime:
return createMessage