Repository navigation
Expand file tree
/
Copy pathmsg.h
More file actions
99 lines (82 loc) · 1.6 KB
/
Copy pathmsg.h
File metadata and controls
99 lines (82 loc) · 1.6 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef _zz_msg_h_
#define _zz_msg_h_
#include <string>
#include "base/zztype_def.h"
namespace zz {
struct msg_head_t
{
uint16_t cmd_id;
uint16_t flag;
uint32_t msg_body_size;
};
struct msg_t
{
public:
/**
* 添加消息头.
* @param buf {const char*} 输入缓冲.
* @param off {const char*} 已经添加的消息头字节数.
* @param len {size_t} 输入参数:输入缓冲区的长度. 输出参数:实际写入的数据长度.
* @param {bool} 是否完整写入消息头.
*/
bool add_head(const char* buf, size_t off, IN OUT size_t& len)
{
bool ret;
size_t copy_size;
if ( (off+len)>=sizeof(msg_head_t) )
{
copy_size = sizeof(msg_head_t)-off;
ret = true;
}
else
{
copy_size = len;
ret = false ;
}
len = copy_size;
memcpy((char*)&head+off, buf, copy_size);
return ret;
}
/**
* 添加消息体.
* @param buf {const char*} 输入缓冲.
* @param off {const char*} 已经添加的消息体字节数.
* @param len {size_t} 输入参数:输入缓冲区的长度. 输出参数:实际写入的数据长度.
* @param {bool} 是否完整写入消息体.
*/
bool add_body(const char* buf, size_t off, IN OUT size_t& len)
{
bool ret;
size_t copy_size;
if ( (off+len)>=head.msg_body_size)
{
copy_size = head.msg_body_size-off;
ret = true;
}
else
{
copy_size = len;
ret = false ;
}
len = copy_size;
body.append(buf, copy_size);
return ret;
}
void clear()
{
memset(&head, 0, sizeof(head));
body.clear();
}
bool have_recv_complete_head(size_t have_recv_size) const
{
return have_recv_size>= sizeof(head);
}
uint16_t cmd_id()
{
return head.cmd_id;
}
msg_head_t head;
std::string body;
};
}
#endif