123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "chatclient.h"
- #include "chatcommand.h"
- #include "simplechatgame.h"
- void chatclient::initialize(struct lws *opened_wsi,int64_t chat_client_id)
- {
- chatclientid = chat_client_id;
- logged_in_user = nullptr;
- chatrooms = nullptr;
- messages_to_send = nullptr;
- should_disconnect = false;
- wsi = opened_wsi;
- }
- void chatclient::clear()
- {
- logged_in_user = nullptr;
- if (chatrooms != nullptr) {
- delete chatrooms;
- chatrooms = nullptr;
- }
- return;
- }
- void chatclient::add_chatroom(chatroom *room)
- {
- if (chatrooms == nullptr) {
- chatrooms = new biglist<chatroom *>();
- }
- chatrooms->add(room);
- return;
- }
- void chatclient::remove_chatroom(chatroom *room)
- {
- if (chatrooms != nullptr) {
- chatrooms->remove(room);
- }
- return;
- }
- void chatclient::push_message(message **message_to_add)
- {
- // Adds a message to messages_to_send.
- // Sets *message_to_add to nullptr.
- // Also calls callback_on_writable.
- if (wsi != nullptr) {
- if (messages_to_send == nullptr) {
- messages_to_send = new concurrent_queue<message *>();
- }
- if (messages_to_send != nullptr) {
- messages_to_send->enqueue(*message_to_add);
- *message_to_add = nullptr;
- } else {
- idisposable::dereference((idisposable**)message_to_add);
- }
- callback_on_writable();
- } else {
- idisposable::dereference((idisposable**)message_to_add);
- }
- return;
- }
- void chatclient::add_message(message *message_to_add)
- {
- // Adds a message to messages_to_send.
- // Increments message_to_add->usage.
- // After calling this method, be sure to call callback_on_writable().
- // The only reason this method doesn't call callback_on_writable is that you might want to add several messages to the queue.
- if (wsi != nullptr) {
- if (messages_to_send == nullptr) {
- messages_to_send = new concurrent_queue<message *>();
- }
- if (messages_to_send != nullptr) {
- messages_to_send->enqueue(message_to_add->clone());
- }
- }
- return;
- }
- message *chatclient::get_next_message()
- {
- // Returns a message if it's available.
- // Delete the returned message with message::dereference.
- message *next_message = nullptr;
- if ((messages_to_send != nullptr) && (!messages_to_send->empty())) {
- if (!messages_to_send->dequeue(next_message)) {
- next_message = nullptr;
- }
- }
- return next_message;
- }
- int chatclient::callback_on_writable()
- {
- if (wsi == nullptr) {
- // If the client disconnected, then wsi is no longer valid.
- return 0;
- }
- return lws_callback_on_writable(wsi);
- }
- void chatclient::add_message(datastring *message_to_add)
- {
- add_message(*message_to_add);
- return;
- }
- void chatclient::add_message(datastring message_to_add)
- {
- if (wsi != nullptr) {
- message *new_message;
- new_message = new message();
- new_message->set(message_to_add.data,message_to_add.length);
- add_message(new_message);
- idisposable::dereference((idisposable**)&new_message);
- }
- return;
- }
- void chatclient::add_message(const char *message_to_add)
- {
- message *new_message;
- if (wsi != nullptr) {
- new_message = new message();
- new_message->set(message_to_add);
- add_message(new_message);
- idisposable::dereference((idisposable**)&new_message);
- }
- return;
- }
- void chatclient::send_message_to_clients(biglist<chatclient *> *clients,message *message)
- {
- // Add the message to all the queues.
- biglist_iterator<chatclient *>loop(clients);
- while (!loop.eof()) {
- loop.item->add_message(message);
- loop.movenext();
- }
- // Tell libwebsockets that there are messages to send.
- loop.movefirst();
- while (!loop.eof()) {
- loop.item->callback_on_writable();
- loop.movenext();
- }
- return;
- }
- void chatclient::disconnect()
- {
- should_disconnect = true;
- callback_on_writable();
- }
- void chatclient::send_yourchatclientid()
- {
- message *new_message;
- new_message = chatcommand::yourchatclientid(chatclientid);
- push_message(&new_message);
- return;
- }
|