Added fr::Socket::has_data to check if recv buffer is empty

If using a SocketSelector, you should keep accepting data from the socket until has_data returns false, indicating that you've read everything.
This commit is contained in:
Fred Nicolson 2016-12-27 21:58:39 +00:00
parent 8638b70fb8
commit 2a5d960e56
6 changed files with 39 additions and 1 deletions

View File

@ -98,6 +98,14 @@ namespace fr
abort(); abort();
} }
/*!
* Checks to see if there's data still in the socket's
* recv buffer.
*
* @return True if there is data in the buffer, false otherwise.
*/
virtual bool has_data() const override;
private: private:
std::string unprocessed_buffer; std::string unprocessed_buffer;
std::unique_ptr<char[]> recv_buffer; std::unique_ptr<char[]> recv_buffer;

View File

@ -146,6 +146,14 @@ namespace fr
*/ */
void shutdown(); void shutdown();
/*!
* Checks to see if there's data still in the socket's
* recv buffer.
*
* @return True if there is data in the buffer, false otherwise.
*/
virtual bool has_data() const = 0;
protected: protected:
std::string remote_address; std::string remote_address;
bool is_blocking; bool is_blocking;

View File

@ -45,6 +45,7 @@ private:
virtual fr::Socket::Status send_raw(const char*, size_t){return Socket::Error;} virtual fr::Socket::Status send_raw(const char*, size_t){return Socket::Error;}
virtual fr::Socket::Status receive_raw(void*, size_t, size_t&){return Socket::Error;} virtual fr::Socket::Status receive_raw(void*, size_t, size_t&){return Socket::Error;}
virtual int32_t get_socket_descriptor() const {return socket_descriptor;} virtual int32_t get_socket_descriptor() const {return socket_descriptor;}
virtual bool has_data() const override {return false;};
}; };
} }

View File

@ -18,7 +18,10 @@ class TcpSocket : public Socket
public: public:
TcpSocket() noexcept; TcpSocket() noexcept;
virtual ~TcpSocket() noexcept; virtual ~TcpSocket() noexcept;
TcpSocket(TcpSocket &&) noexcept = default; TcpSocket(TcpSocket &&other) noexcept
: unprocessed_buffer(std::move(other.unprocessed_buffer)),
recv_buffer(std::move(other.recv_buffer)),
socket_descriptor(other.socket_descriptor){}
void operator=(const TcpSocket &other)=delete; void operator=(const TcpSocket &other)=delete;
/*! /*!
@ -92,6 +95,14 @@ public:
*/ */
int32_t get_socket_descriptor() const override; int32_t get_socket_descriptor() const override;
/*!
* Checks to see if there's data still in the socket's
* recv buffer.
*
* @return True if there is data in the buffer, false otherwise.
*/
virtual bool has_data() const override;
protected: protected:
std::string unprocessed_buffer; std::string unprocessed_buffer;
std::unique_ptr<char[]> recv_buffer; std::unique_ptr<char[]> recv_buffer;

View File

@ -168,6 +168,11 @@ namespace fr
is_connected = true; is_connected = true;
ssl_socket_descriptor = std::move(context); ssl_socket_descriptor = std::move(context);
} }
bool SSLSocket::has_data() const
{
return !unprocessed_buffer.empty();
}
} }
#endif #endif

View File

@ -159,4 +159,9 @@ namespace fr
{ {
return socket_descriptor; return socket_descriptor;
} }
bool TcpSocket::has_data() const
{
return !unprocessed_buffer.empty();
}
} }