author | Tero Marttila <terom@fixme.fi> |
Wed, 27 Aug 2008 21:30:32 +0300 | |
changeset 41 | 540737bf6bac |
parent 40 | 9cecd22e643a |
child 42 | 0e503189af2f |
permissions | -rw-r--r-- |
40 | 1 |
|
2 |
#include <stdlib.h> |
|
3 |
#include <assert.h> |
|
4 |
||
5 |
#include "request.h" |
|
6 |
#include "memcache.h" |
|
7 |
#include "../common.h" |
|
8 |
||
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
9 |
struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, void *cb_arg) { |
40 | 10 |
struct memcache_req *req = NULL; |
11 |
||
12 |
// allocate it |
|
13 |
if ((req = calloc(1, sizeof(*req))) == NULL) |
|
14 |
ERROR("calloc"); |
|
15 |
||
16 |
// state |
|
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
17 |
req->state = MEMCACHE_STATE_INVALID; |
40 | 18 |
|
19 |
// copy the key |
|
20 |
if ((req->key.buf = malloc(key->len)) == NULL) |
|
21 |
ERROR("malloc key buf"); |
|
22 |
||
23 |
// copy over the key |
|
24 |
memcpy(req->key.buf, key->buf, key->len); |
|
25 |
req->key.len = key->len; |
|
26 |
||
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
27 |
// store the other data |
40 | 28 |
req->mc = mc; |
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
29 |
req->cmd_type = cmd_type; |
40 | 30 |
req->cb_arg = cb_arg; |
31 |
||
32 |
// success |
|
33 |
return req; |
|
34 |
||
35 |
error: |
|
36 |
if (req) { |
|
37 |
free(req->key.buf); |
|
38 |
free(req); |
|
39 |
} |
|
40 |
||
41 |
return NULL; |
|
42 |
} |
|
43 |
||
44 |
static int _memcache_req_notify (struct memcache_req *req) { |
|
45 |
return req->mc->cb_fn(req, req->cb_arg); |
|
46 |
} |
|
47 |
||
48 |
void memcache_req_error (struct memcache_req *req) { |
|
49 |
// forget our connection |
|
50 |
req->conn = NULL; |
|
51 |
||
52 |
// enter ERROR state |
|
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
53 |
req->state = MEMCACHE_STATE_ERROR; |
40 | 54 |
|
55 |
// notify |
|
56 |
if (_memcache_req_notify(req)) |
|
57 |
WARNING("req error callback failed, ignoring"); |
|
58 |
} |
|
59 |
||
60 |
void memcache_req_free (struct memcache_req *req) { |
|
61 |
// must be unused |
|
62 |
assert(req->conn == NULL); |
|
41
540737bf6bac
sending requests, and partial support for receiving -- incomplete, not tested
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
63 |
assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR); |
40 | 64 |
|
65 |
free(req->key.buf); |
|
66 |
free(req); |
|
67 |
} |
|
68 |