chatclient.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "chatclient.h"
  2. #include "chatcommand.h"
  3. #include "simplechatgame.h"
  4. void chatclient::initialize(struct lws *opened_wsi,int64_t chat_client_id)
  5. {
  6. chatclientid = chat_client_id;
  7. logged_in_user = nullptr;
  8. chatrooms = nullptr;
  9. messages_to_send = nullptr;
  10. should_disconnect = false;
  11. wsi = opened_wsi;
  12. }
  13. void chatclient::clear()
  14. {
  15. logged_in_user = nullptr;
  16. if (chatrooms != nullptr) {
  17. delete chatrooms;
  18. chatrooms = nullptr;
  19. }
  20. return;
  21. }
  22. void chatclient::add_chatroom(chatroom *room)
  23. {
  24. if (chatrooms == nullptr) {
  25. chatrooms = new biglist<chatroom *>();
  26. }
  27. chatrooms->add(room);
  28. return;
  29. }
  30. void chatclient::remove_chatroom(chatroom *room)
  31. {
  32. if (chatrooms != nullptr) {
  33. chatrooms->remove(room);
  34. }
  35. return;
  36. }
  37. void chatclient::push_message(message **message_to_add)
  38. {
  39. // Adds a message to messages_to_send.
  40. // Sets *message_to_add to nullptr.
  41. // Also calls callback_on_writable.
  42. if (wsi != nullptr) {
  43. if (messages_to_send == nullptr) {
  44. messages_to_send = new concurrent_queue<message *>();
  45. }
  46. if (messages_to_send != nullptr) {
  47. messages_to_send->enqueue(*message_to_add);
  48. *message_to_add = nullptr;
  49. } else {
  50. idisposable::dereference((idisposable**)message_to_add);
  51. }
  52. callback_on_writable();
  53. } else {
  54. idisposable::dereference((idisposable**)message_to_add);
  55. }
  56. return;
  57. }
  58. void chatclient::add_message(message *message_to_add)
  59. {
  60. // Adds a message to messages_to_send.
  61. // Increments message_to_add->usage.
  62. // After calling this method, be sure to call callback_on_writable().
  63. // The only reason this method doesn't call callback_on_writable is that you might want to add several messages to the queue.
  64. if (wsi != nullptr) {
  65. if (messages_to_send == nullptr) {
  66. messages_to_send = new concurrent_queue<message *>();
  67. }
  68. if (messages_to_send != nullptr) {
  69. messages_to_send->enqueue(message_to_add->clone());
  70. }
  71. }
  72. return;
  73. }
  74. message *chatclient::get_next_message()
  75. {
  76. // Returns a message if it's available.
  77. // Delete the returned message with message::dereference.
  78. message *next_message = nullptr;
  79. if ((messages_to_send != nullptr) && (!messages_to_send->empty())) {
  80. if (!messages_to_send->dequeue(next_message)) {
  81. next_message = nullptr;
  82. }
  83. }
  84. return next_message;
  85. }
  86. int chatclient::callback_on_writable()
  87. {
  88. if (wsi == nullptr) {
  89. // If the client disconnected, then wsi is no longer valid.
  90. return 0;
  91. }
  92. return lws_callback_on_writable(wsi);
  93. }
  94. void chatclient::add_message(datastring *message_to_add)
  95. {
  96. add_message(*message_to_add);
  97. return;
  98. }
  99. void chatclient::add_message(datastring message_to_add)
  100. {
  101. if (wsi != nullptr) {
  102. message *new_message;
  103. new_message = new message();
  104. new_message->set(message_to_add.data,message_to_add.length);
  105. add_message(new_message);
  106. idisposable::dereference((idisposable**)&new_message);
  107. }
  108. return;
  109. }
  110. void chatclient::add_message(const char *message_to_add)
  111. {
  112. message *new_message;
  113. if (wsi != nullptr) {
  114. new_message = new message();
  115. new_message->set(message_to_add);
  116. add_message(new_message);
  117. idisposable::dereference((idisposable**)&new_message);
  118. }
  119. return;
  120. }
  121. void chatclient::send_message_to_clients(biglist<chatclient *> *clients,message *message)
  122. {
  123. // Add the message to all the queues.
  124. biglist_iterator<chatclient *>loop(clients);
  125. while (!loop.eof()) {
  126. loop.item->add_message(message);
  127. loop.movenext();
  128. }
  129. // Tell libwebsockets that there are messages to send.
  130. loop.movefirst();
  131. while (!loop.eof()) {
  132. loop.item->callback_on_writable();
  133. loop.movenext();
  134. }
  135. return;
  136. }
  137. void chatclient::disconnect()
  138. {
  139. should_disconnect = true;
  140. callback_on_writable();
  141. }
  142. void chatclient::send_yourchatclientid()
  143. {
  144. message *new_message;
  145. new_message = chatcommand::yourchatclientid(chatclientid);
  146. push_message(&new_message);
  147. return;
  148. }