Change message_type from enum to enum class

This commit is contained in:
badaix 2021-05-16 21:21:36 +02:00
parent aba2d96aba
commit 5b8cd7ecd5
3 changed files with 60 additions and 15 deletions

View file

@ -1,6 +1,6 @@
/*** /***
This file is part of snapcast This file is part of snapcast
Copyright (C) 2014-2020 Johannes Pohl Copyright (C) 2014-2021 Johannes Pohl
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -68,21 +68,21 @@ static std::unique_ptr<BaseMessage> createMessage(const BaseMessage& base_messag
std::unique_ptr<BaseMessage> result; std::unique_ptr<BaseMessage> result;
switch (base_message.type) switch (base_message.type)
{ {
case kCodecHeader: case message_type::kCodecHeader:
return createMessage<CodecHeader>(base_message, buffer); return createMessage<CodecHeader>(base_message, buffer);
case kHello: case message_type::kHello:
return createMessage<Hello>(base_message, buffer); return createMessage<Hello>(base_message, buffer);
case kServerSettings: case message_type::kServerSettings:
return createMessage<ServerSettings>(base_message, buffer); return createMessage<ServerSettings>(base_message, buffer);
case kStreamTags: case message_type::kStreamTags:
return createMessage<StreamTags>(base_message, buffer); return createMessage<StreamTags>(base_message, buffer);
case kTime: case message_type::kTime:
return createMessage<Time>(base_message, buffer); return createMessage<Time>(base_message, buffer);
case kWireChunk: case message_type::kWireChunk:
// this is kind of cheated to safe the convertion from WireChunk to PcmChunk // this is kind of cheated to safe the convertion from WireChunk to PcmChunk
// the user of the factory must be aware that a PcmChunk will be created // the user of the factory must be aware that a PcmChunk will be created
return createMessage<PcmChunk>(base_message, buffer); return createMessage<PcmChunk>(base_message, buffer);
case kClientInfo: case message_type::kClientInfo:
return createMessage<ClientInfo>(base_message, buffer); return createMessage<ClientInfo>(base_message, buffer);
default: default:
return nullptr; return nullptr;

View file

@ -28,8 +28,8 @@
#ifndef WINDOWS #ifndef WINDOWS
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <vector>
#include <memory> #include <memory>
#include <vector>
/* /*
template<typename CharT, typename TraitsT = std::char_traits<CharT> > template<typename CharT, typename TraitsT = std::char_traits<CharT> >
@ -52,7 +52,7 @@ struct membuf : public std::basic_streambuf<char>
}; };
enum message_type enum class message_type : uint16_t
{ {
kBase = 0, kBase = 0,
kCodecHeader = 1, kCodecHeader = 1,
@ -67,6 +67,39 @@ enum message_type
kLast = kClientInfo kLast = kClientInfo
}; };
static std::ostream& operator<<(std::ostream& os, const message_type& msg_type)
{
switch (msg_type)
{
case message_type::kBase:
os << "Base";
break;
case message_type::kCodecHeader:
os << "CodecHeader";
break;
case message_type::kWireChunk:
os << "WireChunk";
break;
case message_type::kServerSettings:
os << "ServerSettings";
break;
case message_type::kTime:
os << "Time";
break;
case message_type::kHello:
os << "Hello";
break;
case message_type::kStreamTags:
os << "StreamTags";
break;
case message_type::kClientInfo:
os << "ClientInfo";
break;
default:
os << "Unknown";
}
return os;
}
struct tv struct tv
{ {
@ -121,11 +154,11 @@ using message_ptr = std::shared_ptr<msg::BaseMessage>;
struct BaseMessage struct BaseMessage
{ {
BaseMessage() : type(kBase), id(0), refersTo(0) BaseMessage() : type(message_type::kBase), id(0), refersTo(0)
{ {
} }
BaseMessage(message_type type_) : type(static_cast<uint16_t>(type_)), id(0), refersTo(0) BaseMessage(message_type type_) : type(type_), id(0), refersTo(0)
{ {
} }
@ -182,7 +215,7 @@ struct BaseMessage
return 3 * sizeof(uint16_t) + 2 * sizeof(tv) + sizeof(uint32_t); return 3 * sizeof(uint16_t) + 2 * sizeof(tv) + sizeof(uint32_t);
}; };
uint16_t type; message_type type;
mutable uint16_t id; mutable uint16_t id;
uint16_t refersTo; uint16_t refersTo;
tv received; tv received;
@ -207,6 +240,12 @@ protected:
stream.write(reinterpret_cast<const char*>(&v), sizeof(uint16_t)); stream.write(reinterpret_cast<const char*>(&v), sizeof(uint16_t));
} }
void writeVal(std::ostream& stream, const message_type& val) const
{
uint16_t v = static_cast<uint16_t>(SWAP_16(val));
stream.write(reinterpret_cast<const char*>(&v), sizeof(uint16_t));
}
void writeVal(std::ostream& stream, const int16_t& val) const void writeVal(std::ostream& stream, const int16_t& val) const
{ {
uint16_t v = SWAP_16(val); uint16_t v = SWAP_16(val);
@ -257,6 +296,12 @@ protected:
val = SWAP_16(val); val = SWAP_16(val);
} }
void readVal(std::istream& stream, message_type& val) const
{
stream.read(reinterpret_cast<char*>(&val), sizeof(uint16_t));
val = static_cast<message_type>(SWAP_16(val));
}
void readVal(std::istream& stream, int16_t& val) const void readVal(std::istream& stream, int16_t& val) const
{ {
stream.read(reinterpret_cast<char*>(&val), sizeof(int16_t)); stream.read(reinterpret_cast<char*>(&val), sizeof(int16_t));

View file

@ -1,6 +1,6 @@
/*** /***
This file is part of snapcast This file is part of snapcast
Copyright (C) 2014-2020 Johannes Pohl Copyright (C) 2014-2021 Johannes Pohl
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -55,7 +55,7 @@ class shared_const_buffer
{ {
std::vector<char> data; std::vector<char> data;
bool is_pcm_chunk; bool is_pcm_chunk;
uint16_t type; message_type type;
chronos::time_point_clk rec_time; chronos::time_point_clk rec_time;
}; };