5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/Stream Interface
24 * SILC Stream API is a generic representation of a stream. A common API
25 * is defined that can be used to read from and write to the stream. Any
26 * other stream API derived from this API can use this same interface for
27 * reading and writing.
29 * The SilcStream is an abstraction. It can represent any stream; socket
30 * stream, file descriptor stream, packet stream, etc. See examples in
31 * silcsocketstream.h and silcfdstream.h.
33 * Note that stream implementations usually are not thread-safe. Always
34 * verify whether a stream implementation is thread-safe by checking their
35 * corresponding documentation.
42 /****s* silcutil/SilcStream
46 * typedef void *SilcStream;
50 * Abstact stream context representing any stream. All streams are using
51 * this abstraction so that the stream can be accessed using the standard
52 * silc_stream_* functions. All streams are destroyed by calling the
53 * silc_stream_destroy function.
56 typedef void *SilcStream;
58 /****d* silcutil/SilcStreamStatus
62 * typedef enum { ... } SilcStreamStatus;
66 * Stream status. This status is returned into the SilcStreamNotifier
67 * callback function to indicate the status of the stream at a given
73 SILC_STREAM_CAN_READ, /* Data available for reading */
74 SILC_STREAM_CAN_WRITE, /* Stream ready for writing */
78 /****f* silcutil/SilcStreamNotifier
82 * typedef void (*SilcStreamNotifier)(SilcStream stream,
83 * SilcStreamStatus status,
88 * A callback of this type is called as stream notifier to notify of a
89 * certain action taken over the stream. This is called to notify for
90 * example that data is ready for reading, or writing or that end of
94 typedef void (*SilcStreamNotifier)(SilcStream stream,
95 SilcStreamStatus status,
98 /****s* silcutil/SilcStreamOps
102 * typedef struct { ... } SilcStreamOps;
106 * SILC Stream operations structure. This structure includes callback
107 * functions to the actual stream implementation. Any stream that
108 * use SILC Stream abstraction must fill this structure with the actual
109 * stream implementation.
111 * Each stream implementation MUST set this structure as the first field
112 * in their stream structure. As it is that structure that is passed
113 * to the silc_stream_* routines, the SILC Stream API expects that the
114 * SilcStream context starts with this structure.
119 * const SilcStreamOps *ops;
120 * ... other stuff ...
124 * silc_stream_write((SilcStream)foo, data, data_len);
129 /* This is called to read data from the stream. This is called when
130 silc_stream_read function was called. */
131 int (*read)(SilcStream stream, unsigned char *buf, SilcUInt32 buf_len);
133 /* This is called when writing data to the stream. This is called when
134 silc_stream_write function was called. */
135 int (*write)(SilcStream stream, const unsigned char *data,
136 SilcUInt32 data_len);
138 /* This is called to close the stream. This is called when the
139 silc_stream_close function was called. */
140 SilcBool (*close)(SilcStream stream);
142 /* This is called to destroy the stream. This is called when the
143 silc_stream_destroy function was called. */
144 void (*destroy)(SilcStream stream);
146 /* This is called to set a notifier callback to the stream and schedule
147 the stream. Stream should not be scheduled before calling this
148 function. If stream does not need scheduler then the scheduler can
149 be ignored. This is called when silc_stream_set_notifier was called.
150 Returns FALSE if the stream could not be scheduled. */
151 SilcBool (*notifier)(SilcStream stream, SilcSchedule schedule,
152 SilcStreamNotifier callback, void *context);
154 /* This is called to return the associated scheduler, if set. This is
155 called when silc_stream_get_schedule was called. */
156 SilcSchedule (*get_schedule)(SilcStream stream);
160 /****f* silcutil/silc_stream_read
164 * int silc_stream_read(SilcStream stream, unsigned char *buf,
165 * SilcUInt32 buf_len);
169 * Reads data from the stream indicated by `stream' into the data buffer
170 * indicated by `buf' which is size of `buf_len'. This returns the amount
171 * of data read, zero (0) if end of stream occurred, -1 if data could
172 * not be read at this moment, or -2 if error occurred. If -1 is returned
173 * the notifier callback will later be called with SILC_STREAM_CAN_READ
174 * status when stream is again ready for reading.
176 * If error occurred the error code can be retrieved with silc_errno.
179 int silc_stream_read(SilcStream stream, unsigned char *buf,
182 /****f* silcutil/silc_stream_write
186 * int silc_stream_write(SilcStream stream, const unsigned char *data,
187 * SilcUInt32 data_len);
191 * Writes `data_len' bytes of data to the stream indicated by `stream' from
192 * data buffer indicated by `data'. Returns the amount of data written,
193 * zero (0) if end of stream occurred, -1 if data could not be written
194 * at this moment, or -2 if error occurred. If -1 is returned the
195 * notifier callback will later be called with SILC_STREAM_CAN_WRITE
196 * status when stream is again ready for writing.
198 * If error occurred the error code can be retrieved with silc_errno.
201 int silc_stream_write(SilcStream stream, const unsigned char *data,
202 SilcUInt32 data_len);
204 /****f* silcutil/silc_stream_close
208 * SilcBool silc_stream_close(SilcStream stream);
212 * Closes the stream indicated by `stream'. No data can be read or written
213 * to the stream after calling this function. Return TRUE if the stream
214 * could be closed. If action is taken on closed stream the notifier
215 * callback may be called with an error status.
218 SilcBool silc_stream_close(SilcStream stream);
220 /****f* silcutil/silc_stream_destroy
224 * void silc_stream_destroy(SilcStream stream);
228 * Destroy the stream indicated by `stream'. The `stream' will become
229 * invalid after this function returns. All streams are destroyed by
230 * calling this function. The silc_stream_close should be called
231 * before calling this function. However, if it is not called this
232 * function will call it.
235 void silc_stream_destroy(SilcStream stream);
237 /****f* silcutil/silc_stream_set_notifier
241 * SilcBool silc_stream_set_notifier(SilcStream stream,
242 * SilcSchedule schedule,
243 * SilcStreamNotifier notifier,
248 * Schedule `stream' for stream events. Set the `notifier' callback to
249 * be called when some event takes place on the stream. The event will
250 * be delievered to the `notifier' callback with the `context'. It is
251 * called for example when data is available for reading or writing, or
252 * if an error occurs. This can be called at any time for valid stream.
253 * This call will also set the `stream' into non-blocking mode.
255 * If `notifier' is set to NULL no callback will be called for the stream,
256 * and the stream is not scheduled anymore.
258 * This function returns FALSE if the stream could not be scheduled.
259 * Returns TRUE on success. The `schedule' must always be non-NULL.
262 SilcBool silc_stream_set_notifier(SilcStream stream, SilcSchedule schedule,
263 SilcStreamNotifier notifier, void *context);
265 /****f* silcutil/silc_stream_get_schedule
269 * SilcSchedule silc_stream_get_schedule(SilcStream stream);
273 * Returns the scheduler that has been associated with the `stream', or
274 * NULL if one has not been set for the `stream'.
277 SilcSchedule silc_stream_get_schedule(SilcStream stream);
279 #endif /* SILCSTREAM_H */