From 12ab556477d8ffc620a84d29ac8abfaefeb4985f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 15 Oct 2013 22:18:37 +0200 Subject: event/BufferedSocket: pass writable pointer to OnSocketInput() Remove the const_cast from HttpdClient.cxx, and avoid one allocation in ClientRead.cxx. --- src/ClientInternal.hxx | 3 +-- src/ClientRead.cxx | 16 +++++++--------- src/event/BufferedSocket.hxx | 10 +++++++++- src/output/HttpdClient.cxx | 11 +++++------ src/output/HttpdClient.hxx | 3 +-- 5 files changed, 23 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/ClientInternal.hxx b/src/ClientInternal.hxx index e4c2525b4..37054fc00 100644 --- a/src/ClientInternal.hxx +++ b/src/ClientInternal.hxx @@ -111,8 +111,7 @@ public: private: /* virtual methods from class BufferedSocket */ - virtual InputResult OnSocketInput(const void *data, - size_t length) override; + virtual InputResult OnSocketInput(void *data, size_t length) override; virtual void OnSocketError(Error &&error) override; virtual void OnSocketClosed() override; diff --git a/src/ClientRead.cxx b/src/ClientRead.cxx index 8f7c4cf7e..af2e572a2 100644 --- a/src/ClientRead.cxx +++ b/src/ClientRead.cxx @@ -22,27 +22,25 @@ #include "Main.hxx" #include "event/Loop.hxx" -#include - #include #include BufferedSocket::InputResult -Client::OnSocketInput(const void *data, size_t length) +Client::OnSocketInput(void *data, size_t length) { - const char *p = (const char *)data; - const char *newline = (const char *)memchr(p, '\n', length); + char *p = (char *)data; + char *newline = (char *)memchr(p, '\n', length); if (newline == NULL) return InputResult::MORE; TimeoutMonitor::ScheduleSeconds(client_timeout); - char *line = g_strndup(p, newline - p); - BufferedSocket::ConsumeInput(newline + 1 - p); + /* terminate the string at the end of the line */ + *newline = 0; - enum command_return result = client_process_line(this, line); - g_free(line); + BufferedSocket::ConsumeInput(newline + 1 - p); + enum command_return result = client_process_line(this, p); switch (result) { case COMMAND_RETURN_OK: case COMMAND_RETURN_IDLE: diff --git a/src/event/BufferedSocket.hxx b/src/event/BufferedSocket.hxx index 31d6c3c57..db920f981 100644 --- a/src/event/BufferedSocket.hxx +++ b/src/event/BufferedSocket.hxx @@ -101,7 +101,15 @@ protected: CLOSED, }; - virtual InputResult OnSocketInput(const void *data, size_t length) = 0; + /** + * Data has been received on the socket. + * + * @param data a pointer to the beginning of the buffer; the + * buffer may be modified by the method while it processes the + * data + */ + virtual InputResult OnSocketInput(void *data, size_t length) = 0; + virtual void OnSocketError(Error &&error) = 0; virtual void OnSocketClosed() = 0; diff --git a/src/output/HttpdClient.cxx b/src/output/HttpdClient.cxx index 5b65cc956..c622f823a 100644 --- a/src/output/HttpdClient.cxx +++ b/src/output/HttpdClient.cxx @@ -402,7 +402,7 @@ HttpdClient::OnSocketReady(unsigned flags) } BufferedSocket::InputResult -HttpdClient::OnSocketInput(const void *data, size_t length) +HttpdClient::OnSocketInput(void *data, size_t length) { if (state == RESPONSE) { LogWarning(httpd_output_domain, @@ -411,8 +411,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length) return InputResult::CLOSED; } - const char *line = (const char *)data; - const char *newline = (const char *)memchr(line, '\n', length); + char *line = (char *)data; + char *newline = (char *)memchr(line, '\n', length); if (newline == nullptr) return InputResult::MORE; @@ -421,9 +421,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length) if (newline > line && newline[-1] == '\r') --newline; - /* terminate the string at the end of the line; the const_cast - is a dirty hack */ - *const_cast(newline) = 0; + /* terminate the string at the end of the line */ + *newline = 0; if (!HandleLine(line)) { assert(state == RESPONSE); diff --git a/src/output/HttpdClient.hxx b/src/output/HttpdClient.hxx index d481898a2..90295fdf7 100644 --- a/src/output/HttpdClient.hxx +++ b/src/output/HttpdClient.hxx @@ -177,8 +177,7 @@ public: protected: virtual bool OnSocketReady(unsigned flags) override; - virtual InputResult OnSocketInput(const void *data, - size_t length) override; + virtual InputResult OnSocketInput(void *data, size_t length) override; virtual void OnSocketError(Error &&error) override; virtual void OnSocketClosed() override; }; -- cgit v1.2.3