iobuffer.h revision 647afe054117804e3e156b46ada4c5bec0ac4c38
#ifndef __IOBUFFER_H
#define __IOBUFFER_H
#include "ioloop.h"
#define IO_BUFFER_MIN_SIZE 512
struct _IOBuffer {
int fd;
int buf_errno; /* set when write() or read() failed. */
/* private: */
int refcount;
int priority;
int timeout_msecs;
void *timeout_context;
void *flush_context;
unsigned char *buffer;
next call to io_buffer_next_line() */
};
/* Create an I/O buffer. It can be used for either sending or receiving data,
NEVER BOTH AT SAME TIME. */
int autoclose_fd);
/* Read the file by mmap()ing it in blocks. stop_offset specifies where to
stop reading, or 0 to end of file. */
int autoclose_fd);
/* Reference counting. References start from 1, so calling io_buffer_unref()
destroys the buffer if io_buffer_ref() is never used. */
The data already in buffer can be used, and the remaining output buffer
will be sent. */
/* Reset all pointers so that the buffer looks empty, the actual data is
not touched and can be used. */
/* Change the memory pool used by the buffer. Data already in
buffer will be transferred to new buffer. */
/* Change the maximum size for buffer to grow. */
/* Change buffer's blocking state. The blocking state in fd itself isn't
changed, and it's not needed to be blocking. This affects two things:
When buffer reaches max_size, it will block until all the data has been
sent or timeout has been reached. Setting max_size to 0 disables this
(default). Setting timeout_msecs to 0 may block infinitely, or until
socket is closed.
Sets the timeout for io_buffer_read_blocking(). If max_size is non-zero,
it acts the same as io_buffer_set_max_size().
timeout_func is called with both cases when timeout occurs.
*/
void *context);
/* Set TCP_CORK on if supported, ie. don't send out partial frames.
io_buffer_send_flush() removes the cork. */
/* Returns 1 if all was ok, -1 if disconnected, -2 if buffer is full */
/* Send data from input buffer to output buffer using the fastest
possible method. Returns 1 if all sent or -1 if disconnected.
Note that this function may block. */
/* Flush the output buffer, blocks until all is sent. If
io_buffer_set_send_blocking() is called, it's timeout settings are used. */
/* Call specified function when the whole transmit buffer has been sent.
If the buffer is empty already, the function will be called immediately.
The function will be called only once. */
void *context);
/* Change the start_offset and call io_buffer_reset(). Doesn't do anything
if offset is the same as existing start_offset. */
/* IO buffer won't be read past specified offset. Giving 0 as offset removes
the limit. */
/* Returns number of bytes read if read was ok,
-1 if disconnected / EOF, -2 if the buffer is full */
/* Blocking read, doesn't return until at least one byte is read, or until
socket is disconnected or timeout has occured. Note that the fd must be
nonblocking, or the timeout doesn't work. Returns number of bytes read
(never 0), -1 if error or -2 if buffer is full. */
/* Skip forward a number of bytes */
/* Seek to specified position from beginning of file. This works only for
files. Returns TRUE if successful. */
/* Returns the next line from input buffer, or NULL if more data is needed
to make a full line. NOTE: call to io_buffer_read() invalidates the
returned data. */
/* Returns pointer to beginning of data in buffer,
or NULL if there's no data. */
/* Like io_buffer_get_data(), but read it when needed. Returns 1 if more
than threshold bytes were stored into buffer, 0 if less, -1 if error or
EOF with no bytes in buffer or -2 if buffer is full. */
/* Returns a pointer to buffer wanted amount of space,
or NULL if size is too big. */
/* Send data saved to buffer from io_buffer_get_space().
Returns -1 if disconnected. */
/* Put data to buffer as if it was received.
Returns 1 if successful, -2 if buffer isn't big enough. */
/* Returns TRUE if there's nothing in buffer. */
#endif