Extend streams to get max non-blocking read size

Add a function to streams for obtaining the maximum size that may be
passed to read() without blocking. read() may return fewer bytes
including none.
This commit is contained in:
Florian Albrechtskirchinger 2025-02-03 13:27:40 +01:00
parent 2496aa960d
commit a8e27bf3b1
No known key found for this signature in database
GPG Key ID: 9C4E2AE92D3A9595
2 changed files with 22 additions and 0 deletions

View File

@ -735,6 +735,10 @@ public:
virtual bool is_readable() const = 0;
virtual bool is_writable() const = 0;
// Returns maximum size that may be passed to read() without blocking; read()
// may return fewer bytes
virtual size_t nonblocking_read_size() const = 0;
virtual ssize_t read(char *ptr, size_t size) = 0;
virtual ssize_t write(const char *ptr, size_t size) = 0;
virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0;
@ -2384,6 +2388,7 @@ public:
bool is_readable() const override;
bool is_writable() const override;
size_t nonblocking_read_size() const override;
ssize_t read(char *ptr, size_t size) override;
ssize_t write(const char *ptr, size_t size) override;
void get_remote_ip_and_port(std::string &ip, int &port) const override;
@ -3311,6 +3316,7 @@ public:
bool is_readable() const override;
bool is_writable() const override;
size_t nonblocking_read_size() const override;
ssize_t read(char *ptr, size_t size) override;
ssize_t write(const char *ptr, size_t size) override;
void get_remote_ip_and_port(std::string &ip, int &port) const override;
@ -3341,6 +3347,7 @@ public:
bool is_readable() const override;
bool is_writable() const override;
size_t nonblocking_read_size() const override;
ssize_t read(char *ptr, size_t size) override;
ssize_t write(const char *ptr, size_t size) override;
void get_remote_ip_and_port(std::string &ip, int &port) const override;
@ -5977,6 +5984,10 @@ inline bool SocketStream::is_writable() const {
is_socket_alive(sock_);
}
inline size_t SocketStream::nonblocking_read_size() const {
return read_buff_content_size_ - read_buff_off_;
}
inline ssize_t SocketStream::read(char *ptr, size_t size) {
#ifdef _WIN32
size =
@ -6051,6 +6062,10 @@ inline bool BufferStream::is_readable() const { return true; }
inline bool BufferStream::is_writable() const { return true; }
inline size_t BufferStream::nonblocking_read_size() const {
return (std::numeric_limits<size_t>::max)();
}
inline ssize_t BufferStream::read(char *ptr, size_t size) {
#if defined(_MSC_VER) && _MSC_VER < 1910
auto len_read = buffer._Copy_s(ptr, size, size, position);
@ -9121,6 +9136,10 @@ inline bool SSLSocketStream::is_writable() const {
is_socket_alive(sock_);
}
inline size_t SSLSocketStream::nonblocking_read_size() const {
return static_cast<size_t>(SSL_pending(ssl_));
}
inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
if (SSL_pending(ssl_) > 0) {
return SSL_read(ssl_, ptr, static_cast<int>(size));

View File

@ -1,4 +1,5 @@
#include <cstdint>
#include <limits>
#include <httplib.h>
@ -27,6 +28,8 @@ public:
bool is_writable() const override { return true; }
size_t nonblocking_read_size() const override { return (std::numeric_limits<size_t>::max)(); }
void get_remote_ip_and_port(std::string &ip, int &port) const override {
ip = "127.0.0.1";
port = 8080;