Client: rebase on the new BufferedSocket class

This commit is contained in:
Max Kellermann
2013-01-14 23:42:06 +01:00
parent 396480cf94
commit 39439b80f5
10 changed files with 66 additions and 304 deletions

View File

@@ -20,98 +20,9 @@
#include "config.h"
#include "ClientInternal.hxx"
#include <assert.h>
#include <string.h>
#include <stdio.h>
static size_t
client_write_direct(Client *client, const void *data, size_t length)
{
assert(client != NULL);
assert(client->channel != NULL);
assert(data != NULL);
assert(length > 0);
gsize bytes_written;
GError *error = NULL;
GIOStatus status =
g_io_channel_write_chars(client->channel, (const gchar *)data,
length, &bytes_written, &error);
switch (status) {
case G_IO_STATUS_NORMAL:
return bytes_written;
case G_IO_STATUS_AGAIN:
return 0;
case G_IO_STATUS_EOF:
/* client has disconnected */
client->SetExpired();
return 0;
case G_IO_STATUS_ERROR:
/* I/O error */
client->SetExpired();
g_warning("failed to write to %i: %s",
client->num, error->message);
g_error_free(error);
return 0;
}
/* unreachable */
assert(false);
return 0;
}
void
client_write_deferred(Client *client)
{
assert(!client_is_expired(client));
while (true) {
size_t length;
const void *data = client->output_buffer.Read(&length);
if (data == nullptr)
break;
size_t nbytes = client_write_direct(client, data, length);
if (nbytes == 0)
return;
client->output_buffer.Consume(nbytes);
if (nbytes < length)
return;
g_timer_start(client->last_activity);
}
}
static void
client_defer_output(Client *client, const void *data, size_t length)
{
if (!client->output_buffer.Append(data, length)) {
g_warning("[%u] output buffer size is "
"larger than the max (%lu)",
client->num,
(unsigned long)client_max_output_buffer_size);
/* cause client to close */
client->SetExpired();
return;
}
}
void
client_write_output(Client *client)
{
if (client->IsExpired())
return;
client_write_deferred(client);
}
/**
* Write a block of data to the client.
*/
@@ -122,7 +33,7 @@ client_write(Client *client, const char *data, size_t length)
if (client->IsExpired() || length == 0)
return;
client_defer_output(client, data, length);
client->Write(data, length);
}
void