ConsoleServer.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "OsSocket.h"
  25. #include "List.h"
  26. #include "Queue.h"
  27. #include "IdTable.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. typedef Id ClientId;
  32. #define MAX_CONSOLE_CLIENTS 16
  33. struct RPCCallback
  34. {
  35. ClientId client;
  36. uint32_t message_index;
  37. };
  38. class ConsoleServer
  39. {
  40. public:
  41. ConsoleServer();
  42. /// Initializes the system. If @a wait is true, this function
  43. /// blocks until a client is connected.
  44. void init(bool wait);
  45. void shutdown();
  46. void log_to_all(const char* message, LogSeverity::Enum severity);
  47. void send_message_to(ClientId client, const char* message);
  48. void send_message_to_all(const char* message);
  49. /// Collects requests from clients without processing them.
  50. void update();
  51. /// Processes all the requests collected by update() possibly accessing
  52. /// global resources. It should be called only when "it is safe".
  53. void process_requests();
  54. private:
  55. void update_client(ClientId id);
  56. void add_client(TCPSocket& client);
  57. void remove_client(ClientId id);
  58. void add_request(ClientId client, uint32_t message_index);
  59. void process_ping(ClientId client, const char* msg);
  60. void process_script(ClientId client, const char* msg);
  61. void process_stats(ClientId client, const char* msg);
  62. void process_command(ClientId client, const char* msg);
  63. private:
  64. TCPListener m_listener;
  65. uint8_t m_num_clients;
  66. IdTable<MAX_CONSOLE_CLIENTS> m_clients_table;
  67. TCPSocket m_clients[MAX_CONSOLE_CLIENTS];
  68. List<char> m_receive_buffer;
  69. List<char> m_send_buffer;
  70. Queue<RPCCallback> m_receive_callbacks;
  71. Queue<RPCCallback> m_send_callbacks;
  72. };
  73. } // namespace crown