task.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "task.h"
  2. #include "websocket.h"
  3. #include "parameters.h"
  4. #include <libwebsockets.h>
  5. #include <stdio.h>
  6. #include "message.h"
  7. #include "Debug.h"
  8. #include "chatcommand_sendtoall.h"
  9. #include "chatcommand_addchatroom.h"
  10. #include "chatcommand_getchatroomlist.h"
  11. #include "chatcommand_adduser.h"
  12. #include "chatcommand_login.h"
  13. #include "chatcommand_logout.h"
  14. #include "chatcommand_joinchatroom.h"
  15. #include "chatcommand_send.h"
  16. #include "chatcommand_leavechatroom.h"
  17. #include "chatcommand_getusersinchatroom.h"
  18. #include "chatcommand_removechatroom.h"
  19. #include "chatcommand_sendto.h"
  20. #include "chatcommand_removeuser.h"
  21. #include "chatcommand_nop.h"
  22. #include "chatcommand_getusers.h"
  23. #include "chatcommand_startsimplegame.h"
  24. #include "chatcommand_joinsimplegame.h"
  25. #include "chatcommand_simplegamesetdata.h"
  26. #include "chatcommand_leavesimplegame.h"
  27. #include "chatcommand_simplegamechat.h"
  28. #include "chatcommand_simplegameusers.h"
  29. #include "chatcommand_simplegameusersetdata.h"
  30. #include "chatcommand_simplegameusergetdata.h"
  31. task::task()
  32. {
  33. started = true;
  34. finished = true; // Make it look like this task finished and can be reused.
  35. _client = nullptr;
  36. _message = nullptr;
  37. }
  38. void task::closeconnection(chatclient *client,bool run_async)
  39. {
  40. DEBUG_FUNCTION
  41. _client = client;
  42. client->wsi = nullptr;
  43. DEBUG_LINE
  44. initialize_task(task_type::close_connection,run_async);
  45. DEBUG_LINE
  46. return;
  47. }
  48. void task::initialize_task(task_type tasktype,bool run_async)
  49. {
  50. _task_type = tasktype;
  51. started = false;
  52. finished = false;
  53. running_async = run_async;
  54. return;
  55. }
  56. void task::receivedmessage(chatclient *client,message *new_message,bool run_async)
  57. {
  58. DEBUG_FUNCTION
  59. _client = client;
  60. _message = new_message->clone();
  61. DEBUG_LINE
  62. initialize_task(task_type::received_message,run_async);
  63. DEBUG_LINE
  64. return;
  65. }
  66. void task::dotask()
  67. {
  68. started = true;
  69. DEBUG_FUNCTION
  70. switch (_task_type) {
  71. case task_type::close_connection:
  72. {
  73. DEBUG_LINE
  74. closeconnection();
  75. break;
  76. }
  77. case task_type::received_message:
  78. {
  79. DEBUG_LINE
  80. receivedmessage();
  81. break;
  82. }
  83. }
  84. DEBUG_LINE
  85. finished = true;
  86. // Clean up.
  87. _client = nullptr;
  88. idisposable::dereference((idisposable**)&_message);
  89. return;
  90. }
  91. void task::closeconnection()
  92. {
  93. DEBUG_FUNCTION
  94. message *new_message;
  95. // The user closed the connection.
  96. // Remove them from any chatrooms.
  97. if (_client->chatrooms != nullptr) {
  98. DEBUG_LINE
  99. biglist_iterator<chatroom *> chatroom_loop(_client->chatrooms);
  100. DEBUG_LINE
  101. while (!chatroom_loop.eof())
  102. {
  103. DEBUG_LINE
  104. // Remove this user from the chatroom.
  105. chatroom_loop.item->remove_client(_client);
  106. DEBUG_LINE
  107. // Tell all the users in this chatroom that the user left.
  108. new_message = chatcommand::userleftchatroom(0,chatroom_loop.item->chatroomid,_client->chatclientid,_client->logged_in_user);
  109. chatroom_loop.item->send_message_to_clients(new_message);
  110. idisposable::dereference((idisposable**)&new_message);
  111. DEBUG_LINE
  112. // Is this chatroom empty? Should it be deleted?
  113. if ((chatroom_loop.item->delete_if_unused)
  114. && (!chatroom_loop.item->has_clients())) {
  115. // Tell other clients that this chatroom is gone.
  116. DEBUG_LINE
  117. new_message = chatcommand::chatroomwasdeleted(0,chatroom_loop.item->chatroomid);
  118. DEBUG_LINE
  119. chatclient::send_message_to_clients(&the_websocket->chatclients,new_message);
  120. DEBUG_LINE
  121. idisposable::dereference((idisposable**)&new_message);
  122. DEBUG_LINE
  123. // Delete this chatroom.
  124. DEBUG_LINE
  125. the_websocket->chatrooms.remove(chatroom_loop.item);
  126. DEBUG_LINE
  127. chatroom_loop.row->used = false;
  128. DEBUG_LINE
  129. delete chatroom_loop.item;
  130. DEBUG_LINE
  131. }
  132. DEBUG_LINE
  133. chatroom_loop.movenext();
  134. }
  135. }
  136. DEBUG_LINE
  137. // Remove this connection from the list of connection.
  138. the_websocket->remove_client_from_simple_games(_client,true);
  139. DEBUG_LINE
  140. the_websocket->chatclients.remove(_client);
  141. DEBUG_LINE
  142. // Clear any memory _client is using.
  143. _client->clear();
  144. return;
  145. }
  146. void task::receivedmessage()
  147. {
  148. DEBUG_FUNCTION
  149. char first_letter;
  150. // Figure out the first letter of the message. This is to speed up finding a match.
  151. if ((_message != nullptr) && (_message->actual_message.length > 0)) {
  152. first_letter = _message->actual_message.data[0];
  153. } else {
  154. first_letter = ' ';
  155. }
  156. DEBUG_LINE
  157. do { // loop only once
  158. if (chatcommand_getchatroomlist::processmessage(first_letter,_message,_client))
  159. {
  160. break; // Get the list of chatrooms.
  161. }
  162. DEBUG_LINE
  163. if (chatcommand_sendtoall::processmessage(first_letter,_message,_client))
  164. {
  165. break; // Send a message to all users. Server password is required.
  166. }
  167. DEBUG_LINE
  168. if (chatcommand_addchatroom::processmessage(first_letter,_message,_client))
  169. {
  170. break; // Add a chatroom. Server password is optional.
  171. }
  172. DEBUG_LINE
  173. if (chatcommand_adduser::processmessage(first_letter,_message,_client))
  174. {
  175. break; // Add a user. Server password is required.
  176. }
  177. DEBUG_LINE
  178. if (chatcommand_login::processmessage(first_letter,_message,_client))
  179. {
  180. break; // Log in as a certain user.
  181. }
  182. DEBUG_LINE
  183. if (chatcommand_logout::processmessage(first_letter,_message,_client))
  184. {
  185. break; // log out.
  186. }
  187. DEBUG_LINE
  188. if (chatcommand_joinchatroom::processmessage(first_letter,_message,_client))
  189. {
  190. break; // join chat room.
  191. }
  192. DEBUG_LINE
  193. if (chatcommand_send::processmessage(first_letter,_message,_client))
  194. {
  195. break; // join chat room.
  196. }
  197. DEBUG_LINE
  198. if (chatcommand_leavechatroom::processmessage(first_letter,_message,_client))
  199. {
  200. break; // leave chat room.
  201. }
  202. DEBUG_LINE
  203. if (chatcommand_getusersinchatroom::processmessage(first_letter,_message,_client))
  204. {
  205. break; // get the list of users in the chatroom.
  206. }
  207. DEBUG_LINE
  208. if (chatcommand_removechatroom::processmessage(first_letter,_message,_client))
  209. {
  210. break; // removes a chatroom.
  211. }
  212. DEBUG_LINE
  213. if (chatcommand_sendto::processmessage(first_letter,_message,_client))
  214. {
  215. break; // Send a private message to a user.
  216. }
  217. DEBUG_LINE
  218. if (chatcommand_startsimplegame::processmessage(first_letter,_message,_client))
  219. {
  220. break; // Start a simple game.
  221. }
  222. DEBUG_LINE
  223. if (chatcommand_joinsimplegame::processmessage(first_letter,_message,_client))
  224. {
  225. break; // Join a simple game.
  226. }
  227. DEBUG_LINE
  228. if (chatcommand_simplegamesetdata::processmessage(first_letter,_message,_client))
  229. {
  230. break; // Set the game data on a simple game.
  231. }
  232. DEBUG_LINE
  233. if (chatcommand_leavesimplegame::processmessage(first_letter,_message,_client))
  234. {
  235. break; // Leave simple game.
  236. }
  237. DEBUG_LINE
  238. if (chatcommand_simplegamechat::processmessage(first_letter,_message,_client))
  239. {
  240. break; // Simple game chat.
  241. }
  242. DEBUG_LINE
  243. if (chatcommand_simplegameusers::processmessage(first_letter,_message,_client))
  244. {
  245. break; // Get the list of users in a simple game.
  246. }
  247. DEBUG_LINE
  248. if (chatcommand_simplegameusersetdata::processmessage(first_letter,_message,_client))
  249. {
  250. break; // Simple game user set data.
  251. }
  252. DEBUG_LINE
  253. if (chatcommand_simplegameusergetdata::processmessage(first_letter,_message,_client))
  254. {
  255. break; // Simple game user get data.
  256. }
  257. DEBUG_LINE
  258. if (chatcommand_removeuser::processmessage(first_letter,_message,_client))
  259. {
  260. break; // Removes a user.
  261. }
  262. DEBUG_LINE
  263. if (chatcommand_nop::processmessage(first_letter,_message,_client))
  264. {
  265. break; // No operation.
  266. }
  267. DEBUG_LINE
  268. if (chatcommand_getusers::processmessage(first_letter,_message,_client))
  269. {
  270. break; // Gets the list of all users.
  271. }
  272. // Send a message back to the original client.
  273. // Message wasn't understood.
  274. DEBUG_LINE
  275. chatcommand::message_not_understood(_client,_message->actual_message);
  276. } while (false);
  277. DEBUG_LINE
  278. return;
  279. }