memcache/request.c
changeset 44 03a7e064f833
parent 43 e5b714190dee
child 46 8a832c0e01ee
--- a/memcache/request.c	Thu Aug 28 00:29:39 2008 +0300
+++ b/memcache/request.c	Thu Aug 28 01:34:14 2008 +0300
@@ -6,8 +6,11 @@
 #include "memcache.h"
 #include "../common.h"
 
-struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, void *cb_arg) {
+struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, const struct memcache_obj *obj, const struct memcache_buf *buf, void *cb_arg) {
     struct memcache_req *req = NULL;
+
+    // ensure key is provided
+    assert(key != NULL);
     
     // allocate it
     if ((req = calloc(1, sizeof(*req))) == NULL)
@@ -20,9 +23,20 @@
     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;
+    
+    // copy the obj if provided
+    if (obj) {
+        memcpy(&req->obj, obj, sizeof(req->obj));
+        req->have_obj = 1;
+    }
+
+    // copy the buf if provided
+    if (buf) {
+        memcpy(&req->buf, buf, sizeof(req->buf));
+        req->have_buf = 1;
+    }
 
     // store the other data
     req->mc = mc;
@@ -41,6 +55,32 @@
     return NULL;
 }
 
+// accessors
+enum memcache_req_state memcache_req_state (struct memcache_req *req) {
+    return req->state;
+}
+
+enum memcache_command memcache_req_cmd (struct memcache_req *req) {
+    return req->cmd_type;
+}
+
+enum memcache_reply memcache_req_reply (struct memcache_req *req) {
+    return req->reply_type;
+}
+
+const struct memcache_key *keymemcache_req_key (struct memcache_req *req) {
+    return &req->key;
+}
+
+const struct memcache_obj *memcache_req_obj (struct memcache_req *req) {
+    return req->have_obj ? &req->obj : NULL;
+}
+
+const struct memcache_buf *memcache_req_buf (struct memcache_req *req) {
+    return req->have_buf ? &req->buf : NULL;
+}
+
+// events
 static void _memcache_req_notify (struct memcache_req *req) {
     req->mc->cb_fn(req, req->cb_arg);
 }
@@ -57,10 +97,13 @@
 //    _memcache_req_notify(req);
 }
 
-void memcache_req_reply (struct memcache_req *req, enum memcache_reply reply_type) {
+void memcache_req_recv (struct memcache_req *req, enum memcache_reply reply_type) {
     req->state = MEMCACHE_STATE_REPLY;
     req->reply_type = reply_type;
 
+    // we must surely have a valid obj now
+    req->have_obj = 1;
+
     _memcache_req_notify(req);
 }
 
@@ -68,6 +111,9 @@
     assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA);
 
     req->state = MEMCACHE_STATE_REPLY_DATA;
+
+    // we must surely have a valid buf now
+    req->have_buf = 1;
     
     _memcache_req_notify(req);
 }
@@ -75,15 +121,27 @@
 void memcache_req_done (struct memcache_req *req) {
     // make sure we are in the REPLY/REPLY_DATA state
     assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA);
+    
+    // are we supposed to have data?
+    if (req->buf.data) {
+        // make sure we really have the full data, if applicable
+        assert(req->buf.offset == req->buf.len);
+        
+        // yes...
+        req->have_buf = 1;
 
-    // make sure we really have the full data, if applicable
-    assert(req->buf.data == NULL || req->buf.offset == req->buf.len);
+        // have data
+        req->state = MEMCACHE_STATE_DATA_DONE;
+
+    } else {
+        // no data
+        req->state = MEMCACHE_STATE_DONE;
+    }
 
     // forget the connection
     req->conn = NULL;
     
-    // state depends on if we have data or not...
-    req->state = req->buf.data ? MEMCACHE_STATE_DATA_DONE : MEMCACHE_STATE_DONE;
+    _memcache_req_notify(req);
 }
 
 void memcache_req_error (struct memcache_req *req) {