forked from xl4-shiro/xl4combase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cb_ipcsock.h
186 lines (164 loc) · 6.39 KB
/
cb_ipcsock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Excelfore Communication Base Library
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-combase.
*
* Excelfore-combase is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Excelfore-combase is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-combase. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
/**
* @defgroup IPC utility functions
* @{
* @file cb_ipcsock.h
* @copyright Copyright (C) 2019 Excelfore Corporation
* @author Shiro Ninomiya ([email protected])
*
* @brief IPC utility functions
*/
#ifndef __CB_IPCSOCK_H_
#define __CB_IPCSOCK_H_
typedef enum {
CB_IPCCLIENT_DEFAULT = 0,
CB_IPCCLIENT_BINARY,
CB_IPCCLIENT_TEXT,
} cb_ipcclient_commode_t;
/**
* @brief read from file descriptor with timeout
* @return 0 on timeout, -1 on error, positive number on read size
* @param fd a file descriptor
* @param data buffer to read data, which must have more than size bytes
* @param size read size
* @param tout_ms timeout time in milliseconds
*/
int cb_fdread_timeout(int fd, void *data, int size, int tout_ms);
/**
* @brief creats and initializes Unix Domain Socket for IPC
* @return 0 on success, -1 on error
* @param ipcfd return opened file descriptor
* @param node main part of file node i.e /temp/node
* @param suffix suffix part of file node, set "" for non suffix
* @param server_node if NULL, create a socket without connection. \n
* if not NULL, connect to this server_node(must be existing node).
*/
int cb_ipcsocket_init(int *ipcfd, char *node, char *suffix, char *server_node);
/**
* @brief open Unix Domain Socket in UDP mode for IPC
* @return 0 on success, -1 on error.
* @param ipcfd return opened file descriptor for socket.
* @param own_ip NULL for any IF, '127.0.0.1' for local only
* @param server_ip NULL in the server mode, set IP in the client mode
* @param server_port port number on which the server mode listens
*/
int cb_ipcsocket_udp_init(int *ipcfd, char *own_ip, char *server_ip, int server_port);
/**
* @brief close Unix Domain Socket for IPC
* @return 0 on success, -1 on error
* @param ipcfd ipc file descriptor
* @param node main part of file node
* @param suffix suffix part of file node, set "" for non suffix
* @note this call unlink the node name of node+suffix
*/
int cb_ipcsocket_close(int ipcfd, char *node, char *suffix);
/*
* this number of IPC clients can be allowed to connect.
* 2-connection memory data is expanded by increasing of connections,
* and when the disconnection is detected the data is removed and freed.
*/
#define MAX_IPC_CLIENTS 16
typedef struct cb_ipcserverd cb_ipcserverd_t;
/**
* @brief initialize the server mode ipc socket
* @return the data handle
* @param node_ip unix domain socket file node name OR udp socket port IP address
* @param suffix suffix part of file node, set "" for non suffix
* @param port the local port number for udp mode connection. Set 0 for unix domain socket.
*/
cb_ipcserverd_t *cb_ipcsocket_server_init(char *node_ip, char *suffix, uint16_t port);
/**
* @brief close the server mode ipc socket
* @param ipcsd the data handle
*/
void cb_ipcsocket_server_close(cb_ipcserverd_t *ipcsd);
/**
* @brief send ipc data to a specific client_address or internally managed IPC clients
* @return 0 on success, -1 on error
* @param ipcsd the data handle
* @param data send data
* @param size send data size
* @param client_address if set, the data is sent to this client_address,
* if NULL, the data is sent to all IPC cients
* depends on 'ipcsd->udpport', client_address is 'sockaddr_in' or 'sockaddr_un'
*/
int cb_ipcsocket_server_write(cb_ipcserverd_t *ipcsd, uint8_t *data, int size,
struct sockaddr *client_address);
/**
* @brief callback function to get sending data
* @note *sdata must be allocated in the callback
*/
typedef int(* cb_ipcsocket_server_ddatacb)(void *cbdata, uint8_t **sdata,
int *size, struct sockaddr *addr);
/**
* @brief send ipc data to all clients
* @return 0 on success, -1 on error
* @param ipcsd the data handle
* @param ddatacb callback function to get defered data from the caller
* @note this is used when notice data is different for each client.
* the data is not provided to this call but got by the callback.
*/
int cb_ipcsocket_server_write_ddata(cb_ipcserverd_t *ipcsd, void *cbdata,
cb_ipcsocket_server_ddatacb ddatacb);
/**
* @brief callback function to be called from 'cb_ipcsocket_server_read'
* @note if the callback returns non-zero, the connection is closed.
*/
typedef int(* cb_ipcsocket_server_rdcb)(void *cbdata, uint8_t *rdata,
int size, struct sockaddr *addr);
/**
* @brief receive data on the IPC socket.
* @return 0 on success, -1 on error
* @param ipcsd the data handle
* @param ipccb a callback function to be called with the read data
* @param cbdata data to be passed with the callback
* @note this may block the process. the caller functin must check events not to be blocked
*/
int cb_ipcsocket_server_read(cb_ipcserverd_t *ipcsd,
cb_ipcsocket_server_rdcb ipccb, void *cbdata);
/**
* @brief return ipc socket fd
*/
int cb_ipcsocket_getfd(cb_ipcserverd_t *ipcsd);
/**
* @brief remove IPC client from the managed list
*/
int cb_ipcsocket_remove_client(cb_ipcserverd_t *ipcsd, struct sockaddr *client_address);
/**
* @brief get IPC ipc communication mode
* @return -1 on error, ipc communication mode
* @param ipcsd the data handle
* @param client_address if set, the data is sent to this client_address,
*/
cb_ipcclient_commode_t cb_ipcsocket_get_commode(cb_ipcserverd_t *ipcsd,
struct sockaddr *client_address);
/**
* @brief get IPC ipc communication mode
* @return 0 on success, -1 on error
* @param ipcsd the data handle
* @param client_address if set, the data is sent to this client_address,
* @commode ipc communication mode
*/
int cb_ipcsocket_set_commode(cb_ipcserverd_t *ipcsd, struct sockaddr *client_address,
cb_ipcclient_commode_t commode);
#endif
/** @}*/