8 #include "connection.h" |
8 #include "connection.h" |
9 #include "command.h" |
9 #include "command.h" |
10 #include "request.h" |
10 #include "request.h" |
11 #include "../socket.h" |
11 #include "../socket.h" |
12 #include "../common.h" |
12 #include "../common.h" |
|
13 |
|
14 |
|
15 void memcache_conn_send_req_data (struct memcache_conn *conn); |
|
16 void memcache_conn_finish_req_data (struct memcache_conn *conn); |
|
17 void memcache_conn_handle_reply (struct memcache_conn *conn); |
|
18 void memcache_conn_handle_reply_data (struct memcache_conn *conn, struct evbuffer *buf); |
13 |
19 |
14 static void _memcache_conn_ev_connect (evutil_socket_t fd, short what, void *arg); |
20 static void _memcache_conn_ev_connect (evutil_socket_t fd, short what, void *arg); |
15 static void _memcache_conn_bev_write (struct bufferevent *bev, void *arg); |
21 static void _memcache_conn_bev_write (struct bufferevent *bev, void *arg); |
16 static void _memcache_conn_bev_read (struct bufferevent *bev, void *arg); |
22 static void _memcache_conn_bev_read (struct bufferevent *bev, void *arg); |
17 static void _memcache_conn_bev_error (struct bufferevent *bev, short what, void *arg); |
23 static void _memcache_conn_bev_error (struct bufferevent *bev, short what, void *arg); |
83 void memcache_conn_do_req (struct memcache_conn *conn, struct memcache_req *req) { |
89 void memcache_conn_do_req (struct memcache_conn *conn, struct memcache_req *req) { |
84 assert(conn->fd > 0 && conn->is_connected); |
90 assert(conn->fd > 0 && conn->is_connected); |
85 assert(conn->req == NULL); |
91 assert(conn->req == NULL); |
86 |
92 |
87 // write the request header into our bufferevent's output buffer |
93 // write the request header into our bufferevent's output buffer |
88 if (memcache_cmd_format_header(bufferevent_get_output(conn->bev), req->cmd_type, &req->key, &req->obj)) { |
94 if (memcache_cmd_format_header(bufferevent_get_output(conn->bev), |
|
95 memcache_req_cmd(req), |
|
96 memcache_req_key(req), |
|
97 memcache_req_obj(req) |
|
98 )) { |
89 ERROR("failed to init the cmd"); |
99 ERROR("failed to init the cmd"); |
90 } |
100 } |
91 |
101 |
92 // store the req |
102 // store the req |
93 conn->req = req; |
103 conn->req = req; |
112 |
122 |
113 /* |
123 /* |
114 * Start writing out the request data |
124 * Start writing out the request data |
115 */ |
125 */ |
116 void memcache_conn_send_req_data (struct memcache_conn *conn) { |
126 void memcache_conn_send_req_data (struct memcache_conn *conn) { |
117 // set up the ev_write |
127 if (conn->req->obj.bytes > 0) { |
118 event_set(&conn->ev_write, conn->fd, EV_WRITE, &_memcache_conn_ev_write, conn); |
128 // set up the ev_write |
119 |
129 event_set(&conn->ev_write, conn->fd, EV_WRITE, &_memcache_conn_ev_write, conn); |
120 // just fake a call to the event handler |
130 |
121 _memcache_conn_ev_write(conn->fd, EV_WRITE, conn); |
131 // just fake a call to the event handler |
|
132 _memcache_conn_ev_write(conn->fd, EV_WRITE, conn); |
|
133 |
|
134 } else { |
|
135 // just send the \r\n |
|
136 memcache_conn_finish_req_data(conn); |
|
137 } |
122 } |
138 } |
123 |
139 |
124 /* |
140 /* |
125 * Write out the final \r\n to terminate the request data |
141 * Write out the final \r\n to terminate the request data |
126 */ |
142 */ |
165 // check that the buf doesn't contain any data |
181 // check that the buf doesn't contain any data |
166 assert(conn->req->buf.data == NULL); |
182 assert(conn->req->buf.data == NULL); |
167 |
183 |
168 // bytes *may* be zero if we have an empty cache entry |
184 // bytes *may* be zero if we have an empty cache entry |
169 if (conn->req->obj.bytes > 0) { |
185 if (conn->req->obj.bytes > 0) { |
|
186 // XXX: memcache_req_make_buffer? |
|
187 |
170 // allocate a buffer for the reply data |
188 // allocate a buffer for the reply data |
171 if ((conn->req->buf.data = malloc(conn->req->obj.bytes)) == NULL) |
189 if ((conn->req->buf.data = malloc(conn->req->obj.bytes)) == NULL) |
172 ERROR("malloc"); |
190 ERROR("malloc"); |
173 |
191 |
174 // update the length |
192 // update the length |
175 conn->req->buf.len = conn->req->obj.bytes; |
193 conn->req->buf.len = conn->req->obj.bytes; |
176 |
194 |
177 // set offset to zero |
195 // set offset to zero |
178 conn->req->buf.offset = 0; |
196 conn->req->buf.offset = 0; |
|
197 |
|
198 // and note that it is present, and is ours |
|
199 conn->req->have_buf = 1; |
|
200 conn->req->is_buf_ours = 1; |
179 |
201 |
180 // do we have any data in the buf that we need to copy? |
202 // do we have any data in the buf that we need to copy? |
181 if (evbuffer_get_length(buf) > 0) { |
203 if (evbuffer_get_length(buf) > 0) { |
182 // read the data into the memcache_buf |
204 // read the data into the memcache_buf |
183 ret = evbuffer_remove(buf, conn->req->buf.data, conn->req->buf.len); |
205 ret = evbuffer_remove(buf, conn->req->buf.data, conn->req->buf.len); |
451 static void memcache_conn_error (struct memcache_conn *conn) { |
473 static void memcache_conn_error (struct memcache_conn *conn) { |
452 // fail the request, if we have one |
474 // fail the request, if we have one |
453 if (conn->req) { |
475 if (conn->req) { |
454 // error out the req |
476 // error out the req |
455 memcache_req_error(conn->req); |
477 memcache_req_error(conn->req); |
456 assert(conn->req->conn == NULL); |
|
457 |
478 |
458 // we are now available again |
479 // we are now available again |
459 conn->req = NULL; |
480 conn->req = NULL; |
460 } |
481 } |
461 |
482 |
471 */ |
492 */ |
472 static void memcache_conn_req_done (struct memcache_conn *conn) { |
493 static void memcache_conn_req_done (struct memcache_conn *conn) { |
473 // ensure that we do currently have a req |
494 // ensure that we do currently have a req |
474 assert(conn->req); |
495 assert(conn->req); |
475 |
496 |
476 // have the req detach and check it did so |
497 // have the req detach |
477 memcache_req_done(conn->req); |
498 memcache_req_done(conn->req); |
478 assert(conn->req->conn == NULL); |
499 |
479 |
|
480 // we are now available again |
500 // we are now available again |
481 conn->req = NULL; |
501 conn->req = NULL; |
482 |
502 |
483 memcache_server_conn_ready(conn->server, conn); |
503 memcache_server_conn_ready(conn->server, conn); |
484 } |
504 } |