ConsoleServer.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 100
  33. struct RPCCallback
  34. {
  35. ClientId client;
  36. uint32_t message_index;
  37. };
  38. class ConsoleServer
  39. {
  40. public:
  41. /// Listens on the given @a port.
  42. ConsoleServer(uint16_t port);
  43. /// Initializes the system. If @a wait is true, this function
  44. /// blocks until a client is connected.
  45. void init(bool wait);
  46. void shutdown();
  47. void log_to_all(const char* message, LogSeverity::Enum severity);
  48. void send_message_to(ClientId client, const char* message);
  49. void send_message_to_all(const char* message);
  50. /// Collects requests from clients and processes them all.
  51. void update();
  52. private:
  53. void process_requests();
  54. void update_client(ClientId id);
  55. void add_client(TCPSocket& client);
  56. void remove_client(ClientId id);
  57. void add_request(ClientId client, uint32_t message_index);
  58. void process_ping(ClientId client, const char* msg);
  59. void process_script(ClientId client, const char* msg);
  60. void process_stats(ClientId client, const char* msg);
  61. void process_command(ClientId client, const char* msg);
  62. void processs_filesystem(ClientId client, const char* msg);
  63. private:
  64. uint16_t m_port;
  65. TCPServer m_server;
  66. uint8_t m_num_clients;
  67. IdTable<MAX_CONSOLE_CLIENTS> m_clients_table;
  68. TCPSocket m_clients[MAX_CONSOLE_CLIENTS];
  69. List<char> m_receive_buffer;
  70. Queue<RPCCallback> m_receive_callbacks;
  71. };
  72. } // namespace crown