Attempts to add the reply to the static buffer in the client struct. * Returns C_ERR if the buffer is full, or the reply list is not empty, * in which case the reply must be added to the reply list. */
| 350 | * Returns C_ERR if the buffer is full, or the reply list is not empty, |
| 351 | * in which case the reply must be added to the reply list. */ |
| 352 | int _addReplyToBuffer(client *c, const char *s, size_t len) { |
| 353 | if (c->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_AFTER_REPLY) return C_OK; |
| 354 | |
| 355 | bool fAsync = !FCorrectThread(c); |
| 356 | if (fAsync) |
| 357 | { |
| 358 | serverAssert(GlobalLocksAcquired()); |
| 359 | if (c->replyAsync == nullptr || (c->replyAsync->size - c->replyAsync->used) < len) |
| 360 | { |
| 361 | if (c->replyAsync == nullptr) { |
| 362 | size_t newsize = std::max(len, (size_t)PROTO_ASYNC_REPLY_CHUNK_BYTES); |
| 363 | |
| 364 | clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize); |
| 365 | replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock); |
| 366 | replyNew->used = 0; |
| 367 | c->replyAsync = replyNew; |
| 368 | } else { |
| 369 | size_t newsize = std::max(c->replyAsync->used + len, c->replyAsync->size*2); |
| 370 | clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize); |
| 371 | replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock); |
| 372 | replyNew->used = c->replyAsync->used; |
| 373 | memcpy(replyNew->buf(), c->replyAsync->buf(), c->replyAsync->used); |
| 374 | zfree(c->replyAsync); |
| 375 | c->replyAsync = replyNew; |
| 376 | } |
| 377 | } |
| 378 | memcpy(c->replyAsync->buf() + c->replyAsync->used,s,len); |
| 379 | c->replyAsync->used += len; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | size_t available = sizeof(c->buf)-c->bufpos; |
| 384 | |
| 385 | /* If there already are entries in the reply list, we cannot |
| 386 | * add anything more to the static buffer. */ |
| 387 | if (listLength(c->reply) > 0) return C_ERR; |
| 388 | |
| 389 | /* Check that the buffer has enough space available for this string. */ |
| 390 | if (len > available) return C_ERR; |
| 391 | |
| 392 | memcpy(c->buf+c->bufpos,s,len); |
| 393 | c->bufpos+=len; |
| 394 | } |
| 395 | return C_OK; |
| 396 | } |
| 397 | |
| 398 | /* Adds the reply to the reply linked list. |
| 399 | * Note: some edits to this function need to be relayed to AddReplyFromClient. */ |
no test coverage detected