src/sock_internal.h
author Tero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 06:52:55 +0200
changeset 4 a3ca0f97a075
parent 3 cc94ae754e2a
child 9 4c4c906cc649
permissions -rw-r--r--
change ERROR_* to use pointers again, and implement error_info for sock_init
#ifndef SOCK_INTERNAL_H
#define SOCK_INTERNAL_H

#include "sock.h"

/*
 * The base type struct, which defines the method table.
 */
struct sock_stream_type {
    /* method table */
    struct sock_stream_methods {
        /* Normal read(2) */
        err_t (*read) (struct sock_stream *sock, void *buf, size_t len);

        /* Normal write(2) */
        err_t (*write) (struct sock_stream *sock, const void *buf, size_t len);

    } methods;
};

/*
 * The base sock_stream type, as used by the sock_stream_* functions.
 *
 * The specific implementations should embed this at the start of their type-specific struct, and then cast around
 * as appropriate.
 */
struct sock_stream {
    /* The sock_stream_type for this socket */
    struct sock_stream_type *type;

    /* Last error info */
    struct error_info err;
};

#define SOCK_FROM_BASE(sock, type) ((type*) sock)
#define SOCK_ERR(sock) (&(sock)->err)

/*
 * Initialize a sock_stream with the given sock_stream_type.
 *
 * The sock_stream should be initialized to zero. It is a bug to call this twice.
 */
void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type);

#endif /* SOCK_INTERNAL_H */