silc_stream_set_notifier and silc_schedule_set_listen_fd now
[silc.git] / lib / silcutil / silcfdstream.c
index db9bd9d724185d0396b454b5857d301eb7790c84..67ad3269c7f514d2e1ee1f5ccbc59b81877ee92c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 Pekka Riikonen
+  Copyright (C) 2005 - 2006 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -19,6 +19,8 @@
 
 #include "silc.h"
 
+/************************** Types and definitions ***************************/
+
 #define SILC_IS_FD_STREAM(s) (s->ops == &silc_fd_stream_ops)
 
 const SilcStreamOps silc_fd_stream_ops;
@@ -34,6 +36,9 @@ typedef struct {
   int error;
 } *SilcFDStream;
 
+
+/************************ Static utility functions **************************/
+
 /* The IO process callback that calls the notifier callback to upper layer. */
 
 SILC_TASK_CALLBACK(silc_fd_stream_io)
@@ -44,19 +49,22 @@ SILC_TASK_CALLBACK(silc_fd_stream_io)
     return;
 
   switch (type) {
-  case SILC_TASK_WRITE:
-    stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
-    break;
-
   case SILC_TASK_READ:
     stream->notifier(stream, SILC_STREAM_CAN_READ, stream->notifier_context);
     break;
 
+  case SILC_TASK_WRITE:
+    stream->notifier(stream, SILC_STREAM_CAN_WRITE, stream->notifier_context);
+    break;
+
   default:
     break;
   }
 }
 
+
+/****************************** Public API **********************************/
+
 /* Create file descriptor stream */
 
 SilcStream silc_fd_stream_create(int fd)
@@ -103,7 +111,7 @@ SilcStream silc_fd_stream_file(const char *filename,
   if (writing)
     flags |= O_CREAT | O_WRONLY;
   if (reading && writing)
-    flags |= O_CREAT | O_RDWR;
+    flags = O_CREAT | O_RDWR;
 
   fd = silc_file_open(filename, flags);
   if (fd < 0)
@@ -150,8 +158,6 @@ int silc_fd_stream_read(SilcStream stream, unsigned char *buf,
   SilcFDStream fd_stream = stream;
   int len = 0;
 
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return -2;
   if (!fd_stream->notifier)
     return -2;
 
@@ -188,8 +194,6 @@ int silc_fd_stream_write(SilcStream stream, const unsigned char *data,
   SilcFDStream fd_stream = stream;
   int ret;
 
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return -2;
   if (!fd_stream->notifier)
     return -2;
 
@@ -226,16 +230,15 @@ SilcBool silc_fd_stream_close(SilcStream stream)
 {
   SilcFDStream fd_stream = stream;
 
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return FALSE;
-
   if (fd_stream->fd1 > 0) {
     silc_file_close(fd_stream->fd1);
     silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
+    silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1);
   }
   if (fd_stream->fd2 > 0 && fd_stream->fd2 != fd_stream->fd1) {
     silc_file_close(fd_stream->fd2);
     silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
+    silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2);
   }
 
   return TRUE;
@@ -245,29 +248,19 @@ SilcBool silc_fd_stream_close(SilcStream stream)
 
 void silc_fd_stream_destroy(SilcStream stream)
 {
-  SilcFDStream fd_stream = stream;
-
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return;
-
   silc_fd_stream_close(stream);
-  silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1);
-  silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2);
   silc_free(stream);
 }
 
 /* Sets stream notification callback for the stream */
 
-void silc_fd_stream_notifier(SilcStream stream,
-                            SilcSchedule schedule,
-                            SilcStreamNotifier callback,
-                            void *context)
+SilcBool silc_fd_stream_notifier(SilcStream stream,
+                                SilcSchedule schedule,
+                                SilcStreamNotifier callback,
+                                void *context)
 {
   SilcFDStream fd_stream = stream;
 
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return;
-
   SILC_LOG_DEBUG(("Setting stream notifier callback"));
 
   fd_stream->notifier = callback;
@@ -293,7 +286,11 @@ void silc_fd_stream_notifier(SilcStream stream,
   } else {
     silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd1);
     silc_schedule_unset_listen_fd(fd_stream->schedule, fd_stream->fd2);
+    silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd1);
+    silc_schedule_task_del_by_fd(fd_stream->schedule, fd_stream->fd2);
   }
+
+  return TRUE;
 }
 
 /* Return schedule */
@@ -301,10 +298,6 @@ void silc_fd_stream_notifier(SilcStream stream,
 SilcSchedule silc_fd_stream_get_schedule(SilcStream stream)
 {
   SilcFDStream fd_stream = stream;
-
-  if (!SILC_IS_FD_STREAM(fd_stream))
-    return NULL;
-
   return fd_stream->schedule;
 }