src/nexus.c
author Tero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 10:16:28 +0200
changeset 10 9fe218576d13
parent 9 4c4c906cc649
child 11 14e79683c48c
permissions -rw-r--r--
fix sock_stream read/write return value, move line buffer inside of line_proto, add some initial code for event-based non-blocking operation

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <assert.h>

#include <err.h>
#include <event2/event.h>

#include "sock.h"
#include "line_proto.h"

#define CONNECT_HOST "irc.fixme.fi"
#define CONNECT_SERV "6697"
#define LINE_LENGTH 512

int main (int argc, char **argv) {
    struct event_base *ev_base;
    struct sock_stream *sock;
    struct line_proto *lp;
    char *line;
    struct error_info _err;

    // initialize libevent
    if ((ev_base = event_base_new()) == NULL)
        err(1, "event_base_new");

    // initialize sock module
    if (sock_init(ev_base, &_err))
        errx(1, "sock_init: %s", error_msg(&_err));

    // over-simplified connect
    if (sock_gnutls_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
        errx(1, "sock_gnutls_connect: %s", error_msg(&_err));

    // line protocol
    if (line_proto_create(&lp, sock, LINE_LENGTH, NULL, NULL, &_err))
        errx(1, "line_proto_create: %s", error_msg(&_err));

    // read lines and dump them out
    do {
        // recv
        if (line_proto_read(lp, &line))
            errx(1, "line_proto_read: %s", error_msg(line_proto_error(lp)));

        // printf
        printf("<<< %s\n", line);

    } while (1);
    
    // ok
    return 0;
}