| 392 | } |
| 393 | |
| 394 | int32_t cache_tcp_recv(TRWCache* cache, uint32_t fd) |
| 395 | { |
| 396 | if (NULL == cache) |
| 397 | { |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | int32_t total = 0; |
| 402 | for (uint32_t i = 0; i < 100; i++) |
| 403 | { |
| 404 | TSkBuffer* item = TAILQ_LAST(&cache->list, __sk_buff_list); |
| 405 | if ((NULL == item) |
| 406 | || ((item->data_len + item->data) >= item->end)) |
| 407 | { |
| 408 | item = alloc_sk_buffer(cache->pool); |
| 409 | if (item == NULL) |
| 410 | { |
| 411 | return -2; |
| 412 | } |
| 413 | cache_append_buffer(cache, item); |
| 414 | } |
| 415 | |
| 416 | uint8_t* buff = item->data + item->data_len; |
| 417 | uint32_t remain = item->end - item->data - item->data_len; |
| 418 | mt_hook_syscall(recv); |
| 419 | int32_t recvd_len = ff_hook_recv(fd, buff, remain, 0); |
| 420 | if (recvd_len == 0) |
| 421 | { |
| 422 | MTLOG_DEBUG("remote close, socket: %d", fd); |
| 423 | return -SK_ERR_NEED_CLOSE; |
| 424 | } |
| 425 | else if (recvd_len < 0) |
| 426 | { |
| 427 | if (errno == EAGAIN) |
| 428 | { |
| 429 | return total; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | MTLOG_ERROR("recv tcp socket failed, error: %d[%m]", errno); |
| 434 | return -2; |
| 435 | } |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | item->data_len += recvd_len; |
| 440 | cache->len += recvd_len; |
| 441 | total += recvd_len; |
| 442 | if (recvd_len < (int32_t)remain) |
| 443 | { |
| 444 | return total; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return total; |
| 450 | } |
| 451 |
no test coverage detected