sensord: resize message size
[platform/core/system/sensord.git] / src / shared / socket.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 __SOCKET_H__
21 #define __SOCKET_H__
22
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <string>
27 #include <atomic>
28
29 namespace ipc {
30
31 class socket {
32 public:
33         socket();
34         socket(int sock_fd);
35         socket(const socket &sock);
36         virtual ~socket();
37
38         virtual bool create(const std::string &path);
39
40         bool connect(void);
41         bool bind(void);
42         bool listen(const int max_connections);
43         bool accept(socket &client_sock);
44
45         bool close(void);
46
47         int  get_fd(void) const;
48         void set_fd(int sock_fd);
49
50         int  get_mode(void) const;
51         bool set_mode(int mode);
52
53         bool set_blocking_mode(bool blocking);
54         bool set_recv_timeout(int timeout);
55
56         /* type : SO_SNDBUF, SO_RCVBUF */
57         bool set_buffer_size(int type, int size);
58         int  get_buffer_size(int type);
59         int  get_current_buffer_size(void);
60
61         ssize_t send(const void *buffer, size_t size, bool select = false) const;
62         ssize_t recv(void* buffer, size_t size, bool select = false) const;
63
64 protected:
65         bool create_by_type(const std::string &path, int type);
66
67 private:
68         virtual ssize_t on_send(const void *buffer, size_t size) const = 0;
69         virtual ssize_t on_recv(void* buffer, size_t size) const = 0;
70
71         int  get_sock_type(void);
72         bool set_sock_type(int type);
73         bool has_connected(void);
74
75         int m_sock_fd;
76         int m_mode;
77         std::atomic<bool> m_listening;
78         std::string m_path;
79 };
80
81 }
82
83 #endif /* __SOCKET_H__ */