memcache/request.c
changeset 41 540737bf6bac
parent 40 9cecd22e643a
child 42 0e503189af2f
equal deleted inserted replaced
40:9cecd22e643a 41:540737bf6bac
     4 
     4 
     5 #include "request.h"
     5 #include "request.h"
     6 #include "memcache.h"
     6 #include "memcache.h"
     7 #include "../common.h"
     7 #include "../common.h"
     8 
     8 
     9 struct memcache_req *memcache_req_alloc (struct memcache *mc, struct memcache_key *key, void *cb_arg) {
     9 struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, void *cb_arg) {
    10     struct memcache_req *req = NULL;
    10     struct memcache_req *req = NULL;
    11     
    11     
    12     // allocate it
    12     // allocate it
    13     if ((req = calloc(1, sizeof(*req))) == NULL)
    13     if ((req = calloc(1, sizeof(*req))) == NULL)
    14         ERROR("calloc");
    14         ERROR("calloc");
    15     
    15     
    16     // state
    16     // state
    17     req->state = STATE_INVALID;
    17     req->state = MEMCACHE_STATE_INVALID;
    18 
    18 
    19     // copy the key
    19     // copy the key
    20     if ((req->key.buf = malloc(key->len)) == NULL)
    20     if ((req->key.buf = malloc(key->len)) == NULL)
    21         ERROR("malloc key buf");
    21         ERROR("malloc key buf");
    22     
    22     
    23     // copy over the key
    23     // copy over the key
    24     memcpy(req->key.buf, key->buf, key->len);
    24     memcpy(req->key.buf, key->buf, key->len);
    25     req->key.len = key->len;
    25     req->key.len = key->len;
    26 
    26 
    27     // store the mc + callback argument
    27     // store the other data
    28     req->mc = mc;
    28     req->mc = mc;
       
    29     req->cmd_type = cmd_type;
    29     req->cb_arg = cb_arg;
    30     req->cb_arg = cb_arg;
    30 
    31 
    31     // success
    32     // success
    32     return req;
    33     return req;
    33 
    34 
    47 void memcache_req_error (struct memcache_req *req) {
    48 void memcache_req_error (struct memcache_req *req) {
    48     // forget our connection
    49     // forget our connection
    49     req->conn = NULL;
    50     req->conn = NULL;
    50     
    51     
    51     // enter ERROR state
    52     // enter ERROR state
    52     req->state = STATE_ERROR;
    53     req->state = MEMCACHE_STATE_ERROR;
    53 
    54 
    54     // notify
    55     // notify
    55     if (_memcache_req_notify(req))
    56     if (_memcache_req_notify(req))
    56         WARNING("req error callback failed, ignoring");
    57         WARNING("req error callback failed, ignoring");
    57 }
    58 }
    58 
    59 
    59 void memcache_req_free (struct memcache_req *req) {
    60 void memcache_req_free (struct memcache_req *req) {
    60     // must be unused
    61     // must be unused
    61     assert(req->conn == NULL);
    62     assert(req->conn == NULL);
    62     assert(req->state == STATE_INVALID || req->state == STATE_ERROR);
    63     assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR);
    63 
    64 
    64     free(req->key.buf);
    65     free(req->key.buf);
    65     free(req);
    66     free(req);
    66 }
    67 }
    67 
    68