Client: use std::deque for messages
This commit is contained in:
parent
5155b0f608
commit
59400d38a9
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
|
@ -94,15 +95,9 @@ public:
|
|||
unsigned num_subscriptions;
|
||||
|
||||
/**
|
||||
* A list of messages this client has received in reverse
|
||||
* order (latest first).
|
||||
* A list of messages this client has received.
|
||||
*/
|
||||
GSList *messages;
|
||||
|
||||
/**
|
||||
* The number of messages in #messages.
|
||||
*/
|
||||
unsigned num_messages;
|
||||
std::deque<ClientMessage> messages;
|
||||
|
||||
gcc_pure
|
||||
bool IsSubscribed(const char *channel_name) const {
|
||||
|
|
|
@ -120,9 +120,6 @@ client_new(struct player_control *player_control,
|
|||
|
||||
client->send_buf_used = 0;
|
||||
|
||||
client->messages = NULL;
|
||||
client->num_messages = 0;
|
||||
|
||||
(void)send(fd, GREETING, sizeof(GREETING) - 1, 0);
|
||||
|
||||
client_list_add(client);
|
||||
|
|
|
@ -83,27 +83,13 @@ client_push_message(Client *client, const ClientMessage &msg)
|
|||
{
|
||||
assert(client != NULL);
|
||||
|
||||
if (client->num_messages >= CLIENT_MAX_MESSAGES ||
|
||||
if (client->messages.size() >= CLIENT_MAX_MESSAGES ||
|
||||
!client->IsSubscribed(msg.GetChannel()))
|
||||
return false;
|
||||
|
||||
if (client->messages == NULL)
|
||||
if (client->messages.empty())
|
||||
client_idle_add(client, IDLE_MESSAGE);
|
||||
|
||||
client->messages = g_slist_prepend(client->messages,
|
||||
new ClientMessage(msg));
|
||||
++client->num_messages;
|
||||
|
||||
client->messages.push_back(msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
GSList *
|
||||
client_read_messages(Client *client)
|
||||
{
|
||||
GSList *messages = g_slist_reverse(client->messages);
|
||||
|
||||
client->messages = NULL;
|
||||
client->num_messages = 0;
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
|
|
@ -52,8 +52,4 @@ client_unsubscribe_all(Client *client);
|
|||
bool
|
||||
client_push_message(Client *client, const ClientMessage &msg);
|
||||
|
||||
gcc_malloc
|
||||
GSList *
|
||||
client_read_messages(Client *client);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -109,19 +109,14 @@ handle_read_messages(Client *client,
|
|||
{
|
||||
assert(argc == 1);
|
||||
|
||||
GSList *messages = client_read_messages(client);
|
||||
|
||||
for (GSList *i = messages; i != NULL; i = g_slist_next(i)) {
|
||||
ClientMessage *msg = (ClientMessage *)i->data;
|
||||
while (!client->messages.empty()) {
|
||||
const ClientMessage &msg = client->messages.front();
|
||||
|
||||
client_printf(client, "channel: %s\nmessage: %s\n",
|
||||
msg->GetChannel(),
|
||||
msg->GetMessage());
|
||||
delete msg;
|
||||
msg.GetChannel(), msg.GetMessage());
|
||||
client->messages.pop_front();
|
||||
}
|
||||
|
||||
g_slist_free(messages);
|
||||
|
||||
return COMMAND_RETURN_OK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue