Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / include / utils / MsgIpcSocket.h
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #ifndef __IPCSocket_H__
18 #define __IPCSocket_H__
19
20 /*==================================================================================================
21                                          INCLUDE FILES
22 ==================================================================================================*/
23 #include <sys/types.h>
24 #include <map>
25
26 #include "MsgTypes.h"
27 #include "MsgCppTypes.h"
28
29
30 /*==================================================================================================
31                                          DEFINES
32 ==================================================================================================*/
33 #define MSG_MAX_IPC_SIZE 50000 // 50 * 1000 = sizeof(msg common info) * max message count
34 #define MAX_NUM_IPC_CLIENT 10
35
36 #define MSG_SOCKET_PATH "/tmp/.msgfw_socket"
37
38 #define CUSTOM_SOCKET_ERROR             -1
39 #define CUSTOM_SOCKET_BACKLOG   10
40
41
42 /*==================================================================================================
43                                          ENUM
44 ==================================================================================================*/
45 typedef enum
46 {
47         CLOSE_CONNECTION_BY_SIGNAL = 0,
48         CLOSE_CONNECTION_BY_USER = -17,
49 } IPC_CONTROL_E;
50
51
52 /*==================================================================================================
53                                      CLASS DEFINITIONS
54 ==================================================================================================*/
55 class MsgIpcClientSocket
56 {
57 public:
58         MsgIpcClientSocket();
59 //      ~MsgIpcClientSocket();
60
61         int     maxFd() { return (maxfd+1); }
62         fd_set  fdSet() { return fds; }
63         int     fd() { return sockfd; }
64
65         msg_error_t connect(const char *path);
66         msg_error_t close();
67         /* write msg to ipc server */
68         int     write(const char* buf, unsigned int len);
69         /* read msg from ipc server */
70         int     read(char** buf, unsigned int* len);
71         void    addfd(int fd);
72         int             getRemoteFd() {return remotefd; }
73 private:
74         int readn(char *buf, unsigned int len );
75         int writen (const char *buf, unsigned int len);
76         bool wait_for_reply();
77
78         int sockfd, remotefd, maxfd;
79         fd_set fds;
80 };
81
82
83 class MsgIpcServerSocket
84 {
85 public:
86         MsgIpcServerSocket();
87         ~MsgIpcServerSocket() { mapFds.clear(); }
88         int     maxFd() { return (maxfd+1); }
89         fd_set  fdSet() { return fds; }
90         int     fd() { return sockfd; }
91
92         msg_error_t open(const char *path);
93         msg_error_t accept();
94         void            close(int fd);
95
96         /* read msg from client of fd */
97         int     read(int fd, char** buf, int* len );
98         /* write msg to ipc client */
99         int     write(int fd, const char* buf, unsigned int len);
100         void    addfd(int fd);
101         void    setSockfd(int fd) { sockfd = fd; }
102
103 private:
104         int readn(int fd, char *buf, unsigned int len );
105         int writen (int fd, const char *buf, unsigned int len);
106
107         /* server socket fd */
108         int sockfd;
109
110         /* information about IPC clients, it is used for select() */
111         fd_set                          fds;
112         int                             maxfd;
113         std::map<int, int>      mapFds;
114 };
115
116 #endif //__IPCSocket_H__
117