| 4639 | } |
| 4640 | |
| 4641 | inline ssize_t SocketStream::read(char *ptr, size_t size) { |
| 4642 | #ifdef _WIN32 |
| 4643 | size = |
| 4644 | (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)())); |
| 4645 | #else |
| 4646 | size = (std::min)(size, |
| 4647 | static_cast<size_t>((std::numeric_limits<ssize_t>::max)())); |
| 4648 | #endif |
| 4649 | |
| 4650 | if (read_buff_off_ < read_buff_content_size_) { |
| 4651 | auto remaining_size = read_buff_content_size_ - read_buff_off_; |
| 4652 | if (size <= remaining_size) { |
| 4653 | memcpy(ptr, read_buff_.data() + read_buff_off_, size); |
| 4654 | read_buff_off_ += size; |
| 4655 | return static_cast<ssize_t>(size); |
| 4656 | } else { |
| 4657 | memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); |
| 4658 | read_buff_off_ += remaining_size; |
| 4659 | return static_cast<ssize_t>(remaining_size); |
| 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | if (!is_readable()) { return -1; } |
| 4664 | |
| 4665 | read_buff_off_ = 0; |
| 4666 | read_buff_content_size_ = 0; |
| 4667 | |
| 4668 | if (size < read_buff_size_) { |
| 4669 | auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, |
| 4670 | CPPHTTPLIB_RECV_FLAGS); |
| 4671 | if (n <= 0) { |
| 4672 | return n; |
| 4673 | } else if (n <= static_cast<ssize_t>(size)) { |
| 4674 | memcpy(ptr, read_buff_.data(), static_cast<size_t>(n)); |
| 4675 | return n; |
| 4676 | } else { |
| 4677 | memcpy(ptr, read_buff_.data(), size); |
| 4678 | read_buff_off_ = size; |
| 4679 | read_buff_content_size_ = static_cast<size_t>(n); |
| 4680 | return static_cast<ssize_t>(size); |
| 4681 | } |
| 4682 | } else { |
| 4683 | return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | inline ssize_t SocketStream::write(const char *ptr, size_t size) { |
| 4688 | if (!is_writable()) { return -1; } |