memcache/connection.c
changeset 48 1c67f512779b
parent 46 8a832c0e01ee
child 49 10c7dce1a043
--- a/memcache/connection.c	Thu Aug 28 03:14:07 2008 +0300
+++ b/memcache/connection.c	Fri Aug 29 23:31:17 2008 +0300
@@ -11,6 +11,12 @@
 #include "../socket.h"
 #include "../common.h"
 
+
+void memcache_conn_send_req_data (struct memcache_conn *conn);
+void memcache_conn_finish_req_data (struct memcache_conn *conn);
+void memcache_conn_handle_reply (struct memcache_conn *conn);
+void memcache_conn_handle_reply_data (struct memcache_conn *conn, struct evbuffer *buf);
+
 static void _memcache_conn_ev_connect (evutil_socket_t fd, short what, void *arg);
 static void _memcache_conn_bev_write (struct bufferevent *bev, void *arg);
 static void _memcache_conn_bev_read (struct bufferevent *bev, void *arg);
@@ -85,7 +91,11 @@
     assert(conn->req == NULL);
 
     // write the request header into our bufferevent's output buffer
-    if (memcache_cmd_format_header(bufferevent_get_output(conn->bev), req->cmd_type, &req->key, &req->obj)) {
+    if (memcache_cmd_format_header(bufferevent_get_output(conn->bev), 
+        memcache_req_cmd(req),
+        memcache_req_key(req),
+        memcache_req_obj(req)
+    )) {
         ERROR("failed to init the cmd");
     }
     
@@ -114,11 +124,17 @@
  * Start writing out the request data
  */
 void memcache_conn_send_req_data (struct memcache_conn *conn) {
-    // set up the ev_write
-    event_set(&conn->ev_write, conn->fd, EV_WRITE, &_memcache_conn_ev_write, conn);
+    if (conn->req->obj.bytes > 0) {
+        // set up the ev_write
+        event_set(&conn->ev_write, conn->fd, EV_WRITE, &_memcache_conn_ev_write, conn);
 
-    // just fake a call to the event handler
-    _memcache_conn_ev_write(conn->fd, EV_WRITE, conn);
+        // just fake a call to the event handler
+        _memcache_conn_ev_write(conn->fd, EV_WRITE, conn);
+
+    } else {
+        // just send the \r\n
+        memcache_conn_finish_req_data(conn);
+    }
 }
 
 /*
@@ -167,6 +183,8 @@
 
     // bytes *may* be zero if we have an empty cache entry
     if (conn->req->obj.bytes > 0) {
+        // XXX: memcache_req_make_buffer?
+
         // allocate a buffer for the reply data
         if ((conn->req->buf.data = malloc(conn->req->obj.bytes)) == NULL)
             ERROR("malloc");
@@ -176,6 +194,10 @@
 
         // set offset to zero
         conn->req->buf.offset = 0;
+        
+        // and note that it is present, and is ours
+        conn->req->have_buf = 1;
+        conn->req->is_buf_ours = 1;
 
         // do we have any data in the buf that we need to copy?
         if (evbuffer_get_length(buf) > 0) {
@@ -453,7 +475,6 @@
     if (conn->req) {
         // error out the req
         memcache_req_error(conn->req);
-        assert(conn->req->conn == NULL);
         
         // we are now available again
         conn->req = NULL;
@@ -473,10 +494,9 @@
     // ensure that we do currently have a req
     assert(conn->req);
     
-    // have the req detach and check it did so
+    // have the req detach
     memcache_req_done(conn->req);
-    assert(conn->req->conn == NULL);
-    
+
     // we are now available again
     conn->req = NULL;