src/sock_internal.h
author Tero Marttila <terom@fixme.fi>
Mon, 30 Mar 2009 01:31:27 +0300
changeset 87 f0db6ebf18b9
parent 85 75bc8b164ef8
child 118 05b8d5150313
permissions -rw-r--r--
documentation tweaks
#ifndef SOCK_INTERNAL_H
#define SOCK_INTERNAL_H

/**
 * @file
 *
 * internal sock_* interface
 */
#include "sock.h"

/**
 * General state for all sock_stream's
 */
struct sock_stream_ctx {
    /** libevent core */
    struct event_base *ev_base;

};

/**
 * Global sock_stream_ctx used for sock_init() and all sock_stream's
 */
extern struct sock_stream_ctx _sock_stream_ctx;

/**
 * Socket implementation type methods
 */
struct sock_stream_methods {
    /** As read(2) */
    err_t (*read) (struct sock_stream *sock, void *buf, size_t *len);

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

    /** Initialize events. cb_info/cb_arg are already updated */
    err_t (*event_init) (struct sock_stream *sock);

    /** Enable events as specified */
    err_t (*event_enable) (struct sock_stream *sock, short mask);
    
    /** Release all resources and free the sock_stream */
    void (*release) (struct sock_stream *sock);
};

/**
 * The base type struct, which defines the method table.
 */
struct sock_stream_type {
    /** Method table */
    struct sock_stream_methods 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, XXX: remove this */
    struct error_info err;

    /** Callbacks */
    const struct sock_stream_callbacks *cb_info;

    /** Callback arg */
    void *cb_arg;

    /** Connection callback function */
    sock_stream_connect_cb conn_cb_func;

    /** Connection callback context argument */
    void *conn_cb_arg;
};

/**
 * Convert a `struct sock_stream*` to the given type.
 */
#define SOCK_FROM_BASE(sock, type) ((type*) sock)

/**
 * Get a pointer to the sock_stream's error_info field.
 */
#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.
 *
 * @param sock the new sock_stream
 * @param type the sock_stream_type defining the implementation used
 */
void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type);

/**
 * Invoke the appropriate callbacks for the given EV_* bitmask.
 *
 * @param sock the sock_stream
 * @param what combination of EV_* bits describing what callbacks to invoke
 */
void sock_stream_invoke_callbacks (struct sock_stream *sock, short what);

#endif /* SOCK_INTERNAL_H */