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 = "")
|
2016-12-04 19:04:19 +00:00
|
|
|
: _header(header), _content(content)
|
2016-12-03 12:13:06 +00:00
|
|
|
{}
|
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-04 12:58:25 +00:00
|
|
|
|
|
|
|
std::string get_content() const
|
|
|
|
{ return _content; }
|
2016-12-04 13:30:13 +00:00
|
|
|
|
|
|
|
size_t length() const
|
|
|
|
{ return sizeof(char) + _content.size() + sizeof(char); }
|
2016-12-05 14:08:48 +00:00
|
|
|
|
|
|
|
message get_header() const
|
|
|
|
{ return _header; }
|
2016-12-03 12:13:06 +00:00
|
|
|
};
|
2016-12-03 10:51:27 +00:00
|
|
|
|
2016-12-05 13:17:15 +00:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const chat_message& msg)
|
2016-12-03 12:13:06 +00:00
|
|
|
{ return os << msg.get(); }
|
2016-12-04 19:04:19 +00:00
|
|
|
}
|
|
|
|
|