Use a buffer size when disclosing message
[platform/core/system/sensord.git] / src / shared / message.h
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifndef __MESSAGE_H__
21 #define __MESSAGE_H__
22
23 #include <stdlib.h> /* size_t */
24 #include <atomic>
25 #include <memory>
26
27 #define MAX_MSG_CAPACITY (32*1024)
28 #define MAX_HEADER_RESERVED 3
29
30 namespace ipc {
31
32 typedef struct message_header {
33         uint64_t id { 0 };
34         uint32_t type { 0 };
35         size_t length { 0 };
36         int32_t err { 0 };
37         void *ancillary[MAX_HEADER_RESERVED] { nullptr };
38 } message_header;
39
40 class message {
41 public:
42         template <class... Args>
43         static std::shared_ptr<message> create(Args&&... args)
44                 noexcept(noexcept(message(std::forward<Args>(args)...)))
45         {
46                 return std::shared_ptr<message>(new (std::nothrow) message(std::forward<Args>(args)...));
47         }
48
49         message(size_t capacity = MAX_MSG_CAPACITY);
50         message(const void *msg, size_t size);
51         message(const message &msg);
52         message(int err);
53         ~message();
54
55         void enclose(const void *msg, const size_t size);
56         void enclose(int error);
57         void disclose(void *msg, const size_t size);
58
59         uint32_t type(void);
60         void set_type(uint32_t type);
61
62         size_t size(void);
63
64         void ref(void);
65         void unref(void);
66         int  ref_count(void);
67
68         message_header *header(void);
69         char *body(void);
70
71 private:
72         message_header m_header;
73         size_t m_size;
74         size_t m_capacity;
75
76         char *m_msg;
77 };
78
79 }
80
81 #endif /* __MESSAGE_H__ */