2016-11-29 16:54:00 +00:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
2016-12-03 10:51:27 +00:00
|
|
|
#include <ostream>
|
2016-11-29 16:54:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
namespace chat
|
2016-11-29 16:54:00 +00:00
|
|
|
{
|
2016-12-03 12:13:06 +00:00
|
|
|
enum class message : char
|
|
|
|
{
|
|
|
|
HELLO = 1, NEPTUN = 2, PASSW = 3,
|
|
|
|
SERVER_DIRECTION = 4, MESSAGE = 5,
|
|
|
|
PING = 6, PONG = 7, BYE = 8,
|
|
|
|
LOGIN = 9, LOGOUT = 10, TERM = 0x7f
|
|
|
|
};
|
2016-12-03 13:22:49 +00:00
|
|
|
constexpr char byte(message msg) { return static_cast<char>(msg); }
|
2016-11-29 16:54:00 +00:00
|
|
|
|
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
class chat_message
|
|
|
|
{
|
|
|
|
message _header;
|
|
|
|
std::string _content;
|
2016-11-29 16:54:00 +00:00
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
public:
|
|
|
|
chat_message(message header, std::string content = "")
|
|
|
|
: _header(header), _content(content)
|
|
|
|
{}
|
2016-11-29 16:54:00 +00:00
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
chat_message(std::underlying_type<message>::type header_integral, std::string content)
|
|
|
|
: _header(static_cast<message>(header_integral)), _content(content)
|
|
|
|
{}
|
2016-11-29 16:54:00 +00:00
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
std::string get() const
|
2016-12-03 12:50:09 +00:00
|
|
|
{ return byte(_header) + _content + byte(message::TERM); }
|
2016-12-03 12:13:06 +00:00
|
|
|
};
|
2016-12-03 10:51:27 +00:00
|
|
|
|
2016-12-03 12:13:06 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const chat_message& msg)
|
|
|
|
{ return os << msg.get(); }
|
|
|
|
}
|