src/line_proto.h
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 160 4f8dc89d7cbb
permissions -rw-r--r--
nexus.c compiles
#ifndef LINE_PROTO_H
#define LINE_PROTO_H

/**
 * @file
 *
 * Support for protocols that send/receive lines
 */
#include "transport.h"
#include "error.h"

/**
 * The line_proto state handle
 */
struct line_proto;

/**
 * User callbacks for event-based line_proto behaviour
 */
struct line_proto_callbacks {
    /** Handle received line */
    void (*on_line) (char *line, void *arg);
    
    /** Transport failed, the line_proto is corrupt, you should call line_proto_release next. */
    void (*on_error) (const error_t *err, 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.
 *
 * In case of errors, \a transport will be destroyed in any case.
 *
 * @param lp_ptr a pointer to the new line_proto will be returned via this pointer
 * @param transport the connected transport to use
 * @param buf_size the incoming/outgoing buffer size, should be enough to hold the biggest possible line
 * @param callbacks the callbacks to use, a copy is stored
 * @param cb_arg the read_cb callback argument
 * @param err error information is returned via this pointer
 */
err_t line_proto_create (struct line_proto **lp_ptr, transport_t *transport, size_t buf_size,
        const struct line_proto_callbacks *callbacks, void *cb_arg, error_t *err);

/**
 * Runs transport_read() with 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.
 *
 * @param line_ptr a pointer to the received line is returned via this pointer
 */
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 unsent. 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.
 *
 * @param line pointer to buffer containing \r\n\0 terminated line
 */
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.
 *
 * It is a bug to call this if there is no data waiting to be sent.
 */
int line_proto_flush (struct line_proto *lp);

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

/**
 * Destroy any buffers and the underlying transport.
 *
 * This does not close the connection cleanly, and is intended for use to abort after errors.
 */
void line_proto_destroy (struct line_proto *lp);

#endif /* LINE_PROTO_H */