src/sock_internal.h
author Tero Marttila <terom@fixme.fi>
Thu, 16 Apr 2009 01:20:09 +0300
changeset 139 55b9dcc2b73a
parent 118 05b8d5150313
child 155 c59d3eaff0fb
permissions -rw-r--r--
implement sock_ssl_connect_async (the old sock_ssl_connect exists no more)
#ifndef SOCK_INTERNAL_H
#define SOCK_INTERNAL_H

/**
 * @file
 *
 * internal sock_* interface
 */
#include "sock.h"
#include <stdbool.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, struct error_info *err);

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

    /** 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 type's connect_cb handler, defaults to just invoke conn_cb_func */
    void (*_conn_cb) (struct sock_stream *sock, struct error_info *err);
};

/**
 * 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
 * @param cb_func the optional connect_async callback function
 * @param cb_arg the optional context argument for cb_func
 */
void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type, sock_stream_connect_cb cb_func, void *cb_arg);

/**
 * 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);

/**
 * Invoke the sock_stream_conn_cb callback with the given error param.
 *
 * This invokes the sock_stream_methods::_conn_cb if present and \a direct is not given, otherwise the callback directly
 *
 * @param direct force the conn_cb to be called directly
 */
void sock_stream_invoke_conn_cb (struct sock_stream *sock, struct error_info *err, bool direct);

#endif /* SOCK_INTERNAL_H */