Integrate internal fixes
[platform/core/system/sensord.git] / src / shared / channel.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 __CHANNEL_H__
21 #define __CHANNEL_H__
22
23 #include <unistd.h>
24 #include <atomic>
25
26 #include "socket.h"
27 #include "message.h"
28 #include "event_loop.h"
29 #include "channel_handler.h"
30
31 namespace ipc {
32
33 class channel_handler;
34
35 class channel {
36 public:
37         /* move owernership of the socket to the channel */
38         channel(socket *sock);
39         ~channel();
40
41         uint64_t bind(void);
42         uint64_t bind(channel_handler *handler, event_loop *loop, bool loop_bind);
43
44         uint64_t connect(channel_handler *handler, event_loop *loop, bool loop_bind);
45         void disconnect(void);
46
47         bool is_connected(void);
48
49         bool send(message *msg);
50         bool send_sync(message *msg);
51
52         bool read(void);
53         bool read_sync(message &msg, bool select = true);
54
55         bool get_option(int type, int &value) const;
56         bool set_option(int type, int value);
57
58         int  get_fd(void) const;
59
60 private:
61         int m_fd;
62         uint64_t m_event_id;
63         socket *m_socket;
64         channel_handler *m_handler;
65         event_loop *m_loop;
66
67         std::atomic<bool> m_connected;
68 };
69
70 }
71
72 #endif /* __CHANNEL_H__ */