-added shutdown handle and cancel
[oweals/gnunet.git] / src / include / gnunet_stream_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_stream_lib.h
23  * @brief stream handling using mesh API
24  * @author Sree Harsha Totakura
25  */
26
27 #ifndef GNUNET_STREAM_LIB_H
28 #define GNUNET_STREAM_LIB_H
29
30 #ifdef __cplusplus
31 extern "C" 
32 {
33 #if 0
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_mesh_service.h"
40
41 /**
42  * Stream status 
43  */
44 enum GNUNET_STREAM_Status
45   {
46     /**
47      * All previous read/write operations are successfully done
48      */
49     GNUNET_STREAM_OK = 0,
50
51     /**
52      * A timeout occured while reading/writing the stream
53      */
54     GNUNET_STREAM_TIMEOUT = 1,
55
56     /**
57      * Other side has shutdown the socket for this type of operation
58      * (reading/writing)
59      */
60     GNUNET_STREAM_SHUTDOWN = 2,
61
62     /**
63      * A serious error occured while operating on this stream
64      */
65     GNUNET_STREAM_SYSERR = 3
66   };
67
68 /**
69  * Opaque handler for stream
70  */
71 struct GNUNET_STREAM_Socket;
72
73 /**
74  * Functions of this type will be called when a stream is established
75  *
76  * @param cls the closure from GNUNET_STREAM_open
77  * @param socket socket to use to communicate with the other side (read/write)
78  */
79 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
80                                             struct GNUNET_STREAM_Socket *socket);
81
82
83 /**
84  * Options for the stream.
85  */
86 enum GNUNET_STREAM_Option
87   {
88     /**
89      * End of the option list.
90      */
91     GNUNET_STREAM_OPTION_END = 0,
92
93     /**
94      * Option to set the initial retransmission timeout (when do we retransmit
95      * a packet that did not yield an acknowledgement for the first time?).  
96      * Repeated retransmissions will then use an exponential back-off.
97      * Takes a 'struct GNUNET_TIME_Relative' as the only argument.  A value
98      * of '0' means to use the round-trip time (plus a tiny grace period);
99      * this is also the default.
100      */
101     GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT
102   };
103
104
105 /**
106  * Tries to open a stream to the target peer
107  *
108  * @param cfg configuration to use
109  * @param target the target peer to which the stream has to be opened
110  * @param app_port the application port number which uniquely identifies this
111  *            stream
112  * @param open_cb this function will be called after stream has be established 
113  * @param open_cb_cls the closure for open_cb
114  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
115  * @return if successful it returns the stream socket; NULL if stream cannot be
116  *         opened 
117  */
118 struct GNUNET_STREAM_Socket *
119 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
120                     const struct GNUNET_PeerIdentity *target,
121                     GNUNET_MESH_ApplicationType app_port,
122                     GNUNET_STREAM_OpenCallback open_cb,
123                     void *open_cb_cls,
124                     ...);
125
126
127 /**
128  * Handle for shutdown
129  */
130 struct GNUNET_STREAM_ShutdownHandle;
131
132
133 /**
134  * Shutdown the stream for reading or writing (man 2 shutdown).
135  *
136  * @param socket the stream socket
137  * @param how SHUT_RD, SHUT_WR or SHUT_RDWR 
138  * @return the shutdown handle
139  */
140 struct GNUNET_STREAM_ShutdownHandle *
141 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
142                         int how);
143
144
145 /**
146  * Cancels a pending shutdown
147  *
148  * @param the shutdown handle returned from GNUNET_STREAM_shutdown
149  */
150 void
151 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
152
153
154 /**
155  * Closes the stream and frees the associated state. The stream should be
156  * shutdown before closing.
157  *
158  * @param socket the stream socket
159  */
160 void
161 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
162
163
164 /**
165  * Functions of this type are called upon new stream connection from other peers
166  *
167  * @param cls the closure from GNUNET_STREAM_listen
168  * @param socket the socket representing the stream
169  * @param initiator the identity of the peer who wants to establish a stream
170  *            with us
171  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
172  *             stream (the socket will be invalid after the call)
173  */
174 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
175                                              struct GNUNET_STREAM_Socket *socket,
176                                              const struct 
177                                              GNUNET_PeerIdentity *initiator);
178
179
180 /**
181  * A socket for listening.
182  */
183 struct GNUNET_STREAM_ListenSocket;
184
185 /**
186  * Listens for stream connections for a specific application ports
187  *
188  * @param cfg the configuration to use
189  * @param app_port the application port for which new streams will be accepted
190  * @param listen_cb this function will be called when a peer tries to establish
191  *            a stream with us
192  * @param listen_cb_cls closure for listen_cb
193  * @return listen socket, NULL for any error
194  */
195 struct GNUNET_STREAM_ListenSocket *
196 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
197                       GNUNET_MESH_ApplicationType app_port,
198                       GNUNET_STREAM_ListenCallback listen_cb,
199                       void *listen_cb_cls);
200
201
202 /**
203  * Closes the listen socket
204  *
205  * @param lsocket the listen socket
206  */
207 void
208 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
209
210
211 /**
212  * Functions of this signature are called whenever writing operations
213  * on a stream are executed
214  *
215  * @param cls the closure from GNUNET_STREAM_write
216  * @param status the status of the stream at the time this function is called
217  * @param size the number of bytes written
218  */
219 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
220                                                       enum GNUNET_STREAM_Status
221                                                       status,
222                                                       size_t size);
223
224
225 /**
226  * Handle to cancel IO write operations.
227  */
228 struct GNUNET_STREAM_IOWriteHandle;
229
230
231 /**
232  * Handle to cancel IO read operations.
233  */
234 struct GNUNET_STREAM_IOReadHandle;
235
236 /**
237  * Tries to write the given data to the stream. The maximum size of data that
238  * can be written as part of a write operation is (64 * (64000 - sizeof (struct
239  * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
240  * violation, however only the said number of maximum bytes will be written.
241  *
242  * @param socket the socket representing a stream
243  * @param data the data buffer from where the data is written into the stream
244  * @param size the number of bytes to be written from the data buffer
245  * @param timeout the timeout period
246  * @param write_cont the function to call upon writing some bytes into the
247  *          stream 
248  * @param write_cont_cls the closure
249  * @return handle to cancel the operation; NULL if a previous write is pending
250  */
251 struct GNUNET_STREAM_IOWriteHandle *
252 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
253                      const void *data,
254                      size_t size,
255                      struct GNUNET_TIME_Relative timeout,
256                      GNUNET_STREAM_CompletionContinuation write_cont,
257                      void *write_cont_cls);
258
259
260 /**
261  * Functions of this signature are called whenever data is available from the
262  * stream.
263  *
264  * @param cls the closure from GNUNET_STREAM_read
265  * @param status the status of the stream at the time this function is called
266  * @param data traffic from the other side
267  * @param size the number of bytes available in data read; will be 0 on timeout 
268  * @return number of bytes of processed from 'data' (any data remaining should be
269  *         given to the next time the read processor is called).
270  */
271 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
272                                                enum GNUNET_STREAM_Status status,
273                                                const void *data,
274                                                size_t size);
275
276
277 /**
278  * Tries to read data from the stream
279  *
280  * @param socket the socket representing a stream
281  * @param timeout the timeout period
282  * @param proc function to call with data (once only)
283  * @param proc_cls the closure for proc
284  * @return handle to cancel the operation
285  */
286 struct GNUNET_STREAM_IOReadHandle *
287 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
288                     struct GNUNET_TIME_Relative timeout,
289                     GNUNET_STREAM_DataProcessor proc,
290                     void *proc_cls);
291
292
293 /**
294  * Cancels pending write operation. Also cancels packet retransmissions which
295  * may have resulted otherwise.
296  *
297  * CAUTION: Normally a write operation is considered successful if the data
298  * given to it is sent and acknowledged by the receiver. As data is divided
299  * into packets, it is possible that not all packets are received by the
300  * receiver. Any missing packets are then retransmitted till the receiver
301  * acknowledges all packets or until a timeout . During this scenario if the
302  * write operation is cancelled all such retransmissions are also
303  * cancelled. This may leave the receiver's receive buffer incompletely filled
304  * as some missing packets are never retransmitted. So this operation should be
305  * used before shutting down transmission from our side or before closing the
306  * socket.
307  *
308  * @param ioh handle to operation to cancel
309  */
310 void
311 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
312
313
314 /**
315  * Cancel pending read operation.
316  *
317  * @param ioh handle to operation to cancel
318  */
319 void
320 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
321
322
323 #if 0
324 {
325 #endif
326 #ifdef __cplusplus
327 }
328 #endif
329
330 #endif  /* STREAM_PROTOCOL_H */