2 This file is part of GNUnet.
3 (C) 2011, Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_stream_lib.h
23 * @brief stream handling using mesh API
24 * @author Sree Harsha Totakura
27 #ifndef GNUNET_STREAM_LIB_H_
28 #define GNUNET_STREAM_LIB_H_
38 #include "gnunet_util_lib.h"
39 #include "gnunet_mesh_service.h"
44 enum GNUNET_STREAM_Status
47 * All previous read/write operations are successfully done
52 * A timeout occured while reading/writing the stream
54 GNUNET_STREAM_TIMEOUT = 1,
57 * A serious error occured while operating of this stream
59 GNUNET_STREAM_SYSERR = 2
63 * Opaque handler for stream
65 struct GNUNET_STREAM_socket;
68 * Functions of this type will be called when a stream is established
70 * @param cls the closure from GNUNET_STREAM_open
72 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls);
75 * Tries to open a stream to the target peer
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
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
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);
92 * Functions of this type are called upon new stream connection from other peers
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
98 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
99 * stream (the socket will be invalid after the call)
101 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
102 struct GNUNET_STREAM_socket *socket,
104 GNUNET_PeerIdentity *initiator);
107 * Listens for stream connections for a specific application ports
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
112 * @return GNUNET_OK if we are listening, GNUNET_SYSERR for any error
115 GNUNET_STREAM_listen (GNUNET_MESH_ApplicationType app_port,
116 GNUNET_STREAM_ListenCallback listen_cb,
120 * Functions of this signature are called whenever reading/writing operations
121 * on a stream are executed
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
127 typedef void (*GNUNET_STREAM_CompletionCallback) (void *cls,
128 enum GNUNET_STREAM_Status
133 * Tries to write the given data to the stream
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
143 GNUNET_STREAM_write (const struct GNUNET_STREAM_socket *socket,
146 GNUNET_STREAM_CompletionCallback write_cb,
147 struct GNUNET_TIME_Relative timeout,
151 * Tries to read data from the stream
153 * @param socket the socket representing a stream
154 * @param buffer the buffer into which the read data is stored
159 GNUNET_STREAM_read (const struct GNUNET_STREAM_socket *socket,
162 GNUNET_STREAM_CompletionCallback read_cb,
163 struct GNUNET_TIME_Relative timeout,
168 * @param socket the stream socket
171 GNUNET_STREAM_close (struct GNUNET_STREAM_socket *socket);