aboutsummaryrefslogtreecommitdiffstats
path: root/src/event
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-10-15 10:28:52 +0200
committerMax Kellermann <max@duempel.org>2013-10-15 10:28:52 +0200
commit84d20d9e433028125bfe36557ad54a28b97914b2 (patch)
tree9c0b9469ac91e14b90eebd08b5a04ba3a9f3b4a3 /src/event
parent0c13703da3641951bf061cac7c5cef034eda16f9 (diff)
downloadmpd-84d20d9e433028125bfe36557ad54a28b97914b2.tar.gz
mpd-84d20d9e433028125bfe36557ad54a28b97914b2.tar.xz
mpd-84d20d9e433028125bfe36557ad54a28b97914b2.zip
util/FifoBuffer: C++ version of the fifo_buffer library
Diffstat (limited to 'src/event')
-rw-r--r--src/event/BufferedSocket.cxx49
-rw-r--r--src/event/BufferedSocket.hxx16
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 {
/**