ClientMessage: turn into a class

This commit is contained in:
Max Kellermann 2013-01-03 17:58:22 +01:00
parent d919f8d50a
commit 5155b0f608
5 changed files with 34 additions and 109 deletions

View File

@ -40,57 +40,3 @@ client_message_valid_channel_name(const char *name)
return true;
}
void
client_message_init_null(struct client_message *msg)
{
assert(msg != NULL);
msg->channel = NULL;
msg->message = NULL;
}
void
client_message_init(struct client_message *msg,
const char *channel, const char *message)
{
assert(msg != NULL);
msg->channel = g_strdup(channel);
msg->message = g_strdup(message);
}
void
client_message_copy(struct client_message *dest,
const struct client_message *src)
{
assert(dest != NULL);
assert(src != NULL);
assert(client_message_defined(src));
client_message_init(dest, src->channel, src->message);
}
struct client_message *
client_message_dup(const struct client_message *src)
{
struct client_message *dest = g_slice_new(struct client_message);
client_message_copy(dest, src);
return dest;
}
void
client_message_deinit(struct client_message *msg)
{
assert(msg != NULL);
g_free(msg->channel);
g_free(msg->message);
}
void
client_message_free(struct client_message *msg)
{
client_message_deinit(msg);
g_slice_free(struct client_message, msg);
}

View File

@ -22,52 +22,31 @@
#include "gcc.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <string>
/**
* A client-to-client message.
*/
struct client_message {
char *channel;
class ClientMessage {
std::string channel, message;
char *message;
public:
template<typename T, typename U>
ClientMessage(T &&_channel, U &&_message)
:channel(std::forward<T>(_channel)),
message(std::forward<U>(_message)) {}
const char *GetChannel() const {
return channel.c_str();
}
const char *GetMessage() const {
return message.c_str();
}
};
gcc_pure
bool
client_message_valid_channel_name(const char *name);
gcc_pure
static inline bool
client_message_defined(const struct client_message *msg)
{
assert(msg != NULL);
assert((msg->channel == NULL) == (msg->message == NULL));
return msg->channel != NULL;
}
void
client_message_init_null(struct client_message *msg);
void
client_message_init(struct client_message *msg,
const char *channel, const char *message);
void
client_message_copy(struct client_message *dest,
const struct client_message *src);
gcc_malloc
struct client_message *
client_message_dup(const struct client_message *src);
void
client_message_deinit(struct client_message *msg);
void
client_message_free(struct client_message *msg);
#endif

View File

@ -25,6 +25,7 @@ extern "C" {
#include "idle.h"
}
#include <assert.h>
#include <string.h>
enum client_subscribe_result
@ -78,21 +79,19 @@ client_unsubscribe_all(Client *client)
}
bool
client_push_message(Client *client, const struct client_message *msg)
client_push_message(Client *client, const ClientMessage &msg)
{
assert(client != NULL);
assert(msg != NULL);
assert(client_message_defined(msg));
if (client->num_messages >= CLIENT_MAX_MESSAGES ||
!client->IsSubscribed(msg->channel))
!client->IsSubscribed(msg.GetChannel()))
return false;
if (client->messages == NULL)
client_idle_add(client, IDLE_MESSAGE);
client->messages = g_slist_prepend(client->messages,
client_message_dup(msg));
new ClientMessage(msg));
++client->num_messages;
return true;

View File

@ -24,7 +24,7 @@
typedef struct _GSList GSList;
class Client;
struct client_message;
class ClientMessage;
enum client_subscribe_result {
/** success */
@ -50,7 +50,7 @@ void
client_unsubscribe_all(Client *client);
bool
client_push_message(Client *client, const struct client_message *msg);
client_push_message(Client *client, const ClientMessage &msg);
gcc_malloc
GSList *

View File

@ -112,11 +112,12 @@ handle_read_messages(Client *client,
GSList *messages = client_read_messages(client);
for (GSList *i = messages; i != NULL; i = g_slist_next(i)) {
struct client_message *msg = (struct client_message *)i->data;
ClientMessage *msg = (ClientMessage *)i->data;
client_printf(client, "channel: %s\nmessage: %s\n",
msg->channel, msg->message);
client_message_free(msg);
msg->GetChannel(),
msg->GetMessage());
delete msg;
}
g_slist_free(messages);
@ -125,9 +126,14 @@ handle_read_messages(Client *client,
}
struct send_message_context {
struct client_message msg;
ClientMessage msg;
bool sent;
template<typename T, typename U>
send_message_context(T &&_channel, U &&_message)
:msg(std::forward<T>(_channel), std::forward<U>(_message)),
sent(false) {}
};
static void
@ -137,7 +143,7 @@ send_message(gpointer data, gpointer user_data)
(struct send_message_context *)user_data;
Client *client = (Client *)data;
if (client_push_message(client, &context->msg))
if (client_push_message(client, context->msg))
context->sent = true;
}
@ -153,15 +159,10 @@ handle_send_message(Client *client,
return COMMAND_RETURN_ERROR;
}
struct send_message_context context;
context.sent = false;
client_message_init(&context.msg, argv[1], argv[2]);
struct send_message_context context(argv[1], argv[2]);
client_list_foreach(send_message, &context);
client_message_deinit(&context.msg);
if (context.sent)
return COMMAND_RETURN_OK;
else {