]> code.delx.au - pulseaudio/blobdiff - src/iochannel.c
fix modargs memory leak
[pulseaudio] / src / iochannel.c
index aa7de714e4e8df0aa43eee547b7f5c2d7cf52b84..3843003460b84dd8c8d47918adc16cb6bb35a23a 100644 (file)
@@ -1,57 +1,69 @@
+#include <stdlib.h>
 #include <assert.h>
 #include <fcntl.h>
-#include <stdlib.h>
 #include <unistd.h>
 
 #include "iochannel.h"
+#include "util.h"
+#include "socket-util.h"
 
-struct iochannel {
+struct pa_iochannel {
     int ifd, ofd;
-    struct mainloop* mainloop;
+    struct pa_mainloop_api* mainloop;
 
-    void (*callback)(struct iochannel*io, void *userdata);
+    void (*callback)(struct pa_iochannel*io, void *userdata);
     void*userdata;
     
     int readable;
     int writable;
+    int hungup;
+    
+    int no_close;
 
-    struct mainloop_source* input_source, *output_source;
+    void* input_source, *output_source;
 };
 
-static void enable_mainloop_sources(struct iochannel *io) {
+static void enable_mainloop_sources(struct pa_iochannel *io) {
     assert(io);
 
     if (io->input_source == io->output_source) {
-        enum mainloop_io_event e = MAINLOOP_IO_EVENT_NULL;
+        enum pa_mainloop_api_io_events e = PA_MAINLOOP_API_IO_EVENT_NULL;
         assert(io->input_source);
         
         if (!io->readable)
-            e |= MAINLOOP_IO_EVENT_IN;
+            e |= PA_MAINLOOP_API_IO_EVENT_INPUT;
         if (!io->writable)
-            e |= MAINLOOP_IO_EVENT_OUT;
+            e |= PA_MAINLOOP_API_IO_EVENT_OUTPUT;
 
-        mainloop_source_io_set_events(io->input_source, e);
+        io->mainloop->enable_io(io->mainloop, io->input_source, e);
     } else {
         if (io->input_source)
-            mainloop_source_io_set_events(io->input_source, io->readable ? MAINLOOP_IO_EVENT_NULL : MAINLOOP_IO_EVENT_IN);
+            io->mainloop->enable_io(io->mainloop, io->input_source, io->readable ? PA_MAINLOOP_API_IO_EVENT_NULL : PA_MAINLOOP_API_IO_EVENT_INPUT);
         if (io->output_source)
-            mainloop_source_io_set_events(io->output_source, io->writable ? MAINLOOP_IO_EVENT_NULL : MAINLOOP_IO_EVENT_OUT);
+            io->mainloop->enable_io(io->mainloop, io->output_source, io->writable ? PA_MAINLOOP_API_IO_EVENT_NULL : PA_MAINLOOP_API_IO_EVENT_OUTPUT);
     }
 }
 
-static void callback(struct mainloop_source*s, int fd, enum mainloop_io_event events, void *userdata) {
-    struct iochannel *io = userdata;
-    int changed;
-    assert(s && fd >= 0 && userdata);
+static void callback(struct pa_mainloop_api* m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
+    struct pa_iochannel *io = userdata;
+    int changed = 0;
+    assert(m && fd >= 0 && events && userdata);
 
-    if (events & MAINLOOP_IO_EVENT_IN && !io->readable) {
+    if ((events & PA_MAINLOOP_API_IO_EVENT_HUP) && !io->hungup) {
+        io->hungup = 1;
+        changed = 1;
+    }
+    
+    if ((events & PA_MAINLOOP_API_IO_EVENT_INPUT) && !io->readable) {
         io->readable = 1;
         changed = 1;
+        assert(id == io->input_source);
     }
     
-    if (events & MAINLOOP_IO_EVENT_OUT && !io->writable) {
+    if ((events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) && !io->writable) {
         io->writable = 1;
         changed = 1;
+        assert(id == io->output_source);
     }
 
     if (changed) {
@@ -62,19 +74,11 @@ static void callback(struct mainloop_source*s, int fd, enum mainloop_io_event ev
     }
 }
 
-static void make_nonblock_fd(int fd) {
-    int v;
-
-    if ((v = fcntl(fd, F_GETFL)) >= 0)
-        if (!(v & O_NONBLOCK))
-            fcntl(fd, F_SETFL, v|O_NONBLOCK);
-}
-
-struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) {
-    struct iochannel *io;
+struct pa_iochannel* pa_iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd) {
+    struct pa_iochannel *io;
     assert(m && (ifd >= 0 || ofd >= 0));
 
-    io = malloc(sizeof(struct iochannel));
+    io = malloc(sizeof(struct pa_iochannel));
     io->ifd = ifd;
     io->ofd = ofd;
     io->mainloop = m;
@@ -83,22 +87,24 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) {
     io->callback = NULL;
     io->readable = 0;
     io->writable = 0;
+    io->hungup = 0;
+    io->no_close = 0;
 
     if (ifd == ofd) {
         assert(ifd >= 0);
-        make_nonblock_fd(io->ifd);
-        io->input_source = io->output_source = mainloop_source_new_io(m, ifd, MAINLOOP_IO_EVENT_IN|MAINLOOP_IO_EVENT_OUT, callback, io);
+        pa_make_nonblock_fd(io->ifd);
+        io->input_source = io->output_source = m->source_io(m, ifd, PA_MAINLOOP_API_IO_EVENT_BOTH, callback, io);
     } else {
 
         if (ifd >= 0) {
-            make_nonblock_fd(io->ifd);
-            io->input_source = mainloop_source_new_io(m, ifd, MAINLOOP_IO_EVENT_IN, callback, io);
+            pa_make_nonblock_fd(io->ifd);
+            io->input_source = m->source_io(m, ifd, PA_MAINLOOP_API_IO_EVENT_INPUT, callback, io);
         } else
             io->input_source = NULL;
 
         if (ofd >= 0) {
-            make_nonblock_fd(io->ofd);
-            io->output_source = mainloop_source_new_io(m, ofd, MAINLOOP_IO_EVENT_OUT, callback, io);
+            pa_make_nonblock_fd(io->ofd);
+            io->output_source = m->source_io(m, ofd, PA_MAINLOOP_API_IO_EVENT_OUTPUT, callback, io);
         } else
             io->output_source = NULL;
     }
@@ -106,33 +112,40 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) {
     return io;
 }
 
-void iochannel_free(struct iochannel*io) {
+void pa_iochannel_free(struct pa_iochannel*io) {
     assert(io);
 
-    if (io->ifd >= 0)
-        close(io->ifd);
-    if (io->ofd >= 0 && io->ofd != io->ifd)
-        close(io->ofd);
+    if (!io->no_close) {
+        if (io->ifd >= 0)
+            close(io->ifd);
+        if (io->ofd >= 0 && io->ofd != io->ifd)
+            close(io->ofd);
+    }
 
     if (io->input_source)
-        mainloop_source_free(io->input_source);
-    if (io->output_source)
-        mainloop_source_free(io->output_source);
+        io->mainloop->cancel_io(io->mainloop, io->input_source);
+    if (io->output_source && (io->output_source != io->input_source))
+        io->mainloop->cancel_io(io->mainloop, io->output_source);
     
     free(io);
 }
 
-int iochannel_is_readable(struct iochannel*io) {
+int pa_iochannel_is_readable(struct pa_iochannel*io) {
     assert(io);
     return io->readable;
 }
 
-int iochannel_is_writable(struct iochannel*io) {
+int pa_iochannel_is_writable(struct pa_iochannel*io) {
     assert(io);
     return io->writable;
 }
 
-ssize_t iochannel_write(struct iochannel*io, const void*data, size_t l) {
+int pa_iochannel_is_hungup(struct pa_iochannel*io) {
+    assert(io);
+    return io->hungup;
+}
+
+ssize_t pa_iochannel_write(struct pa_iochannel*io, const void*data, size_t l) {
     ssize_t r;
     assert(io && data && l && io->ofd >= 0);
 
@@ -144,11 +157,11 @@ ssize_t iochannel_write(struct iochannel*io, const void*data, size_t l) {
     return r;
 }
 
-ssize_t iochannel_read(struct iochannel*io, void*data, size_t l) {
+ssize_t pa_iochannel_read(struct pa_iochannel*io, void*data, size_t l) {
     ssize_t r;
     
-    assert(io && data && l && io->ifd >= 0);
-
+    assert(io && data && io->ifd >= 0);
+    
     if ((r = read(io->ifd, data, l)) >= 0) {
         io->readable = 0;
         enable_mainloop_sources(io);
@@ -157,8 +170,28 @@ ssize_t iochannel_read(struct iochannel*io, void*data, size_t l) {
     return r;
 }
 
-void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochannel*io, void *userdata), void *userdata) {
+void pa_iochannel_set_callback(struct pa_iochannel*io, void (*callback)(struct pa_iochannel*io, void *userdata), void *userdata) {
     assert(io);
     io->callback = callback;
     io->userdata = userdata;
 }
+
+void pa_iochannel_set_noclose(struct pa_iochannel*io, int b) {
+    assert(io);
+    io->no_close = b;
+}
+
+void pa_iochannel_socket_peer_to_string(struct pa_iochannel*io, char*s, size_t l) {
+    assert(io && s && l);
+    pa_socket_peer_to_string(io->ifd, s, l);
+}
+
+int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel *io, size_t l) {
+    assert(io);
+    return pa_socket_set_rcvbuf(io->ifd, l);
+}
+
+int pa_iochannel_socket_set_sndbuf(struct pa_iochannel *io, size_t l) {
+    assert(io);
+    return pa_socket_set_sndbuf(io->ofd, l);
+}