123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "task.h"
- #include "websocket.h"
- #include "parameters.h"
- #include <libwebsockets.h>
- #include <stdio.h>
- #include "message.h"
- #include "Debug.h"
- #include "chatcommand_sendtoall.h"
- #include "chatcommand_addchatroom.h"
- #include "chatcommand_getchatroomlist.h"
- #include "chatcommand_adduser.h"
- #include "chatcommand_login.h"
- #include "chatcommand_logout.h"
- #include "chatcommand_joinchatroom.h"
- #include "chatcommand_send.h"
- #include "chatcommand_leavechatroom.h"
- #include "chatcommand_getusersinchatroom.h"
- #include "chatcommand_removechatroom.h"
- #include "chatcommand_sendto.h"
- #include "chatcommand_removeuser.h"
- #include "chatcommand_nop.h"
- #include "chatcommand_getusers.h"
- #include "chatcommand_startsimplegame.h"
- #include "chatcommand_joinsimplegame.h"
- #include "chatcommand_simplegamesetdata.h"
- #include "chatcommand_leavesimplegame.h"
- #include "chatcommand_simplegamechat.h"
- #include "chatcommand_simplegameusers.h"
- #include "chatcommand_simplegameusersetdata.h"
- #include "chatcommand_simplegameusergetdata.h"
- task::task()
- {
- started = true;
- finished = true; // Make it look like this task finished and can be reused.
- _client = nullptr;
- _message = nullptr;
- }
- void task::closeconnection(chatclient *client,bool run_async)
- {
- DEBUG_FUNCTION
- _client = client;
- client->wsi = nullptr;
- DEBUG_LINE
- initialize_task(task_type::close_connection,run_async);
- DEBUG_LINE
- return;
- }
- void task::initialize_task(task_type tasktype,bool run_async)
- {
- _task_type = tasktype;
- started = false;
- finished = false;
- running_async = run_async;
- return;
- }
- void task::receivedmessage(chatclient *client,message *new_message,bool run_async)
- {
- DEBUG_FUNCTION
- _client = client;
- _message = new_message->clone();
- DEBUG_LINE
- initialize_task(task_type::received_message,run_async);
- DEBUG_LINE
- return;
- }
- void task::dotask()
- {
- started = true;
- DEBUG_FUNCTION
- switch (_task_type) {
- case task_type::close_connection:
- {
- DEBUG_LINE
- closeconnection();
- break;
- }
- case task_type::received_message:
- {
- DEBUG_LINE
- receivedmessage();
- break;
- }
- }
- DEBUG_LINE
- finished = true;
- // Clean up.
- _client = nullptr;
- idisposable::dereference((idisposable**)&_message);
- return;
- }
- void task::closeconnection()
- {
- DEBUG_FUNCTION
- message *new_message;
- // The user closed the connection.
- // Remove them from any chatrooms.
- if (_client->chatrooms != nullptr) {
- DEBUG_LINE
- biglist_iterator<chatroom *> chatroom_loop(_client->chatrooms);
- DEBUG_LINE
- while (!chatroom_loop.eof())
- {
- DEBUG_LINE
- // Remove this user from the chatroom.
- chatroom_loop.item->remove_client(_client);
- DEBUG_LINE
- // Tell all the users in this chatroom that the user left.
- new_message = chatcommand::userleftchatroom(0,chatroom_loop.item->chatroomid,_client->chatclientid,_client->logged_in_user);
- chatroom_loop.item->send_message_to_clients(new_message);
- idisposable::dereference((idisposable**)&new_message);
- DEBUG_LINE
-
- // Is this chatroom empty? Should it be deleted?
- if ((chatroom_loop.item->delete_if_unused)
- && (!chatroom_loop.item->has_clients())) {
- // Tell other clients that this chatroom is gone.
- DEBUG_LINE
- new_message = chatcommand::chatroomwasdeleted(0,chatroom_loop.item->chatroomid);
- DEBUG_LINE
- chatclient::send_message_to_clients(&the_websocket->chatclients,new_message);
- DEBUG_LINE
- idisposable::dereference((idisposable**)&new_message);
- DEBUG_LINE
- // Delete this chatroom.
- DEBUG_LINE
- the_websocket->chatrooms.remove(chatroom_loop.item);
- DEBUG_LINE
- chatroom_loop.row->used = false;
- DEBUG_LINE
- delete chatroom_loop.item;
- DEBUG_LINE
- }
- DEBUG_LINE
- chatroom_loop.movenext();
- }
- }
- DEBUG_LINE
- // Remove this connection from the list of connection.
- the_websocket->remove_client_from_simple_games(_client,true);
- DEBUG_LINE
- the_websocket->chatclients.remove(_client);
- DEBUG_LINE
- // Clear any memory _client is using.
-
- _client->clear();
- return;
- }
- void task::receivedmessage()
- {
- DEBUG_FUNCTION
- char first_letter;
- // Figure out the first letter of the message. This is to speed up finding a match.
- if ((_message != nullptr) && (_message->actual_message.length > 0)) {
- first_letter = _message->actual_message.data[0];
- } else {
- first_letter = ' ';
- }
- DEBUG_LINE
- do { // loop only once
- if (chatcommand_getchatroomlist::processmessage(first_letter,_message,_client))
- {
- break; // Get the list of chatrooms.
- }
- DEBUG_LINE
- if (chatcommand_sendtoall::processmessage(first_letter,_message,_client))
- {
- break; // Send a message to all users. Server password is required.
- }
- DEBUG_LINE
- if (chatcommand_addchatroom::processmessage(first_letter,_message,_client))
- {
- break; // Add a chatroom. Server password is optional.
- }
- DEBUG_LINE
- if (chatcommand_adduser::processmessage(first_letter,_message,_client))
- {
- break; // Add a user. Server password is required.
- }
- DEBUG_LINE
- if (chatcommand_login::processmessage(first_letter,_message,_client))
- {
- break; // Log in as a certain user.
- }
- DEBUG_LINE
- if (chatcommand_logout::processmessage(first_letter,_message,_client))
- {
- break; // log out.
- }
- DEBUG_LINE
- if (chatcommand_joinchatroom::processmessage(first_letter,_message,_client))
- {
- break; // join chat room.
- }
- DEBUG_LINE
- if (chatcommand_send::processmessage(first_letter,_message,_client))
- {
- break; // join chat room.
- }
- DEBUG_LINE
- if (chatcommand_leavechatroom::processmessage(first_letter,_message,_client))
- {
- break; // leave chat room.
- }
- DEBUG_LINE
- if (chatcommand_getusersinchatroom::processmessage(first_letter,_message,_client))
- {
- break; // get the list of users in the chatroom.
- }
- DEBUG_LINE
- if (chatcommand_removechatroom::processmessage(first_letter,_message,_client))
- {
- break; // removes a chatroom.
- }
- DEBUG_LINE
- if (chatcommand_sendto::processmessage(first_letter,_message,_client))
- {
- break; // Send a private message to a user.
- }
- DEBUG_LINE
- if (chatcommand_startsimplegame::processmessage(first_letter,_message,_client))
- {
- break; // Start a simple game.
- }
- DEBUG_LINE
- if (chatcommand_joinsimplegame::processmessage(first_letter,_message,_client))
- {
- break; // Join a simple game.
- }
- DEBUG_LINE
- if (chatcommand_simplegamesetdata::processmessage(first_letter,_message,_client))
- {
- break; // Set the game data on a simple game.
- }
- DEBUG_LINE
- if (chatcommand_leavesimplegame::processmessage(first_letter,_message,_client))
- {
- break; // Leave simple game.
- }
- DEBUG_LINE
- if (chatcommand_simplegamechat::processmessage(first_letter,_message,_client))
- {
- break; // Simple game chat.
- }
- DEBUG_LINE
- if (chatcommand_simplegameusers::processmessage(first_letter,_message,_client))
- {
- break; // Get the list of users in a simple game.
- }
- DEBUG_LINE
- if (chatcommand_simplegameusersetdata::processmessage(first_letter,_message,_client))
- {
- break; // Simple game user set data.
- }
- DEBUG_LINE
- if (chatcommand_simplegameusergetdata::processmessage(first_letter,_message,_client))
- {
- break; // Simple game user get data.
- }
- DEBUG_LINE
- if (chatcommand_removeuser::processmessage(first_letter,_message,_client))
- {
- break; // Removes a user.
- }
- DEBUG_LINE
- if (chatcommand_nop::processmessage(first_letter,_message,_client))
- {
- break; // No operation.
- }
- DEBUG_LINE
- if (chatcommand_getusers::processmessage(first_letter,_message,_client))
- {
- break; // Gets the list of all users.
- }
-
- // Send a message back to the original client.
- // Message wasn't understood.
- DEBUG_LINE
- chatcommand::message_not_understood(_client,_message->actual_message);
- } while (false);
- DEBUG_LINE
-
- return;
-
- }
|