Client: move connection functions into the class

This commit is contained in:
Max Kellermann 2013-01-15 10:11:08 +01:00
parent 3fcf17cb79
commit 1e2018ce83
11 changed files with 50 additions and 56 deletions

View File

@ -20,11 +20,6 @@
#include "config.h" #include "config.h"
#include "ClientInternal.hxx" #include "ClientInternal.hxx"
bool client_is_expired(const Client *client)
{
return client->channel == NULL;
}
int client_get_uid(const Client *client) int client_get_uid(const Client *client)
{ {
return client->uid; return client->uid;

View File

@ -37,9 +37,6 @@ void
client_new(Partition &partition, client_new(Partition &partition,
int fd, const struct sockaddr *sa, size_t sa_length, int uid); int fd, const struct sockaddr *sa, size_t sa_length, int uid);
gcc_pure
bool client_is_expired(const Client *client);
/** /**
* returns the uid of the client process, or a negative value if the * returns the uid of the client process, or a negative value if the
* uid is unknown * uid is unknown

View File

@ -30,17 +30,17 @@ client_out_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
{ {
Client *client = (Client *)data; Client *client = (Client *)data;
assert(!client_is_expired(client)); assert(!client->IsExpired());
if (condition != G_IO_OUT) { if (condition != G_IO_OUT) {
client_set_expired(client); client->SetExpired();
return false; return false;
} }
client_write_deferred(client); client_write_deferred(client);
if (client_is_expired(client)) { if (client->IsExpired()) {
client_close(client); client->Close();
return false; return false;
} }
@ -66,10 +66,10 @@ client_in_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
Client *client = (Client *)data; Client *client = (Client *)data;
enum command_return ret; enum command_return ret;
assert(!client_is_expired(client)); assert(!client->IsExpired());
if (condition != G_IO_IN) { if (condition != G_IO_IN) {
client_set_expired(client); client->SetExpired();
return false; return false;
} }
@ -83,17 +83,17 @@ client_in_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
break; break;
case COMMAND_RETURN_KILL: case COMMAND_RETURN_KILL:
client_close(client); client->Close();
main_loop->Break(); main_loop->Break();
return false; return false;
case COMMAND_RETURN_CLOSE: case COMMAND_RETURN_CLOSE:
client_close(client); client->Close();
return false; return false;
} }
if (client_is_expired(client)) { if (client->IsExpired()) {
client_close(client); client->Close();
return false; return false;
} }

View File

@ -24,34 +24,34 @@
static guint expire_source_id; static guint expire_source_id;
void void
client_set_expired(Client *client) Client::SetExpired()
{ {
if (!client_is_expired(client)) if (!IsExpired())
client_schedule_expire(); client_schedule_expire();
if (client->source_id != 0) { if (source_id != 0) {
g_source_remove(client->source_id); g_source_remove(source_id);
client->source_id = 0; source_id = 0;
} }
if (client->channel != NULL) { if (channel != NULL) {
g_io_channel_unref(client->channel); g_io_channel_unref(channel);
client->channel = NULL; channel = nullptr;
} }
} }
static void static void
client_check_expired_callback(Client *client, G_GNUC_UNUSED gpointer user_data) client_check_expired_callback(Client *client, G_GNUC_UNUSED gpointer user_data)
{ {
if (client_is_expired(client)) { if (client->IsExpired()) {
g_debug("[%u] expired", client->num); g_debug("[%u] expired", client->num);
client_close(client); client->Close();
} else if (!client->idle_waiting && /* idle clients } else if (!client->idle_waiting && /* idle clients
never expire */ never expire */
(int)g_timer_elapsed(client->last_activity, NULL) > (int)g_timer_elapsed(client->last_activity, NULL) >
client_timeout) { client_timeout) {
g_debug("[%u] timeout", client->num); g_debug("[%u] timeout", client->num);
client_close(client); client->Close();
} }
} }

View File

@ -58,7 +58,7 @@ static void client_close_all(void)
while (!client_list_is_empty()) { while (!client_list_is_empty()) {
Client *client = client_list_get_first(); Client *client = client_list_get_first();
client_close(client); client->Close();
} }
assert(client_list_is_empty()); assert(client_list_is_empty());

View File

