| 247 | } |
| 248 | |
| 249 | bool memcache::set_data(const void* data, size_t dlen) |
| 250 | { |
| 251 | if (!opened_) { |
| 252 | ebuf_.format("not opened yet!"); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | if (data == NULL || dlen == 0) { |
| 257 | ebuf_.format("invalid input, data %s, dlen %d", |
| 258 | data ? "not null" : "null", dlen ? (int) dlen : 0); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (dlen + length_ > content_length_) { |
| 263 | ebuf_.format("dlen(%d) + length_(%d) > content_length_(%d)", |
| 264 | (int) dlen, (int) length_, (int) content_length_); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | if (dlen + length_ < content_length_) { |
| 269 | if (conn_->write(data, dlen) == -1) { |
| 270 | close(); |
| 271 | ebuf_.format("write data error"); |
| 272 | return false; |
| 273 | } |
| 274 | length_ += dlen; |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | struct iovec v[2]; |
| 279 | |
| 280 | #ifdef MINGW |
| 281 | v[0].iov_base = (char*) data; |
| 282 | #else |
| 283 | v[0].iov_base = (void*) data; |
| 284 | #endif |
| 285 | v[0].iov_len = dlen; |
| 286 | #ifdef MINGW |
| 287 | v[1].iov_base = (char*) "\r\n"; |
| 288 | #else |
| 289 | v[1].iov_base = (void*) "\r\n"; |
| 290 | #endif |
| 291 | v[1].iov_len = 2; |
| 292 | |
| 293 | if (conn_->writev(v, 2) < 0) { |
| 294 | close(); |
| 295 | ebuf_.format("write data2 error!"); |
| 296 | return false; |
| 297 | } |
| 298 | length_ += dlen; |
| 299 | |
| 300 | if (!conn_->gets(res_line_)) { |
| 301 | close(); |
| 302 | ebuf_.format("reply forerror"); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | if (res_line_.compare("STORED", false) != 0) { |