#ifndef __USTREAM_H
#define __USTREAM_H
#include <stdarg.h>
#include "uloop.h"
struct ustream;
struct ustream_buf;
enum read_blocked_reason {
READ_BLOCKED_USER = (1 << 0),
READ_BLOCKED_FULL = (1 << 1),
};
struct ustream_buf_list {
struct ustream_buf *head;
struct ustream_buf *data_tail;
struct ustream_buf *tail;
int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
int data_bytes;
int min_buffers;
int max_buffers;
int buffer_len;
int buffers;
};
struct ustream {
struct ustream_buf_list r, w;
struct uloop_timeout state_change;
struct ustream *next;
void (*notify_read)(struct ustream *s, int bytes_new);
void (*notify_write)(struct ustream *s, int bytes);
void (*notify_state)(struct ustream *s);
int (*write)(struct ustream *s, const char *buf, int len, bool more);
void (*free)(struct ustream *s);
void (*set_read_blocked)(struct ustream *s);
bool (*poll)(struct ustream *s);
bool string_data;
bool write_error;
bool eof, eof_write_done;
enum read_blocked_reason read_blocked;
};
struct ustream_fd {
struct ustream stream;
struct uloop_fd fd;
};
struct ustream_buf {
struct ustream_buf *next;
char *data;
char *tail;
char *end;
char head[];
};
void ustream_fd_init(struct ustream_fd *s, int fd);
void ustream_free(struct ustream *s);
void ustream_consume(struct ustream *s, int len);
int ustream_read(struct ustream *s, char *buf, int buflen);
int ustream_write(struct ustream *s, const char *buf, int len, bool more);
int ustream_printf(struct ustream *s, const char *format, ...)
__attribute__ ((format (printf, 2, 3)));
int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
__attribute__ ((format (printf, 2, 0)));
char *ustream_get_read_buf(struct ustream *s, int *buflen);
void ustream_set_read_blocked(struct ustream *s, bool set);
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;
}
void ustream_init_defaults(struct ustream *s);
char *ustream_reserve(struct ustream *s, int len, int *maxlen);
void ustream_fill_read(struct ustream *s, int len);
bool ustream_write_pending(struct ustream *s);
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