Commit 12ab5564 authored by Max Kellermann's avatar Max Kellermann

event/BufferedSocket: pass writable pointer to OnSocketInput()

Remove the const_cast from HttpdClient.cxx, and avoid one allocation in ClientRead.cxx.
parent 509f8dab
...@@ -111,8 +111,7 @@ public: ...@@ -111,8 +111,7 @@ public:
private: private:
/* virtual methods from class BufferedSocket */ /* virtual methods from class BufferedSocket */
virtual InputResult OnSocketInput(const void *data, virtual InputResult OnSocketInput(void *data, size_t length) override;
size_t length) override;
virtual void OnSocketError(Error &&error) override; virtual void OnSocketError(Error &&error) override;
virtual void OnSocketClosed() override; virtual void OnSocketClosed() override;
......
...@@ -22,27 +22,25 @@ ...@@ -22,27 +22,25 @@
#include "Main.hxx" #include "Main.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
BufferedSocket::InputResult BufferedSocket::InputResult
Client::OnSocketInput(const void *data, size_t length) Client::OnSocketInput(void *data, size_t length)
{ {
const char *p = (const char *)data; char *p = (char *)data;
const char *newline = (const char *)memchr(p, '\n', length); char *newline = (char *)memchr(p, '\n', length);
if (newline == NULL) if (newline == NULL)
return InputResult::MORE; return InputResult::MORE;
TimeoutMonitor::ScheduleSeconds(client_timeout); TimeoutMonitor::ScheduleSeconds(client_timeout);
char *line = g_strndup(p, newline - p); /* terminate the string at the end of the line */
BufferedSocket::ConsumeInput(newline + 1 - p); *newline = 0;
enum command_return result = client_process_line(this, line); BufferedSocket::ConsumeInput(newline + 1 - p);
g_free(line);
enum command_return result = client_process_line(this, p);
switch (result) { switch (result) {
case COMMAND_RETURN_OK: case COMMAND_RETURN_OK:
case COMMAND_RETURN_IDLE: case COMMAND_RETURN_IDLE:
......
...@@ -101,7 +101,15 @@ protected: ...@@ -101,7 +101,15 @@ protected:
CLOSED, 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 OnSocketError(Error &&error) = 0;
virtual void OnSocketClosed() = 0; virtual void OnSocketClosed() = 0;
......
...@@ -402,7 +402,7 @@ HttpdClient::OnSocketReady(unsigned flags) ...@@ -402,7 +402,7 @@ HttpdClient::OnSocketReady(unsigned flags)
} }
BufferedSocket::InputResult BufferedSocket::InputResult
HttpdClient::OnSocketInput(const void *data, size_t length) HttpdClient::OnSocketInput(void *data, size_t length)
{ {
if (state == RESPONSE) { if (state == RESPONSE) {
LogWarning(httpd_output_domain, LogWarning(httpd_output_domain,
...@@ -411,8 +411,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length) ...@@ -411,8 +411,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length)
return InputResult::CLOSED; return InputResult::CLOSED;
} }
const char *line = (const char *)data; char *line = (char *)data;
const char *newline = (const char *)memchr(line, '\n', length); char *newline = (char *)memchr(line, '\n', length);
if (newline == nullptr) if (newline == nullptr)
return InputResult::MORE; return InputResult::MORE;
...@@ -421,9 +421,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length) ...@@ -421,9 +421,8 @@ HttpdClient::OnSocketInput(const void *data, size_t length)
if (newline > line && newline[-1] == '\r') if (newline > line && newline[-1] == '\r')
--newline; --newline;
/* terminate the string at the end of the line; the const_cast /* terminate the string at the end of the line */
is a dirty hack */ *newline = 0;
*const_cast<char *>(newline) = 0;
if (!HandleLine(line)) { if (!HandleLine(line)) {
assert(state == RESPONSE); assert(state == RESPONSE);
......
...@@ -177,8 +177,7 @@ public: ...@@ -177,8 +177,7 @@ public:
protected: protected:
virtual bool OnSocketReady(unsigned flags) override; virtual bool OnSocketReady(unsigned flags) override;
virtual InputResult OnSocketInput(const void *data, virtual InputResult OnSocketInput(void *data, size_t length) override;
size_t length) override;
virtual void OnSocketError(Error &&error) override; virtual void OnSocketError(Error &&error) override;
virtual void OnSocketClosed() override; virtual void OnSocketClosed() override;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment