Client: use std::deque for messages

This commit is contained in:
Max Kellermann 2013-01-03 18:07:48 +01:00
parent 5155b0f608
commit 59400d38a9
5 changed files with 10 additions and 41 deletions

View File

@ -26,6 +26,7 @@
#include <set> #include <set>
#include <string> #include <string>
#include <deque>
#include <glib.h> #include <glib.h>
@ -94,15 +95,9 @@ public:
unsigned num_subscriptions; unsigned num_subscriptions;
/** /**
* A list of messages this client has received in reverse * A list of messages this client has received.
* order (latest first).
*/ */
GSList *messages; std::deque<ClientMessage> messages;
/**
* The number of messages in #messages.
*/
unsigned num_messages;
gcc_pure gcc_pure
bool IsSubscribed(const char *channel_name) const { bool IsSubscribed(const char *channel_name) const {

View File

@ -120,9 +120,6 @@ client_new(struct player_control *player_control,
client->send_buf_used = 0; client->send_buf_used = 0;
client->messages = NULL;
client->num_messages = 0;
(void)send(fd, GREETING, sizeof(GREETING) - 1, 0); (void)send(fd, GREETING, sizeof(GREETING) - 1, 0);
client_list_add(client); client_list_add(client);

View File

@ -83,27 +83,13 @@ client_push_message(Client *client, const ClientMessage &msg)
{ {
assert(client != NULL); assert(client != NULL);
if (client->num_messages >= CLIENT_MAX_MESSAGES || if (client->messages.size() >= CLIENT_MAX_MESSAGES ||
!client->IsSubscribed(msg.GetChannel())) !client->IsSubscribed(msg.GetChannel()))
return false; return false;
if (client->messages == NULL) if (client->messages.empty())
client_idle_add(client, IDLE_MESSAGE); client_idle_add(client, IDLE_MESSAGE);
client->messages = g_slist_prepend(client->messages, client->messages.push_back(msg);
new ClientMessage(msg));
++client->num_messages;
return true; return true;
} }
GSList *
client_read_messages(Client *client)
{
GSList *messages = g_slist_reverse(client->messages);
client->messages = NULL;
client->num_messages = 0;
return messages;
}

View File

@ -52,8 +52,4 @@ client_unsubscribe_all(Client *client);
bool bool
client_push_message(Client *client, const ClientMessage &msg); client_push_message(Client *client, const ClientMessage &msg);
gcc_malloc
GSList *
client_read_messages(Client *client);
#endif #endif

View File

@ -109,19 +109,14 @@ handle_read_messages(Client *client,
{ {
assert(argc == 1); assert(argc == 1);
GSList *messages = client_read_messages(client); while (!client->messages.empty()) {
const ClientMessage &msg = client->messages.front();
for (GSList *i = messages; i != NULL; i = g_slist_next(i)) {
ClientMessage *msg = (ClientMessage *)i->data;
client_printf(client, "channel: %s\nmessage: %s\n", client_printf(client, "channel: %s\nmessage: %s\n",
msg->GetChannel(), msg.GetChannel(), msg.GetMessage());
msg->GetMessage()); client->messages.pop_front();
delete msg;
} }
g_slist_free(messages);
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
} }