memcache/request.c
changeset 48 1c67f512779b
parent 46 8a832c0e01ee
equal deleted inserted replaced
47:a5c09677ca6f 48:1c67f512779b
    45         // ensure that it is a valid buffer
    45         // ensure that it is a valid buffer
    46         assert(buf->data && buf->len > 0 && buf->offset == buf->len);
    46         assert(buf->data && buf->len > 0 && buf->offset == buf->len);
    47 
    47 
    48         memcpy(&req->buf, buf, sizeof(req->buf));
    48         memcpy(&req->buf, buf, sizeof(req->buf));
    49         req->have_buf = 1;
    49         req->have_buf = 1;
       
    50         req->is_buf_ours = 0;
    50 
    51 
    51         // set offset to zero
    52         // set offset to zero
    52         req->buf.offset = 0;
    53         req->buf.offset = 0;
    53     }
    54     }
    54 
    55 
    68 
    69 
    69     return NULL;
    70     return NULL;
    70 }
    71 }
    71 
    72 
    72 // accessors
    73 // accessors
    73 enum memcache_req_state memcache_req_state (struct memcache_req *req) {
    74 enum memcache_state memcache_req_state (struct memcache_req *req) {
    74     return req->state;
    75     return req->state;
    75 }
    76 }
    76 
    77 
    77 enum memcache_command memcache_req_cmd (struct memcache_req *req) {
    78 enum memcache_command memcache_req_cmd (struct memcache_req *req) {
    78     return req->cmd_type;
    79     return req->cmd_type;
    80 
    81 
    81 enum memcache_reply memcache_req_reply (struct memcache_req *req) {
    82 enum memcache_reply memcache_req_reply (struct memcache_req *req) {
    82     return req->reply_type;
    83     return req->reply_type;
    83 }
    84 }
    84 
    85 
    85 const struct memcache_key *keymemcache_req_key (struct memcache_req *req) {
    86 const struct memcache_key *memcache_req_key (struct memcache_req *req) {
    86     return &req->key;
    87     return &req->key;
    87 }
    88 }
    88 
    89 
    89 const struct memcache_obj *memcache_req_obj (struct memcache_req *req) {
    90 const struct memcache_obj *memcache_req_obj (struct memcache_req *req) {
    90     return req->have_obj ? &req->obj : NULL;
    91     return req->have_obj ? &req->obj : NULL;
   110     
   111     
   111 //    _memcache_req_notify(req);
   112 //    _memcache_req_notify(req);
   112 }
   113 }
   113 
   114 
   114 void memcache_req_recv (struct memcache_req *req, enum memcache_reply reply_type) {
   115 void memcache_req_recv (struct memcache_req *req, enum memcache_reply reply_type) {
   115     req->state = MEMCACHE_STATE_REPLY;
   116     // set state to REPLY_DATA/REPLY based on have_buf/is_buf_ours
       
   117     req->state = (req->have_buf && req->is_buf_ours) ? MEMCACHE_STATE_REPLY_DATA : MEMCACHE_STATE_REPLY;
   116     req->reply_type = reply_type;
   118     req->reply_type = reply_type;
   117 
   119 
   118     // we must surely have a valid obj now
   120     // we must surely have a valid obj now
   119     req->have_obj = 1;
   121     req->have_obj = 1;
   120 
   122 
   134 
   136 
   135 void memcache_req_done (struct memcache_req *req) {
   137 void memcache_req_done (struct memcache_req *req) {
   136     // make sure we are in the REPLY/REPLY_DATA state
   138     // make sure we are in the REPLY/REPLY_DATA state
   137     assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA);
   139     assert(req->state == MEMCACHE_STATE_REPLY || req->state == MEMCACHE_STATE_REPLY_DATA);
   138     
   140     
   139     // are we supposed to have data?
   141     // set state to DONE_DATA/DONE based on have_buf/is_buf_ours
   140     if (req->buf.data) {
   142     req->state = (req->have_buf && req->is_buf_ours) ? MEMCACHE_STATE_DONE_DATA : MEMCACHE_STATE_DONE;
   141         // make sure we really have the full data, if applicable
       
   142         assert(req->buf.offset == req->buf.len);
       
   143         
       
   144         // yes...
       
   145         req->have_buf = 1;
       
   146 
       
   147         // have data
       
   148         req->state = MEMCACHE_STATE_DATA_DONE;
       
   149 
       
   150     } else {
       
   151         // no data
       
   152         req->state = MEMCACHE_STATE_DONE;
       
   153     }
       
   154 
   143 
   155     // forget the connection
   144     // forget the connection
   156     req->conn = NULL;
   145     req->conn = NULL;
   157     
   146     
   158     _memcache_req_notify(req);
   147     _memcache_req_notify(req);
   168 }
   157 }
   169 
   158 
   170 void memcache_req_free (struct memcache_req *req) {
   159 void memcache_req_free (struct memcache_req *req) {
   171     // must be unused
   160     // must be unused
   172     assert(req->conn == NULL);
   161     assert(req->conn == NULL);
   173     assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR || req->state == MEMCACHE_STATE_DONE || req->state == MEMCACHE_STATE_DATA_DONE);
   162     assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR || req->state == MEMCACHE_STATE_DONE || req->state == MEMCACHE_STATE_DONE_DATA);
       
   163     
       
   164     if (req->have_buf && req->is_buf_ours)
       
   165         free(req->buf.data);
   174 
   166 
   175     free(req->key.buf);
   167     free(req->key.buf);
   176     free(req);
   168     free(req);
   177 }
   169 }
   178 
   170