syntax
[oweals/gnunet.git] / src / include / gnunet_stream_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 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 of 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  * Tries to open a stream to the target peer
85  *
86  * @param target the target peer to which the stream has to be opened
87  * @param app_port the application port number which uniquely identifies this
88  *            stream
89  * @param open_cb this function will be called after stream has be established 
90  * @param open_cb_cls the closure for open_cb
91  * @return if successful it returns the stream socket; NULL if stream cannot be
92  *         opened 
93  */
94 struct GNUNET_STREAM_Socket *
95 GNUNET_STREAM_open (const struct GNUNET_PeerIdentity *target,
96                     GNUNET_MESH_ApplicationType app_port,
97                     GNUNET_STREAM_OpenCallback open_cb,
98                     void *open_cb_cls);
99
100
101 /**
102  * Shutdown the stream for reading or writing (man 2 shutdown).
103  *
104  * @param socket the stream socket
105  * @param how SHUT_RD, SHUT_WR or SHUT_RDWR 
106  */
107 void
108 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
109                         int how);
110
111
112 /**
113  * Closes the stream
114  *
115  * @param socket the stream socket
116  */
117 void
118 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
119
120
121 /**
122  * Functions of this type are called upon new stream connection from other peers
123  *
124  * @param cls the closure from GNUNET_STREAM_listen
125  * @param socket the socket representing the stream
126  * @param initiator the identity of the peer who wants to establish a stream
127  *            with us
128  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
129  *             stream (the socket will be invalid after the call)
130  */
131 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
132                                              struct GNUNET_STREAM_Socket *socket,
133                                              const struct 
134                                              GNUNET_PeerIdentity *initiator);
135
136
137 /**
138  * A socket for listening.
139  */
140 struct GNUNET_STREAM_ListenSocket;
141
142 /**
143  * Listens for stream connections for a specific application ports
144  *
145  * @param app_port the application port for which new streams will be accepted
146  * @param listen_cb this function will be called when a peer tries to establish
147  *            a stream with us
148  * @param listen_cb_cls closure for listen_cb
149  * @return listen socket, NULL for any error
150  */
151 struct GNUNET_STREAM_ListenSocket *
152 GNUNET_STREAM_listen (GNUNET_MESH_ApplicationType app_port,
153                       GNUNET_STREAM_ListenCallback listen_cb,
154                       void *listen_cb_cls);
155
156
157 /**
158  * Closes the listen socket
159  *
160  * @param socket the listen socket
161  */
162 void
163 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *socket);
164
165
166 /**
167  * Functions of this signature are called whenever writing operations
168  * on a stream are executed
169  *
170  * @param cls the closure from GNUNET_STREAM_write/read
171  * @param status the status of the stream at the time this function is called
172  * @param size the number of bytes read or written
173  */
174 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
175                                                       enum GNUNET_STREAM_Status
176                                                       status,
177                                                       size_t size);
178
179
180 /**
181  * Handle to cancel IO operations.
182  */
183 struct GNUNET_STREAM_IOHandle;
184
185
186 /**
187  * Tries to write the given data to the stream
188  *
189  * @param socket the socket representing a stream
190  * @param data the data buffer from where the data is written into the stream
191  * @param size the number of bytes to be written from the data buffer
192  * @param timeout the timeout period
193  * @param write_cont the function to call upon writing some bytes into the stream
194  * @param write_cont_cls the closure
195  * @return handle to cancel the operation
196  */
197 struct GNUNET_STREAM_IOHandle *
198 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
199                      const void *data,
200                      size_t size,
201                      struct GNUNET_TIME_Relative timeout,
202                      GNUNET_STREAM_CompletionContinuation write_cont,
203                      void *write_cont_cls);
204
205
206 /**
207  * Functions of this signature are called whenever data is available from the
208  * stream.
209  *
210  * @param cls the closure from GNUNET_STREAM_write/read
211  * @param status the status of the stream at the time this function is called
212  * @param data traffic from the other side
213  * @param size the number of bytes available in data read 
214  */
215 typedef void (*GNUNET_STREAM_DataProcessor) (void *cls,
216                                              enum GNUNET_STREAM_Status status,
217                                              const char *data,
218                                              size_t size);
219
220
221 /**
222  * Tries to read data from the stream
223  *
224  * @param socket the socket representing a stream
225  * @param timeout the timeout period
226  * @param proc function to call with data
227  * @param proc_cls the closure for proc
228  * @return handle to cancel the operation
229  */
230 struct GNUNET_STREAM_IOHandle *
231 GNUNET_STREAM_read (const struct GNUNET_STREAM_Socket *socket,
232                     struct GNUNET_TIME_Relative timeout,
233                     GNUNET_STREAM_DataProcessor proc,
234                     void *proc_cls);
235
236
237 /**
238  * Cancel pending read or write operation.
239  *
240  * @param ioh handle to operation to cancel
241  */
242 void
243 GNUNET_STREAM_io_cancel (struct GNUNET_STREAM_IOHandle *ioh);
244
245
246 #if 0
247 {
248 #endif
249 #ifdef __cplusplus
250 }
251 #endif
252
253 #endif