/
MsgBlock.hpp
56 lines (47 loc) · 1.18 KB
/
MsgBlock.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
#include <cstring>
#include "Core/Core.hpp"
using namespace IRCCore;
#include "Server/IrcConstants.hpp"
namespace IRC
{
struct MsgBlock;
/** Message block for managing the fixed Constants::MESSAGE_LEN_MAX size message data.
*
* @details new/delete overrided with memory pool.
*/
struct MsgBlock : public FlexibleMemoryPoolingBase<MsgBlock>
{
public:
char Msg[MESSAGE_LEN_MAX];
size_t MsgLen;
FORCEINLINE MsgBlock()
: Msg()
, MsgLen(0)
{
Msg[0] = '\0'; //< Not necessary. Just for debug.
}
FORCEINLINE MsgBlock(const char* str, size_t msgLen)
: Msg()
, MsgLen(msgLen)
{
// If the string is too long, truncate it.
if (msgLen >= MESSAGE_LEN_MAX)
{
MsgLen = MESSAGE_LEN_MAX - CRLF_LEN_2;
}
std::memcpy(Msg, str, MsgLen);
}
FORCEINLINE MsgBlock(const std::string& str)
: Msg()
, MsgLen(str.size())
{
// If the string is too long, truncate it.
if (str.size() >= MESSAGE_LEN_MAX)
{
MsgLen = MESSAGE_LEN_MAX - CRLF_LEN_2;
}
std::memcpy(Msg, str.c_str(), MsgLen);
}
};
} // namespace IRC