| 5022 | } |
| 5023 | |
| 5024 | inline ssize_t SocketStream::read(char *ptr, size_t size) { |
| 5025 | #ifdef _WIN32 |
| 5026 | size = |
| 5027 | (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)())); |
| 5028 | #else |
| 5029 | size = (std::min)(size, |
| 5030 | static_cast<size_t>((std::numeric_limits<ssize_t>::max)())); |
| 5031 | #endif |
| 5032 | |
| 5033 | if (read_buff_off_ < read_buff_content_size_) { |
| 5034 | auto remaining_size = read_buff_content_size_ - read_buff_off_; |
| 5035 | if (size <= remaining_size) { |
| 5036 | memcpy(ptr, read_buff_.data() + read_buff_off_, size); |
| 5037 | read_buff_off_ += size; |
| 5038 | return static_cast<ssize_t>(size); |
| 5039 | } else { |
| 5040 | memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); |
| 5041 | read_buff_off_ += remaining_size; |
| 5042 | return static_cast<ssize_t>(remaining_size); |
| 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | if (!is_readable()) { return -1; } |
| 5047 | |
| 5048 | read_buff_off_ = 0; |
| 5049 | read_buff_content_size_ = 0; |
| 5050 | |
| 5051 | if (size < read_buff_size_) { |
| 5052 | auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, |
| 5053 | CPPHTTPLIB_RECV_FLAGS); |
| 5054 | if (n <= 0) { |
| 5055 | return n; |
| 5056 | } else if (n <= static_cast<ssize_t>(size)) { |
| 5057 | memcpy(ptr, read_buff_.data(), static_cast<size_t>(n)); |
| 5058 | return n; |
| 5059 | } else { |
| 5060 | memcpy(ptr, read_buff_.data(), size); |
| 5061 | read_buff_off_ = size; |
| 5062 | read_buff_content_size_ = static_cast<size_t>(n); |
| 5063 | return static_cast<ssize_t>(size); |
| 5064 | } |
| 5065 | } else { |
| 5066 | return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | inline ssize_t SocketStream::write(const char *ptr, size_t size) { |
| 5071 | if (!is_writable()) { return -1; } |
no test coverage detected