2
0

console_server.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "socket.h"
  25. #include "container_types.h"
  26. #include "queue.h"
  27. #include "id_array.h"
  28. #include <cstdarg>
  29. namespace crown
  30. {
  31. /// Enumerates log levels.
  32. struct LogSeverity
  33. {
  34. enum Enum
  35. {
  36. INFO = 0,
  37. WARN = 1,
  38. ERROR = 2,
  39. DEBUG = 3
  40. };
  41. };
  42. struct Client
  43. {
  44. Id id;
  45. TCPSocket socket;
  46. void close()
  47. {
  48. socket.close();
  49. }
  50. };
  51. typedef IdArray<CE_MAX_CONSOLE_CLIENTS, Client> ClientArray;
  52. class ConsoleServer
  53. {
  54. public:
  55. /// Listens on the given @a port. If @a wait is true, this function
  56. /// blocks until a client is connected.
  57. void init(uint16_t port, bool wait);
  58. void shutdown();
  59. void log_to_all(LogSeverity::Enum severity, const char* message, ...);
  60. void log_to_all(LogSeverity::Enum severity, const char* message, ::va_list arg);
  61. /// Collects requests from clients and processes them all.
  62. void update();
  63. /// Sends the given JSON-encoded string to all clients.
  64. void send_to_all(const char* json);
  65. private:
  66. void send(TCPSocket client, const char* message);
  67. void add_client(TCPSocket socket);
  68. ReadResult update_client(TCPSocket client);
  69. void process(TCPSocket client, const char* request);
  70. void process_ping(TCPSocket client, const char* msg);
  71. void process_script(TCPSocket client, const char* msg);
  72. void process_stats(TCPSocket client, const char* msg);
  73. void process_command(TCPSocket client, const char* msg);
  74. void processs_filesystem(TCPSocket client, const char* msg);
  75. private:
  76. TCPSocket m_server;
  77. ClientArray m_clients;
  78. };
  79. /// Functions for accessing global console.
  80. namespace console_server_globals
  81. {
  82. // Creates the global console server.
  83. void init();
  84. /// Destroys the global console server.
  85. void shutdown();
  86. /// Returns the global console server object.
  87. ConsoleServer& console();
  88. } // namespace console_server_globals
  89. } // namespace crown