ostream.h revision 5238111c460098d9cc8cc22527026138a278b9a4
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef __OSTREAM_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define __OSTREAM_H
402f9183489c1a75736b1e9068c33fe2741a366dTimo Sirainen
402f9183489c1a75736b1e9068c33fe2741a366dTimo Sirainen#include "ioloop.h"
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainenstruct ostream {
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen uoff_t offset;
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen int stream_errno;
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen unsigned int closed:1;
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen
5dd05e966ffd69181ab3067f6939b03ced68ebc3Timo Sirainen struct _ostream *real_stream;
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen};
402f9183489c1a75736b1e9068c33fe2741a366dTimo Sirainen
402f9183489c1a75736b1e9068c33fe2741a366dTimo Sirainen/* Returns 1 if all data is sent (not necessarily flushed), 0 if not.
Pretty much the only real reason to return 0 is if you wish to send more
data to client which isn't buffered, eg. o_stream_send_istream(). */
typedef int stream_flush_callback_t(void *context);
/* Create new output stream from given file descriptor.
If max_buffer_size is 0, an "optimal" buffer size is used (max 128kB). */
struct ostream *
o_stream_create_file(int fd, pool_t pool, size_t max_buffer_size,
int autoclose_fd);
/* Reference counting. References start from 1, so calling o_stream_unref()
destroys the stream if o_stream_ref() is never used. */
void o_stream_ref(struct ostream *stream);
void o_stream_unref(struct ostream *stream);
/* Mark the stream closed. Nothing will be sent after this call. */
void o_stream_close(struct ostream *stream);
/* Set IO_WRITE callback. Default will just try to flush the output and
finishes when the buffer is empty. */
void o_stream_set_flush_callback(struct ostream *stream,
stream_flush_callback_t *callback,
void *context);
/* Change the maximum size for stream's output buffer to grow. */
void o_stream_set_max_buffer_size(struct ostream *stream, size_t max_size);
/* Delays sending as far as possible, writing only full buffers. Also sets
TCP_CORK on if supported. */
void o_stream_cork(struct ostream *stream);
void o_stream_uncork(struct ostream *stream);
/* Flush the output stream, blocks until everything is sent.
Returns 1 if ok, -1 if error. */
int o_stream_flush(struct ostream *stream);
/* Set "flush pending" state of stream. If set, the flush callback is called
when more data is allowed to be sent, even if the buffer itself is empty. */
void o_stream_set_flush_pending(struct ostream *stream, int set);
/* Returns number of bytes currently in buffer. */
size_t o_stream_get_buffer_used_size(struct ostream *stream);
/* Seek to specified position from beginning of file. This works only for
files. Returns 1 if successful, -1 if error. */
int o_stream_seek(struct ostream *stream, uoff_t offset);
/* Returns number of bytes sent, -1 = error */
ssize_t o_stream_send(struct ostream *stream, const void *data, size_t size);
ssize_t o_stream_sendv(struct ostream *stream, const struct const_iovec *iov,
size_t iov_count);
ssize_t o_stream_send_str(struct ostream *stream, const char *str);
/* Send data from input stream. Returns number of bytes sent, or -1 if error.
Note that this function may block if either instream or outstream is
blocking.
It's also possible to use this function to copy data within same file
descriptor. If the file must be grown, you have to do it manually before
calling this function. */
off_t o_stream_send_istream(struct ostream *outstream,
struct istream *instream);
#endif