5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 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 #include "silcincludes.h"
22 #define SILC_IS_FD_STREAM(s) (s->ops == &silc_fd_stream_ops)
24 const SilcStreamOps silc_fd_stream_ops;
26 /* FD stream context */
28 const SilcStreamOps *ops;
29 SilcSchedule schedule;
30 SilcStreamNotifier notifier;
31 void *notifier_context;
37 /* The IO process callback that calls the notifier callback to upper layer. */
39 SILC_TASK_CALLBACK(silc_fd_stream_io)
41 SilcFDStream stream = context;
43 if (!stream->notifier)
48 stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
52 stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context);
60 /* Create file descriptor stream */
62 SilcStream silc_fd_stream_create(int fd, SilcSchedule schedule)
66 return silc_fd_stream_create2(fd, 0, schedule);
69 /* Create stream with two file descriptors */
71 SilcStream silc_fd_stream_create2(int read_fd, int write_fd,
72 SilcSchedule schedule)
79 stream = silc_calloc(1, sizeof(*stream));
83 SILC_LOG_DEBUG(("Creating new fd stream %p", stream));
85 stream->ops = &silc_fd_stream_ops;
86 stream->schedule = schedule;
87 stream->fd1 = read_fd;
88 stream->fd2 = write_fd;
90 /* Schedule the file descriptors */
92 silc_schedule_task_add_fd(schedule, write_fd, silc_fd_stream_io, stream);
93 silc_file_set_nonblock(write_fd);
96 silc_schedule_task_add_fd(schedule, read_fd, silc_fd_stream_io, stream);
97 silc_schedule_set_listen_fd(schedule, read_fd, SILC_TASK_READ, FALSE);
98 silc_file_set_nonblock(read_fd);
100 stream->fd2 = stream->fd1;
106 /* Create by opening file */
108 SilcStream silc_fd_stream_file(const char *filename,
109 SilcBool reading, SilcBool writing,
110 SilcSchedule schedule)
121 if (reading && writing)
124 fd = silc_file_open(filename, flags);
128 return silc_fd_stream_create(fd, schedule);
133 SilcBool silc_fd_stream_get_info(SilcStream stream, int *read_fd, int *write_fd)
135 SilcFDStream fd_stream = stream;
137 if (!SILC_IS_FD_STREAM(fd_stream))
141 *read_fd = fd_stream->fd1;
143 *write_fd = fd_stream->fd2;
150 int silc_fd_stream_get_error(SilcStream stream)
152 SilcFDStream fd_stream = stream;
154 if (!SILC_IS_FD_STREAM(fd_stream))
157 return fd_stream->error;
162 int silc_fd_stream_read(SilcStream stream, unsigned char *buf,
165 SilcFDStream fd_stream = stream;
168 if (!SILC_IS_FD_STREAM(fd_stream))
171 SILC_LOG_DEBUG(("Reading data from fd %d", fd_stream->fd1));
173 len = silc_file_read(fd_stream->fd1, buf, buf_len);
175 if (errno == EAGAIN || errno == EINTR) {
176 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
177 silc_schedule_set_listen_fd(fd_stream->schedule, fd_stream->fd1,
178 SILC_TASK_READ, FALSE);
181 SILC_LOG_DEBUG(("Cannot read from fd: %d:%s",
182 fd_stream->fd1, strerror(errno)));
183 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
184 fd_stream->error = errno;
188 SILC_LOG_DEBUG(("Read %d bytes", len));
191 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
198 int silc_fd_stream_write(SilcStream stream, const unsigned char *data,
201 SilcFDStream fd_stream = stream;
204 if (!SILC_IS_FD_STREAM(fd_stream))
207 SILC_LOG_DEBUG(("Writing data to fd %d", fd_stream->fd2));
209 ret = silc_file_write(fd_stream->fd2, data, data_len);
211 if (errno == EAGAIN || errno == EINTR) {
212 SILC_LOG_DEBUG(("Could not write immediately, will do it later"));
213 silc_schedule_set_listen_fd(fd_stream->schedule, fd_stream->fd2,
214 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
217 SILC_LOG_DEBUG(("Cannot write to fd: %s", strerror(errno)));
218 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
219 fd_stream->error = errno;
223 SILC_LOG_DEBUG(("Wrote data %d bytes", ret));
225 if (fd_stream->fd1 == fd_stream->fd2)
226 silc_schedule_set_listen_fd(fd_stream->schedule, fd_stream->fd2,
227 SILC_TASK_READ, FALSE);
229 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
236 SilcBool silc_fd_stream_close(SilcStream stream)
238 SilcFDStream fd_stream = stream;
240 if (!SILC_IS_FD_STREAM(fd_stream))
243 if (fd_stream->fd1 > 0) {
244 silc_file_close(fd_stream->fd1);
245 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
247 if (fd_stream->fd2 > 0 && fd_stream->fd2 != fd_stream->fd1) {
248 silc_file_close(fd_stream->fd2);
249 silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
257 void silc_fd_stream_destroy(SilcStream stream)
259 SilcFDStream fd_stream = stream;
261 if (!SILC_IS_FD_STREAM(fd_stream))
264 silc_fd_stream_close(stream);
265 silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1);
266 silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2);
270 /* Sets stream notification callback for the stream */
272 void silc_fd_stream_notifier(SilcStream stream,
273 SilcStreamNotifier callback,
276 SilcFDStream fd_stream = stream;
278 if (!SILC_IS_FD_STREAM(fd_stream))
281 SILC_LOG_DEBUG(("Setting stream notifier callback"));
283 fd_stream->notifier = callback;
284 fd_stream->notifier_context = context;
287 /* File descriptor stream operations */
288 const SilcStreamOps silc_fd_stream_ops =
291 silc_fd_stream_write,
292 silc_fd_stream_close,
293 silc_fd_stream_destroy,
294 silc_fd_stream_notifier,