This and previous patch are for writing server response into
non-blocking GIOChannel when we have G_IO_OUT signal.
---
 gatchat/gatserver.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/gatchat/gatserver.c b/gatchat/gatserver.c
index b5ac91e..1b4ce78 100644
--- a/gatchat/gatserver.c
+++ b/gatchat/gatserver.c
@@ -530,6 +530,73 @@ static gboolean received_data(GIOChannel *channel, 
GIOCondition cond,
 static gboolean can_write_data(GIOChannel *channel, GIOCondition cond,
                                gpointer data)
 {
+       GAtServer *server = data;
+       GIOError err;
+       gsize bytes_written;
+       gsize towrite;
+       struct ring_buffer *write_buf;
+       unsigned char *buf;
+       gboolean write_again = FALSE;
+#ifdef WRITE_SCHEDULER_DEBUG
+       int limiter;
+#endif
+
+       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
+               return FALSE;
+
+       /* If we have full buffer, write them out first */
+       if (server->full_list)
+               write_buf = server->full_list->data;
+       else
+               write_buf = server->write_buf;
+
+       buf = ring_buffer_read_ptr(write_buf, 0);
+
+       towrite = ring_buffer_len_no_wrap(write_buf);
+       if (towrite < (gsize)ring_buffer_len(write_buf))
+               write_again = TRUE;
+
+#ifdef WRITE_SCHEDULER_DEBUG
+       limiter = towrite;
+
+       if (limiter > 5)
+               limiter = 5;
+#endif
+
+       err = g_io_channel_write(server->channel,
+                       (char *)buf,
+#ifdef WRITE_SCHEDULER_DEBUG
+                       limiter,
+#else
+                       towrite,
+#endif
+                       &bytes_written);
+
+       if (err != G_IO_ERROR_NONE) {
+               g_source_remove(server->read_watch);
+               return FALSE;
+       }
+
+       g_at_util_debug_chat(FALSE, (char *)buf, bytes_written, server->debugf,
+                               server->debug_data);
+
+       ring_buffer_read_advance(write_buf, bytes_written);
+
+       if (server->full_list) {
+               /* All data in current full list is written, free it */
+               if (ring_buffer_len(write_buf) == 0) {
+                       server->full_list = g_slist_remove(server->full_list,
+                                                               write_buf);
+
+                       ring_buffer_free(write_buf);
+               }
+
+               return TRUE;
+       }
+
+       if (bytes_written < towrite || write_again == TRUE)
+               return TRUE;
+
        return FALSE;
 }
 
-- 
1.6.6.1

_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono

Reply via email to