*queuing, and add missing file
authorTero Marttila <terom@fixme.fi>
Wed, 27 Aug 2008 10:13:38 +0300
changeset 40 9cecd22e643a
parent 39 0e21a65074a6
child 41 540737bf6bac
*queuing, and add missing file
memcache/request.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memcache/request.c	Wed Aug 27 10:13:38 2008 +0300
@@ -0,0 +1,67 @@
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "request.h"
+#include "memcache.h"
+#include "../common.h"
+
+struct memcache_req *memcache_req_alloc (struct memcache *mc, struct memcache_key *key, void *cb_arg) {
+    struct memcache_req *req = NULL;
+    
+    // allocate it
+    if ((req = calloc(1, sizeof(*req))) == NULL)
+        ERROR("calloc");
+    
+    // state
+    req->state = STATE_INVALID;
+
+    // copy the key
+    if ((req->key.buf = malloc(key->len)) == NULL)
+        ERROR("malloc key buf");
+    
+    // copy over the key
+    memcpy(req->key.buf, key->buf, key->len);
+    req->key.len = key->len;
+
+    // store the mc + callback argument
+    req->mc = mc;
+    req->cb_arg = cb_arg;
+
+    // success
+    return req;
+
+error:
+    if (req) {
+        free(req->key.buf);
+        free(req);
+    }
+
+    return NULL;
+}
+
+static int _memcache_req_notify (struct memcache_req *req) {
+    return req->mc->cb_fn(req, req->cb_arg);
+}
+
+void memcache_req_error (struct memcache_req *req) {
+    // forget our connection
+    req->conn = NULL;
+    
+    // enter ERROR state
+    req->state = STATE_ERROR;
+
+    // notify
+    if (_memcache_req_notify(req))
+        WARNING("req error callback failed, ignoring");
+}
+
+void memcache_req_free (struct memcache_req *req) {
+    // must be unused
+    assert(req->conn == NULL);
+    assert(req->state == STATE_INVALID || req->state == STATE_ERROR);
+
+    free(req->key.buf);
+    free(req);
+}
+