Commit 1e2018ce authored by Max Kellermann's avatar Max Kellermann

Client: move connection functions into the class

parent 3fcf17cb
......@@ -20,11 +20,6 @@
#include "config.h"
#include "ClientInternal.hxx"
bool client_is_expired(const Client *client)
{
return client->channel == NULL;
}
int client_get_uid(const Client *client)
{
return client->uid;
......
......@@ -37,9 +37,6 @@ void
client_new(Partition &partition,
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
* uid is unknown
......
......@@ -30,17 +30,17 @@ client_out_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
{
Client *client = (Client *)data;
assert(!client_is_expired(client));
assert(!client->IsExpired());
if (condition != G_IO_OUT) {
client_set_expired(client);
client->SetExpired();
return false;
}
client_write_deferred(client);
if (client_is_expired(client)) {
client_close(client);
if (client->IsExpired()) {
client->Close();
return false;
}
......@@ -66,10 +66,10 @@ client_in_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
Client *client = (Client *)data;
enum command_return ret;
assert(!client_is_expired(client));
assert(!client->IsExpired());
if (condition != G_IO_IN) {
client_set_expired(client);
client->SetExpired();
return false;
}
......@@ -83,17 +83,17 @@ client_in_event(G_GNUC_UNUSED GIOChannel *source, GIOCondition condition,
break;
case COMMAND_RETURN_KILL:
client_close(client);
client->Close();
main_loop->Break();
return false;
case COMMAND_RETURN_CLOSE:
client_close(client);
client->Close();
return false;
}
if (client_is_expired(client)) {
client_close(client);
if (client->IsExpired()) {
client->Close();
return false;
}
......
......@@ -24,34 +24,34 @@
static guint expire_source_id;
void
client_set_expired(Client *client)
Client::SetExpired()
{
if (!client_is_expired(client))
if (!IsExpired())
client_schedule_expire();
if (client->source_id != 0) {
g_source_remove(client->source_id);
client->source_id = 0;
if (source_id != 0) {
g_source_remove(source_id);
source_id = 0;
}
if (client->channel != NULL) {
g_io_channel_unref(client->channel);
client->channel = NULL;
if (channel != NULL) {
g_io_channel_unref(channel);
channel = nullptr;
}
}
static void
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);
client_close(client);
client->Close();
} else if (!client->idle_waiting && /* idle clients
never expire */
(int)g_timer_elapsed(client->last_activity, NULL) >
client_timeout) {
g_debug("[%u] timeout", client->num);
client_close(client);
client->Close();
}
}
......
......@@ -58,7 +58,7 @@ static void client_close_all(void)
while (!client_list_is_empty()) {
Client *client = client_list_get_first();
client_close(client);
client->Close();
}
assert(client_list_is_empty());
......
......@@ -55,7 +55,7 @@ client_idle_notify(Client *client)
void
client_idle_add(Client *client, unsigned flags)
{
if (client_is_expired(client))
if (client->IsExpired())
return;
client->idle_flags |= flags;
......
......@@ -112,6 +112,15 @@ public:
bool IsSubscribed(const char *channel_name) const {
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;
......@@ -119,12 +128,6 @@ extern int client_timeout;
extern size_t client_max_command_list_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
* clients which have been set "expired" with client_set_expired().
......
......@@ -149,13 +149,12 @@ client_new(Partition &partition,
}
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,
"[%u] closed", client->num);
delete client;
g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE, "[%u] closed", num);
delete this;
}
......@@ -42,7 +42,7 @@ client_process_command_list(Client *client, bool list_ok,
cmd);
ret = command_process(client, num++, cmd);
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;
else if (list_ok)
client_puts(client, "list_OK\n");
......@@ -91,7 +91,7 @@ client_process_line(Client *client, char *line)
"list returned %i", client->num, ret);
if (ret == COMMAND_RETURN_CLOSE ||
client_is_expired(client))
client->IsExpired())
return COMMAND_RETURN_CLOSE;
if (ret == COMMAND_RETURN_OK)
......@@ -125,7 +125,7 @@ client_process_line(Client *client, char *line)
client->num, ret);
if (ret == COMMAND_RETURN_CLOSE ||
client_is_expired(client))
client->IsExpired())
return COMMAND_RETURN_CLOSE;
if (ret == COMMAND_RETURN_OK)
......
......@@ -58,7 +58,7 @@ client_input_received(Client *client, size_t bytesRead)
if (ret == COMMAND_RETURN_KILL ||
ret == COMMAND_RETURN_CLOSE)
return ret;
if (client_is_expired(client))
if (client->IsExpired())
return COMMAND_RETURN_CLOSE;
}
......
......@@ -49,13 +49,13 @@ client_write_deferred_buffer(Client *client,
case G_IO_STATUS_EOF:
/* client has disconnected */
client_set_expired(client);
client->SetExpired();
return 0;
case G_IO_STATUS_ERROR:
/* I/O error */
client_set_expired(client);
client->SetExpired();
g_warning("failed to flush buffer for %i: %s",
client->num, error->message);
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_max_output_buffer_size);
/* cause client to close */
client_set_expired(client);
client->SetExpired();
return;
}
......@@ -160,13 +160,13 @@ client_write_direct(Client *client, const char *data, size_t length)
case G_IO_STATUS_EOF:
/* client has disconnected */
client_set_expired(client);
client->SetExpired();
return;
case G_IO_STATUS_ERROR:
/* I/O error */
client_set_expired(client);
client->SetExpired();
g_warning("failed to write to %i: %s",
client->num, error->message);
g_error_free(error);
......@@ -184,14 +184,14 @@ client_write_direct(Client *client, const char *data, size_t length)
void
client_write_output(Client *client)
{
if (client_is_expired(client) || !client->send_buf_used)
if (client->IsExpired() || !client->send_buf_used)
return;
if (!g_queue_is_empty(client->deferred_send)) {
client_defer_output(client, client->send_buf,
client->send_buf_used);
if (client_is_expired(client))
if (client->IsExpired())
return;
/* 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)
{
/* if the client is going to be closed, do nothing */
if (client_is_expired(client))
if (client->IsExpired())
return;
while (buflen > 0 && !client_is_expired(client)) {
while (buflen > 0 && !client->IsExpired()) {
size_t copylen;
assert(client->send_buf_used < sizeof(client->send_buf));
......
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