diff options
Diffstat (limited to '')
-rw-r--r-- | src/event/BufferedSocket.cxx | 49 | ||||
-rw-r--r-- | src/event/BufferedSocket.hxx | 16 |
2 files changed, 21 insertions, 44 deletions
diff --git a/src/event/BufferedSocket.cxx b/src/event/BufferedSocket.cxx index f333a5987..92e350e85 100644 --- a/src/event/BufferedSocket.cxx +++ b/src/event/BufferedSocket.cxx @@ -20,20 +20,9 @@ #include "config.h" #include "BufferedSocket.hxx" #include "system/SocketError.hxx" -#include "util/fifo_buffer.h" #include "util/Error.hxx" #include "util/Domain.hxx" -#include <assert.h> -#include <stdint.h> -#include <string.h> - -BufferedSocket::~BufferedSocket() -{ - if (input != nullptr) - fifo_buffer_free(input); -} - BufferedSocket::ssize_t BufferedSocket::DirectRead(void *data, size_t length) { @@ -62,16 +51,12 @@ BufferedSocket::ReadToBuffer() { assert(IsDefined()); - if (input == nullptr) - input = fifo_buffer_new(8192); - - size_t length; - void *buffer = fifo_buffer_write(input, &length); - assert(buffer != nullptr); + const auto buffer = input.Write(); + assert(!buffer.IsEmpty()); - const auto nbytes = DirectRead(buffer, length); + const auto nbytes = DirectRead(buffer.data, buffer.size); if (nbytes > 0) - fifo_buffer_append(input, nbytes); + input.Append(nbytes); return nbytes >= 0; } @@ -81,23 +66,17 @@ BufferedSocket::ResumeInput() { assert(IsDefined()); - if (input == nullptr) { - ScheduleRead(); - return true; - } - while (true) { - size_t length; - const void *data = fifo_buffer_read(input, &length); - if (data == nullptr) { + const auto buffer = input.Read(); + if (buffer.IsEmpty()) { ScheduleRead(); return true; } - const auto result = OnSocketInput(data, length); + const auto result = OnSocketInput(buffer.data, buffer.size); switch (result) { case InputResult::MORE: - if (fifo_buffer_is_full(input)) { + if (input.IsFull()) { // TODO static constexpr Domain buffered_socket_domain("buffered_socket"); Error error; @@ -123,14 +102,6 @@ BufferedSocket::ResumeInput() } } -void -BufferedSocket::ConsumeInput(size_t nbytes) -{ - assert(IsDefined()); - - fifo_buffer_consume(input, nbytes); -} - bool BufferedSocket::OnSocketReady(unsigned flags) { @@ -142,12 +113,12 @@ BufferedSocket::OnSocketReady(unsigned flags) } if (flags & READ) { - assert(input == nullptr || !fifo_buffer_is_full(input)); + assert(!input.IsFull()); if (!ReadToBuffer() || !ResumeInput()) return false; - if (input == nullptr || !fifo_buffer_is_full(input)) + if (input.IsFull()) ScheduleRead(); } diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index 578000961..31d6c3c57 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -22,8 +22,12 @@ #include "check.h" #include "SocketMonitor.hxx" +#include "util/FifoBuffer.hxx" #include "Compiler.h" +#include <assert.h> +#include <stdint.h> + struct fifo_buffer; class Error; @@ -31,16 +35,14 @@ class Error; * A #SocketMonitor specialization that adds an input buffer. */ class BufferedSocket : protected SocketMonitor { - fifo_buffer *input; + FifoBuffer<uint8_t, 8192> input; public: BufferedSocket(int _fd, EventLoop &_loop) - :SocketMonitor(_fd, _loop), input(nullptr) { + :SocketMonitor(_fd, _loop) { ScheduleRead(); } - ~BufferedSocket(); - using SocketMonitor::IsDefined; using SocketMonitor::Close; using SocketMonitor::Write; @@ -67,7 +69,11 @@ protected: * does not invalidate the pointer passed to OnSocketInput() * yet. */ - void ConsumeInput(size_t nbytes); + void ConsumeInput(size_t nbytes) { + assert(IsDefined()); + + input.Consume(nbytes); + } enum class InputResult { /** |