ustream: another fix for ustream_read_buf_full() with split read buffers
[project/libubox.git] / ustream.h
index 29f370f..6431744 100644 (file)
--- a/ustream.h
+++ b/ustream.h
@@ -52,7 +52,7 @@ struct ustream {
        struct ustream *next;
 
        /*
-        * notify_read:
+        * notify_read: (optional)
         * called by the ustream core to notify that new data is available
         * for reading.
         * must not free the ustream from this callback
@@ -68,7 +68,7 @@ struct ustream {
        void (*notify_write)(struct ustream *s, int bytes);
 
        /*
-        * notify_state:
+        * notify_state: (optional)
         * called by the ustream implementation to notify that the read
         * side of the stream is closed (eof is set) or there was a write
         * error (write_error is set).
@@ -101,6 +101,14 @@ struct ustream {
        void (*set_read_blocked)(struct ustream *s);
 
        /*
+        * poll: (optional)
+        * defined by the upstream implementation, called to request polling for
+        * available data.
+        * returns true if data was fetched.
+        */
+       bool (*poll)(struct ustream *s);
+
+       /*
         * ustream user should set this if the input stream is expected
         * to contain string data. the core will keep all data 0-terminated.
         */
@@ -155,6 +163,19 @@ static inline bool ustream_read_blocked(struct ustream *s)
        return !!(s->read_blocked & READ_BLOCKED_USER);
 }
 
+static inline int ustream_pending_data(struct ustream *s, bool write)
+{
+       struct ustream_buf_list *b = write ? &s->w : &s->r;
+       return b->data_bytes;
+}
+
+static inline bool ustream_read_buf_full(struct ustream *s)
+{
+       struct ustream_buf *buf = s->r.data_tail;
+       return buf && buf->data == buf->head && buf->tail == buf->end &&
+              s->r.buffers == s->r.max_buffers;
+}
+
 /*** --- functions only used by ustream implementations --- ***/
 
 /* ustream_init_defaults: fill default callbacks and options */
@@ -182,4 +203,12 @@ static inline void ustream_state_change(struct ustream *s)
        uloop_timeout_set(&s->state_change, 0);
 }
 
+static inline bool ustream_poll(struct ustream *s)
+{
+       if (!s->poll)
+               return false;
+
+       return s->poll(s);
+}
+
 #endif