ConsoleServer.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "ContainerTypes.h"
  26. #include "Queue.h"
  27. #include "IdArray.h"
  28. #include "Log.h"
  29. #include "Config.h"
  30. namespace crown
  31. {
  32. struct Client
  33. {
  34. Id id;
  35. TCPSocket socket;
  36. void close()
  37. {
  38. socket.close();
  39. }
  40. };
  41. typedef IdArray<CE_MAX_CONSOLE_CLIENTS, Client> ClientArray;
  42. class ConsoleServer
  43. {
  44. public:
  45. /// Listens on the given @a port. If @a wait is true, this function
  46. /// blocks until a client is connected.
  47. void init(uint16_t port, bool wait);
  48. void shutdown();
  49. void log_to_all(const char* message, LogSeverity::Enum severity);
  50. /// Collects requests from clients and processes them all.
  51. void update();
  52. private:
  53. void send(TCPSocket client, const char* message);
  54. void send_to_all(const char* message);
  55. void add_client(TCPSocket socket);
  56. ReadResult update_client(TCPSocket client);
  57. void process(TCPSocket client, const char* request);
  58. void process_ping(TCPSocket client, const char* msg);
  59. void process_script(TCPSocket client, const char* msg);
  60. void process_stats(TCPSocket client, const char* msg);
  61. void process_command(TCPSocket client, const char* msg);
  62. void processs_filesystem(TCPSocket client, const char* msg);
  63. private:
  64. TCPServer m_server;
  65. ClientArray m_clients;
  66. };
  67. } // namespace crown