src/sock.c
author Tero Marttila <terom@fixme.fi>
Sat, 28 Feb 2009 17:39:37 +0200
changeset 11 14e79683c48c
parent 10 9fe218576d13
child 12 4147fae232d9
permissions -rw-r--r--
working event-based operation for sock_tcp

#include "sock_internal.h"
#include "sock_gnutls.h"

#include <assert.h>

// global sock_stream_ctx instance
struct sock_stream_ctx _sock_stream_ctx;

err_t sock_init (struct event_base *ev_base, struct error_info *err)
{
    // store ev_base
    _sock_stream_ctx.ev_base = ev_base;

    // XXX: just call these all directly for now
    if (sock_gnutls_global_init(err))
        return ERROR_CODE(err);

    // done
    return SUCCESS;
}

void sock_stream_init (struct sock_stream *sock, struct sock_stream_type *type)
{
    // be strict
    assert(sock->type == NULL);

    // store type
    sock->type = type;
}

int sock_stream_read (struct sock_stream *sock, void *buf, size_t len)
{
    err_t err;

    // proxy off to method handler
    if ((err = sock->type->methods.read(sock, buf, &len)))
        return -err;
    
    // return updated bytes-read len
    return len;
}

int sock_stream_write (struct sock_stream *sock, const void *buf, size_t len)
{
    err_t err;

    // proxy off to method handler
    if ((err = sock->type->methods.write(sock, buf, &len)))
        return -err;

    // return updated bytes-written len
    return len;
}

err_t sock_stream_event_init (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg)
{
    // store
    sock->cb_info = callbacks;
    sock->cb_arg = arg;

    // run method
    return sock->type->methods.event_init(sock);
}

err_t sock_stream_event_enable (struct sock_stream *sock, short mask)
{
    // run method
    return sock->type->methods.event_enable(sock, mask);
}

const struct error_info* sock_stream_error (struct sock_stream *sock)
{
    // return pointer
    return SOCK_ERR(sock);
}