add sock_stream_callbacks and ev_base
authorTero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 08:48:21 +0200
changeset 9 4c4c906cc649
parent 8 be88e543c8ff
child 10 9fe218576d13
add sock_stream_callbacks and ev_base
Makefile
src/nexus.c
src/sock.c
src/sock.h
src/sock_gnutls.c
src/sock_internal.h
--- a/Makefile	Sun Feb 22 08:21:22 2009 +0200
+++ b/Makefile	Sun Feb 22 08:48:21 2009 +0200
@@ -16,8 +16,8 @@
 FIXED_CFLAGS = -Wall -std=gnu99
 
 # libevent
-LIBEVENT_CFLAGS = 
-LIBEVENT_LDFLAGS = -levent
+LIBEVENT_CFLAGS = -I/home/terom/opt/include
+LIBEVENT_LDFLAGS = -L/home/terom/opt/lib  -levent_core
 
 # GnuTLS stuff
 GNUTLS_NAME = "gnutls"
--- a/src/nexus.c	Sun Feb 22 08:21:22 2009 +0200
+++ b/src/nexus.c	Sun Feb 22 08:48:21 2009 +0200
@@ -7,6 +7,7 @@
 #include <assert.h>
 
 #include <err.h>
+#include <event2/event.h>
 
 #include "sock.h"
 #include "line_proto.h"
@@ -16,22 +17,27 @@
 #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_buf[LINE_LENGTH + 1];
-    struct error_info err;
+    struct error_info _err;
 
-    // initialize
-    if (sock_init(&err))
-        errx(1, "sock_init: %s", error_msg(&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));
+    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, &err))
-        errx(1, "line_proto_create: %s", error_msg(&err));
+    if (line_proto_create(&lp, sock, &_err))
+        errx(1, "line_proto_create: %s", error_msg(&_err));
 
     // read lines and dump them out
     do {
--- a/src/sock.c	Sun Feb 22 08:21:22 2009 +0200
+++ b/src/sock.c	Sun Feb 22 08:48:21 2009 +0200
@@ -4,8 +4,14 @@
 
 #include <assert.h>
 
-err_t sock_init (struct error_info *err)
+// 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);
@@ -23,6 +29,13 @@
     sock->type = type;
 }
 
+void sock_stream_set_callbacks (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg)
+{
+    // store
+    sock->cb_info = callbacks;
+    sock->cb_arg = arg;
+}
+
 err_t sock_stream_read (struct sock_stream *sock, void *buf, size_t len)
 {
     // proxy off to method handler
--- a/src/sock.h	Sun Feb 22 08:21:22 2009 +0200
+++ b/src/sock.h	Sun Feb 22 08:48:21 2009 +0200
@@ -6,6 +6,7 @@
  */
 #include "error.h"
 #include <sys/types.h>
+#include <event2/event.h>
 
 /*
  * The generic socket handle
@@ -13,9 +14,22 @@
 struct sock_stream;
 
 /*
+ * Async callbacks for socket operation
+ */
+struct sock_stream_callbacks {
+    /* Sockeet is readable */
+    void (*on_read)(struct sock_stream *sock, void *arg);
+
+    /* Socket is writeable */
+    void (*on_write)(struct sock_stream *sock, void *arg);
+};
+
+/*
  * Initialize the socket module's global state. Call this before calling any other sock_* functions.
+ *
+ * The given \a ev_base is the libevent base to use for nonblocking operation.
  */
-err_t sock_init (struct error_info *err);
+err_t sock_init (struct event_base *ev_base, struct error_info *err);
 
 /*
  * A simple blocking TCP connect to the given host/service, using getaddrinfo. The connected socket is returned via
@@ -37,6 +51,11 @@
 err_t sock_gnutls_connect (struct sock_stream **sock_ptr, const char *host, const char *service, struct error_info *err);
 
 /*
+ * Set the callbacks for a sock_stream. Note that the callbacks struct isn't copied - it's used as-is-given.
+ */
+void sock_stream_set_callbacks (struct sock_stream *sock, const struct sock_stream_callbacks *callbacks, void *arg);
+
+/*
  * The generic read/write API for stream sockets.
  */
 err_t sock_stream_read (struct sock_stream *sock, void *buf, size_t len);
--- a/src/sock_gnutls.c	Sun Feb 22 08:21:22 2009 +0200
+++ b/src/sock_gnutls.c	Sun Feb 22 08:48:21 2009 +0200
@@ -4,14 +4,6 @@
 #include <stdlib.h>
 #include <err.h>
 
-static void _sock_gnutls_error (struct sock_gnutls *sock, const char *func, int _err) {
-    if (_err == GNUTLS_E_FATAL_ALERT_RECEIVED)
-        errx(1, "%s: %s: %s", func, gnutls_strerror(_err), gnutls_alert_get_name(gnutls_alert_get(sock->session)));
-
-    else
-        errx(1, "%s: %s", func, gnutls_strerror(_err));
-}
-
 static err_t sock_gnutls_read (struct sock_stream *base_sock, void *buf, size_t len)
 {
     struct sock_gnutls *sock = SOCK_FROM_BASE(base_sock, struct sock_gnutls);
--- a/src/sock_internal.h	Sun Feb 22 08:21:22 2009 +0200
+++ b/src/sock_internal.h	Sun Feb 22 08:48:21 2009 +0200
@@ -4,6 +4,15 @@
 #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 {
@@ -30,6 +39,12 @@
 
     /* 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)