Modify flora license version.
[platform/core/messaging/msg-service.git] / include / utils / MsgIpcSocket.h
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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
77         int sockfd, remotefd, maxfd;
78         fd_set fds;
79 };
80
81
82 class MsgIpcServerSocket
83 {
84 public:
85         MsgIpcServerSocket();
86         ~MsgIpcServerSocket() { mapFds.clear(); }
87         int     maxFd() { return (maxfd+1); }
88         fd_set  fdSet() { return fds; }
89         int     fd() { return sockfd; }
90
91         msg_error_t open(const char *path);
92         msg_error_t accept();
93         void            close(int fd);
94
95         /* read msg from client of fd */
96         int     read(int fd, char** buf, int* len );
97         /* write msg to ipc client */
98         int     write(int fd, const char* buf, unsigned int len);
99         void    addfd(int fd);
100         void    setSockfd(int fd) { sockfd = fd; }
101
102 private:
103         int readn(int fd, char *buf, unsigned int len );
104         int writen (int fd, const char *buf, unsigned int len);
105
106         /* server socket fd */
107         int sockfd;
108
109         /* information about IPC clients, it is used for select() */
110         fd_set                          fds;
111         int                             maxfd;
112         std::map<int, int>      mapFds;
113 };
114
115 #endif //__IPCSocket_H__
116