src/sock_internal.h
author Tero Marttila <terom@fixme.fi>
Tue, 10 Mar 2009 02:34:11 +0200
changeset 28 9c1050bc8709
parent 12 4147fae232d9
child 85 75bc8b164ef8
permissions -rw-r--r--
add sock_stream_release/line_proto_release/irc_conn_release functions, and add proper cleanup to irc_net_create
#ifndef SOCK_INTERNAL_H
#define SOCK_INTERNAL_H

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

/**
 * Global config related to all sock_streams
 */
extern struct sock_stream_ctx {
    /* Event core */
    struct event_base *ev_base;

} _sock_stream_ctx;

/**
 * The base type struct, which defines the method table.
 */
struct sock_stream_type {
    /** Method table */
    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);
    } 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;

    /* Callbacks */
    const struct sock_stream_callbacks *cb_info;

    /* Callback arg */
    void *cb_arg;
};

#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);

/*
 * Invoke the appropriate callbacks for the given EV_* bitmask.
 */
void sock_stream_invoke_callbacks (struct sock_stream *sock, short what);

#endif /* SOCK_INTERNAL_H */