update tizen source
[framework/messaging/msg-service.git] / include / utils / MsgIpcSocket.h
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #ifndef __IPCSocket_H__
32 #define __IPCSocket_H__
33
34 /*==================================================================================================
35                                          INCLUDE FILES
36 ==================================================================================================*/
37 #include <sys/types.h>
38 #include <map>
39
40 #include "MsgTypes.h"
41 #include "MsgCppTypes.h"
42
43
44 /*==================================================================================================
45                                          DEFINES
46 ==================================================================================================*/
47 #define MSG_MAX_IPC_SIZE 50000 // 50 * 1000 = sizeof(msg common info) * max message count
48 #define MAX_NUM_IPC_CLIENT 10
49
50 #define MSG_SOCKET_PATH "/tmp/.msgfw_socket"
51
52 #define CUSTOM_SOCKET_ERROR             -1
53 #define CUSTOM_SOCKET_BACKLOG   10
54
55
56 /*==================================================================================================
57                                          ENUM
58 ==================================================================================================*/
59 typedef enum
60 {
61         CLOSE_CONNECTION_BY_SIGNAL = 0,
62         CLOSE_CONNECTION_BY_USER = -17,
63 } IPC_CONTROL_E;
64
65
66 /*==================================================================================================
67                                      CLASS DEFINITIONS
68 ==================================================================================================*/
69 class MsgIpcClientSocket
70 {
71 public:
72         MsgIpcClientSocket();
73 //      ~MsgIpcClientSocket();
74
75         int     maxFd() { return (maxfd+1); }
76         fd_set  fdSet() { return fds; }
77         int     fd() { return sockfd; }
78
79         MSG_ERROR_T connect(const char *path);
80         MSG_ERROR_T close();
81         /* write msg to ipc server */
82         int     write(const char* buf, int len);
83         /* read msg from ipc server */
84         int     read(char** buf, int* len);
85         void    addfd(int fd);
86         int             getRemoteFd() {return remotefd; }
87 private:
88         int readn(char *buf, int len );
89         int writen (const char *buf, int len);
90
91         int sockfd, remotefd, maxfd;
92         fd_set fds;
93 };
94
95
96 class MsgIpcServerSocket
97 {
98 public:
99         MsgIpcServerSocket();
100         ~MsgIpcServerSocket() { mapFds.clear(); }
101         int     maxFd() { return (maxfd+1); }
102         fd_set  fdSet() { return fds; }
103         int     fd() { return sockfd; }
104
105         MSG_ERROR_T open(const char *path);
106         MSG_ERROR_T accept();
107         void            close(int fd);
108
109         /* read msg from client of fd */
110         int     read(int fd, char** buf, int* len );
111         /* write msg to ipc client */
112         int     write(int fd, const char* buf, int len);
113         void    addfd(int fd);
114         void    setSockfd(int fd) { sockfd = fd; }
115
116 private:
117         int readn(int fd, char *buf, int len );
118         int writen (int fd, const char *buf, int len);
119
120         /* server socket fd */
121         int sockfd;
122
123         /* information about IPC clients, it is used for select() */
124         fd_set                          fds;
125         int                             maxfd;
126         std::map<int, int>      mapFds;
127 };
128
129 #endif //__IPCSocket_H__
130