added API definitions for stream library
[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      * A serious error occured while operating of this stream
58      */
59     GNUNET_STREAM_SYSERR = 2
60   };
61
62 /**
63  * Opaque handler for stream
64  */
65 struct GNUNET_STREAM_socket;
66
67 /**
68  * Functions of this type will be called when a stream is established
69  *
70  * @param cls the closure from GNUNET_STREAM_open
71  */
72 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls);
73
74 /**
75  * Tries to open a stream to the target peer
76  *
77  * @param cls the closure
78  * @param target the target peer to which the stream has to be opened
79  * @param app_port the application port number which uniquely identifies this
80  *            stream
81  * @param open_cb this function will be called after stream has be established 
82  * @return if successful it returns the stream socket; NULL if stream cannot be
83  *         opened 
84  */
85 struct GNUNET_STREAM_socket *
86 GNUNET_STREAM_open (void *cls,
87                     const struct GNUNET_PeerIdentity *target,
88                     GNUNET_MESH_ApplicationType app_port,
89                     GNUNET_STREAM_OpenCallback open_cb);
90
91 /**
92  * Functions of this type are called upon new stream connection from other peers
93  *
94  * @param cls the closure from GNUNET_STREAM_listen
95  * @param socket the socket representing the stream
96  * @param initiator the identity of the peer who wants to establish a stream
97  *            with us
98  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
99  *             stream (the socket will be invalid after the call)
100  */
101 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
102                                              struct GNUNET_STREAM_socket *socket,
103                                              const struct 
104                                              GNUNET_PeerIdentity *initiator);
105
106 /**
107  * Listens for stream connections for a specific application ports
108  *
109  * @param app_port the application port for which new streams will be accepted
110  * @param listen_cb this function will be called when a peer tries to establish
111  *            a stream with us
112  * @return GNUNET_OK if we are listening, GNUNET_SYSERR for any error
113  */
114 int
115 GNUNET_STREAM_listen (GNUNET_MESH_ApplicationType app_port,
116                       GNUNET_STREAM_ListenCallback listen_cb,
117                       void *cls);
118
119 /**
120  * Functions of this signature are called whenever reading/writing operations
121  * on a stream are executed
122  *
123  * @param cls the closure from GNUNET_STREAM_write/read
124  * @param status the status of the stream at the time this function is called
125  * @param size the number of bytes read or written
126  */
127 typedef void (*GNUNET_STREAM_CompletionCallback) (void *cls,
128                                                   enum GNUNET_STREAM_Status
129                                                   status,
130                                                   size_t size);
131
132 /**
133  * Tries to write the given data to the stream
134  *
135  * @param socket the socket representing a stream
136  * @param data the data buffer from where the data is written into the stream
137  * @param size the number of bytes to be written from the data buffer
138  * @param write_cb the function to call upon writing some bytes into the stream
139  * @param timeout the timeout period
140  * @param cls the closure
141  */
142 void
143 GNUNET_STREAM_write (const struct GNUNET_STREAM_socket *socket,
144                      void *data,
145                      size_t size,
146                      GNUNET_STREAM_CompletionCallback write_cb,
147                      struct GNUNET_TIME_Relative timeout,
148                      void *cls);
149
150 /**
151  * Tries to read data from the stream
152  *
153  * @param socket the socket representing a stream
154  * @param buffer the buffer into which the read data is stored
155
156  * @return 
157  */
158 size_t
159 GNUNET_STREAM_read (const struct GNUNET_STREAM_socket *socket,
160                     void *buffer,
161                     size_t size,
162                     GNUNET_STREAM_CompletionCallback read_cb,
163                     struct GNUNET_TIME_Relative timeout,
164                     void *cls);
165 /**
166  * Closes the stream
167  *
168  * @param socket the stream socket
169  */
170 void
171 GNUNET_STREAM_close (struct GNUNET_STREAM_socket *socket);
172
173 #if 0
174 {
175 #endif
176 #ifdef __cplusplus
177 }
178 #endif
179
180 #endif