src/line_proto.h
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 22:50:08 +0200
changeset 45 71e65564afd2
parent 41 40f7aa051acb
child 87 f0db6ebf18b9
permissions -rw-r--r--
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#ifndef LINE_PROTO_H
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#define LINE_PROTO_H
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
     4
/**
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
     5
 * @file
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
     6
 *
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
 * Support for protocols that send/receive lines
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
 */
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
#include "sock.h"
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
#include "error.h"
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    12
/**
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    13
 * The line_proto state handle
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
 */
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
struct line_proto;
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    17
/**
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    18
 * User callback list
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    19
 */
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    20
struct line_proto_callbacks {
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    21
    /** Handle received line */
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    22
    void (*on_line) (char *line, void *arg);
33
e5139b339b18 add line_proto_callbacks.on_error, although irc_conn doesn't pass it up
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    23
    
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    24
    /** Transport failed, the line_proto is corrupt, you should call line_proto_release next. */
33
e5139b339b18 add line_proto_callbacks.on_error, although irc_conn doesn't pass it up
Tero Marttila <terom@fixme.fi>
parents: 32
diff changeset
    25
    void (*on_error) (struct error_info *err, void *arg);
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    26
};
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    27
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    28
/**
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
 * Create a new line_proto off the the given sock_stream. The newly allocated line_proto will be returned via *lp_ptr.
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    30
 *
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    31
 * The incoming lines are buffered in a buffer of \a buf_size bytes. This imposes a maximum limit on the line length.
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    32
 *
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
    33
 * Note that the given callbacks struct is copied.
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
    34
 *
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    35
 * @param lp_ptr a pointer to the new line_proto will be returned via this pointer
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    36
 * @param sock the sock_stream to use
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    37
 * @param buf_size the incoming/outgoing buffer size, should be enough to hold the biggest possible line
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 33
diff changeset
    38
 * @param callbacks the callbacks to use, a copy is stored
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    39
 * @param cb_arg the read_cb callback argument
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    40
 * @param err error information is returned via this pointer
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
 */
10
9fe218576d13 fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    42
err_t line_proto_create (struct line_proto **lp_ptr, struct sock_stream *sock, size_t buf_size,
32
ae66e9ae4afb convert line_proto to use a line_proto_callbacks struct
Tero Marttila <terom@fixme.fi>
parents: 28
diff changeset
    43
        const struct line_proto_callbacks *callbacks, void *cb_arg, struct error_info *err);
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    45
/**
11
14e79683c48c working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    46
 * Runs the socket recv() into our internal buffer. If a full line was received, a pointer to our internal bufffer is
14e79683c48c working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    47
 * returned via *line_ptr, and we return SUCCESS. If we don't yet have a full line, and receiving more would block,
14e79683c48c working event-based operation for sock_tcp
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
    48
 * NULL is returned via *line_ptr instead. Otherwise, nonzero error return code.
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    49
 *
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    50
 * @param line_ptr a pointer to the received line is returned via this pointer
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
 */
19
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    52
err_t line_proto_recv (struct line_proto *lp, char **line_ptr);
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    54
/**
18
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    55
 * Write a single line to the sock_stream, buffering any incomplete fragment that remains unset. Returns zero if the
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    56
 * line was succesfully sent, >0 if it was only partially sent, or -err on errors.
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    57
 *
dedf137b504f add initial irc_conn code that can register
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
    58
 * The given line should already include the terminating '\r\n' character sequence.
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    59
 *
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    60
 * @param line pointer to buffer containing \r\n\0 terminated line
12
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
    61
 */
19
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    62
int line_proto_send (struct line_proto *lp, const char *line);
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    63
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    64
/**
19
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    65
 * Flush out any buffered line fragment. Returns zero if the buffer was flushed empty, >0 if there's still fragments
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    66
 * remaining, or -err on errors.
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    67
 */
8c80580ccde9 improve line_proto output buffering slightly
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
    68
int line_proto_flush (struct line_proto *lp);
12
4147fae232d9 update sock_stream_read/write semantics for EOF/EAGAIN, tentative event-based gnutls code
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
    69
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    70
/**
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
 * Get current error_info*
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
 */
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
const struct error_info* line_proto_error (struct line_proto *lp);
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
28
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    75
/**
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    76
 * Release any allocated buffers, and the underlying sock_stream.
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    77
 *
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    78
 * This does not close the connection cleanly, and is intended for use to abort after errors.
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    79
 */
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    80
void line_proto_release (struct line_proto *lp);
9c1050bc8709 add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
Tero Marttila <terom@fixme.fi>
parents: 19
diff changeset
    81
8
be88e543c8ff split off line_proto, and make sock_stream_error return a const error_info
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
#endif /* LINE_PROTO_H */