sensord: terminate sensor daemon safely
[platform/core/system/sensord.git] / src / shared / csocket.h
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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 _CSOCKET_H_
21 #define _CSOCKET_H_
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sensor_log.h>
30
31 class csocket {
32 public:
33         csocket();
34         virtual ~csocket();
35         csocket(int sock_fd);
36         csocket(const csocket &sock);
37
38         //Server
39         bool create(int sock_type);
40         bool bind(const char *sock_path);
41         bool listen(const int max_connections);
42         bool accept(csocket& client_socket) const;
43
44         //Client
45         bool connect(const char *sock_path);
46
47         //Data Transfer
48         ssize_t send(const void *buffer, size_t size) const;
49         ssize_t recv(void* buffer, size_t size) const;
50
51         bool set_connection_mode(void);
52         bool set_transfer_mode(void);
53         bool is_blocking_mode(void);
54
55         //check if socket is created
56         bool is_valid(void) const;
57         int get_socket_fd(void) const;
58
59         bool close(void);
60
61         bool is_block_mode(void);
62
63 private:
64         bool set_blocking_mode(bool blocking);
65         bool set_sock_type(void);
66
67         ssize_t send_for_seqpacket(const void *buffer, size_t size) const;
68         ssize_t send_for_stream(const void *buffer, size_t size) const;
69         ssize_t recv_for_seqpacket(void* buffer, size_t size) const;
70         ssize_t recv_for_stream(void* buffer, size_t size) const;
71
72 private:
73         int m_sock_fd;
74         int m_sock_type;
75         sockaddr_un m_addr;
76         int m_send_flags;
77         int m_recv_flags;
78 };
79
80 #endif /* _CSOCKET_H_ */