@ -55,7 +55,7 @@ client_idle_notify(Client *client)
void void
client_idle_add(Client *client, unsigned flags) client_idle_add(Client *client, unsigned flags)
{ {
if (client_is_expired(client)) if (client->IsExpired())
return; return;
client->idle_flags |= flags; client->idle_flags |= flags;

View File

@ -112,6 +112,15 @@ public:
bool IsSubscribed(const char *channel_name) const { bool IsSubscribed(const char *channel_name) const {
return subscriptions.find(channel_name) != subscriptions.end(); return subscriptions.find(channel_name) != subscriptions.end();
} }
gcc_pure
bool IsExpired() const {
return channel == nullptr;
}
void Close();
void SetExpired();
}; };
extern unsigned int client_max_connections; extern unsigned int client_max_connections;
@ -119,12 +128,6 @@ extern int client_timeout;
extern size_t client_max_command_list_size; extern size_t client_max_command_list_size;
extern size_t client_max_output_buffer_size; extern size_t client_max_output_buffer_size;
void
client_close(Client *client);
void
client_set_expired(Client *client);
/** /**
* Schedule an "expired" check for all clients: permanently delete * Schedule an "expired" check for all clients: permanently delete
* clients which have been set "expired" with client_set_expired(). * clients which have been set "expired" with client_set_expired().

View File

@ -149,13 +149,12 @@ client_new(Partition &partition,
} }
void void
client_close(Client *client) Client::Close()
{ {
client_list_remove(client); client_list_remove(this);
client_set_expired(client); SetExpired();
g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE, g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE, "[%u] closed", num);
"[%u] closed", client->num); delete this;
delete client;
} }

View File

@ -42,7 +42,7 @@ client_process_command_list(Client *client, bool list_ok,
cmd); cmd);
ret = command_process(client, num++, cmd); ret = command_process(client, num++, cmd);
g_debug("command_process_list: command returned %i", ret); g_debug("command_process_list: command returned %i", ret);
if (ret != COMMAND_RETURN_OK || client_is_expired(client)) if (ret != COMMAND_RETURN_OK || client->IsExpired())
break; break;
else if (list_ok) else if (list_ok)
client_puts(client, "list_OK\n"); client_puts(client, "list_OK\n");
@ -91,7 +91,7 @@ client_process_line(Client *client, char *line)
"list returned %i", client->num, ret); "list returned %i", client->num, ret);
if (ret == COMMAND_RETURN_CLOSE || if (ret == COMMAND_RETURN_CLOSE ||
client_is_expired(client)) client->IsExpired())
return COMMAND_RETURN_CLOSE; return COMMAND_RETURN_CLOSE;
if (ret == COMMAND_RETURN_OK) if (ret == COMMAND_RETURN_OK)
@ -125,7 +125,7 @@ client_process_line(Client *client, char *line)
client->num, ret); client->num, ret);
if (ret == COMMAND_RETURN_CLOSE || if (ret == COMMAND_RETURN_CLOSE ||
client_is_expired(client)) client->IsExpired())
return COMMAND_RETURN_CLOSE; return COMMAND_RETURN_CLOSE;
if (ret == COMMAND_RETURN_OK) if (ret == COMMAND_RETURN_OK)

View File

@ -58,7 +58,7 @@ client_input_received(Client *client, size_t bytesRead)
if (ret == COMMAND_RETURN_KILL || if (ret == COMMAND_RETURN_KILL ||
ret == COMMAND_RETURN_CLOSE) ret == COMMAND_RETURN_CLOSE)
return ret; return ret;
if (client_is_expired(client)) if (client->IsExpired())
return COMMAND_RETURN_CLOSE; return COMMAND_RETURN_CLOSE;
} }

View File

@ -49,13 +49,13 @@ client_write_deferred_buffer(Client *client,
case G_IO_STATUS_EOF: case G_IO_STATUS_EOF:
/* client has disconnected */ /* client has disconnected */
client_set_expired(client); client->SetExpired();
return 0; return 0;
case G_IO_STATUS_ERROR: case G_IO_STATUS_ERROR:
/* I/O error */ /* I/O error */
client_set_expired(client); client->SetExpired();
g_warning("failed to flush buffer for %i: %s", g_warning("failed to flush buffer for %i: %s",
client->num, error->message); client->num, error->message);
g_error_free(error); g_error_free(error);
@ -126,7 +126,7 @@ client_defer_output(Client *client, const void *data, size_t length)
(unsigned long)client->deferred_bytes, (unsigned long)client->deferred_bytes,
(unsigned long)client_max_output_buffer_size); (unsigned long)client_max_output_buffer_size);
/* cause client to close */ /* cause client to close */
client_set_expired(client); client->SetExpired();
return; return;
} }
@ -160,13 +160,13 @@ client_write_direct(Client *client, const char *data, size_t length)
case G_IO_STATUS_EOF: case G_IO_STATUS_EOF:
/* client has disconnected */ /* client has disconnected */
client_set_expired(client); client->SetExpired();
return; return;
case G_IO_STATUS_ERROR: case G_IO_STATUS_ERROR:
/* I/O error */ /* I/O error */
client_set_expired(client); client->SetExpired();
g_warning("failed to write to %i: %s", g_warning("failed to write to %i: %s",
client->num, error->message); client->num, error->message);
g_error_free(error); g_error_free(error);
@ -184,14 +184,14 @@ client_write_direct(Client *client, const char *data, size_t length)
void void
client_write_output(Client *client) client_write_output(Client *client)
{ {
if (client_is_expired(client) || !client->send_buf_used) if (client->IsExpired() || !client->send_buf_used)
return; return;
if (!g_queue_is_empty(client->deferred_send)) { if (!g_queue_is_empty(client->deferred_send)) {
client_defer_output(client, client->send_buf, client_defer_output(client, client->send_buf,
client->send_buf_used); client->send_buf_used);
if (client_is_expired(client)) if (client->IsExpired())
return; return;
/* try to flush the deferred buffers now; the current /* try to flush the deferred buffers now; the current
@ -216,10 +216,10 @@ static void
client_write(Client *client, const char *buffer, size_t buflen) client_write(Client *client, const char *buffer, size_t buflen)
{ {
/* if the client is going to be closed, do nothing */ /* if the client is going to be closed, do nothing */
if (client_is_expired(client)) if (client->IsExpired())
return; return;
while (buflen > 0 && !client_is_expired(client)) { while (buflen > 0 && !client->IsExpired()) {
size_t copylen; size_t copylen;
assert(client->send_buf_used < sizeof(client->send_buf)); assert(client->send_buf_used < sizeof(client->send_buf));