src/line_proto.h
author Tero Marttila <terom@fixme.fi>
Sat, 28 Feb 2009 23:48:34 +0200
changeset 19 8c80580ccde9
parent 18 dedf137b504f
child 28 9c1050bc8709
permissions -rw-r--r--
improve line_proto output buffering slightly
#ifndef LINE_PROTO_H
#define LINE_PROTO_H

/*
 * Support for protocols that send/receive lines
 */
#include "sock.h"
#include "error.h"

/*
 * The state handle
 */
struct line_proto;

/*
 * The callback for receiving lines
 */
typedef void (*line_proto_read_cb)(char *line, void *arg);

/*
 * Create a new line_proto off the the given sock_stream. The newly allocated line_proto will be returned via *lp_ptr.
 *
 * The incoming lines are buffered in a buffer of \a buf_size bytes. This imposes a maximum limit on the line length.
 *
 * The given callback function/argument will be used to provide event-based recv support.
 */
err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
        line_proto_read_cb cb_func, void *cb_arg, struct error_info *err);

/*
 * Runs the socket recv() into our internal buffer. If a full line was received, a pointer to our internal bufffer is
 * returned via *line_ptr, and we return SUCCESS. If we don't yet have a full line, and receiving more would block,
 * NULL is returned via *line_ptr instead. Otherwise, nonzero error return code.
 */
err_t line_proto_recv (struct line_proto *lp, char **line_ptr);

/*
 * Write a single line to the sock_stream, buffering any incomplete fragment that remains unset. Returns zero if the
 * line was succesfully sent, >0 if it was only partially sent, or -err on errors.
 *
 * The given line should already include the terminating '\r\n' character sequence.
 */
int line_proto_send (struct line_proto *lp, const char *line);

/*
 * Flush out any buffered line fragment. Returns zero if the buffer was flushed empty, >0 if there's still fragments
 * remaining, or -err on errors.
 */
int line_proto_flush (struct line_proto *lp);

/*
 * Get current error_info*
 */
const struct error_info* line_proto_error (struct line_proto *lp);

#endif /* LINE_PROTO_H */