console_server.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/containers/hash_map.h"
  6. #include "core/json/json_object.h"
  7. #include "core/json/sjson.h"
  8. #include "core/memory/temp_allocator.h"
  9. #include "core/strings/string_id.h"
  10. #include "core/strings/string_stream.h"
  11. #include "device/console_server.h"
  12. namespace crown
  13. {
  14. ConsoleServer::ConsoleServer(Allocator& a)
  15. : _clients(a)
  16. , _commands(a)
  17. {
  18. }
  19. void ConsoleServer::listen(u16 port, bool wait)
  20. {
  21. _server.bind(port);
  22. _server.listen(5);
  23. if (wait)
  24. {
  25. AcceptResult ar;
  26. TCPSocket client;
  27. do
  28. {
  29. ar = _server.accept(client);
  30. }
  31. while (ar.error != AcceptResult::SUCCESS);
  32. array::push_back(_clients, client);
  33. }
  34. }
  35. void ConsoleServer::shutdown()
  36. {
  37. for (u32 i = 0; i < array::size(_clients); ++i)
  38. _clients[i].close();
  39. _server.close();
  40. }
  41. void ConsoleServer::send(TCPSocket client, const char* json)
  42. {
  43. u32 len = strlen32(json);
  44. client.write(&len, 4);
  45. client.write(json, len);
  46. }
  47. void ConsoleServer::error(TCPSocket client, const char* msg)
  48. {
  49. TempAllocator4096 ta;
  50. StringStream ss(ta);
  51. ss << "{\"type\":\"error\",\"message\":\"" << msg << "\"}";
  52. send(client, string_stream::c_str(ss));
  53. }
  54. void ConsoleServer::log(LogSeverity::Enum sev, const char* system, const char* msg)
  55. {
  56. const char* severity_map[] = { "info", "warning", "error" };
  57. CE_STATIC_ASSERT(countof(severity_map) == LogSeverity::COUNT);
  58. TempAllocator4096 ta;
  59. StringStream ss(ta);
  60. ss << "{\"type\":\"message\",\"severity\":\"";
  61. ss << severity_map[sev];
  62. ss << "\",\"system\":\"";
  63. ss << system;
  64. ss << "\",\"message\":\"";
  65. // Sanitize msg
  66. const char* ch = msg;
  67. for (; *ch; ch++)
  68. {
  69. if (*ch == '"' || *ch == '\\')
  70. ss << "\\";
  71. ss << *ch;
  72. }
  73. ss << "\"}";
  74. send(string_stream::c_str(ss));
  75. }
  76. void ConsoleServer::send(const char* json)
  77. {
  78. for (u32 i = 0; i < array::size(_clients); ++i)
  79. send(_clients[i], json);
  80. }
  81. void ConsoleServer::update()
  82. {
  83. TCPSocket client;
  84. AcceptResult ar = _server.accept_nonblock(client);
  85. if (ar.error == AcceptResult::SUCCESS)
  86. array::push_back(_clients, client);
  87. TempAllocator256 alloc;
  88. Array<u32> to_remove(alloc);
  89. // Update all clients
  90. for (u32 i = 0; i < array::size(_clients); ++i)
  91. {
  92. for (;;)
  93. {
  94. u32 msg_len = 0;
  95. ReadResult rr = _clients[i].read_nonblock(&msg_len, 4);
  96. if (rr.error == ReadResult::WOULDBLOCK)
  97. break;
  98. if (rr.error != ReadResult::SUCCESS)
  99. {
  100. array::push_back(to_remove, i);
  101. break;
  102. }
  103. // Read message
  104. TempAllocator4096 ta;
  105. Array<char> msg(ta);
  106. array::resize(msg, msg_len + 1);
  107. rr = _clients[i].read(array::begin(msg), msg_len);
  108. array::push_back(msg, '\0');
  109. if (rr.error != ReadResult::SUCCESS)
  110. {
  111. array::push_back(to_remove, i);
  112. break;
  113. }
  114. // Process message
  115. JsonObject obj(ta);
  116. sjson::parse(array::begin(msg), obj);
  117. Command cmd;
  118. cmd.function = NULL;
  119. cmd.user_data = NULL;
  120. cmd = hash_map::get(_commands
  121. , sjson::parse_string_id(obj["type"])
  122. , cmd
  123. );
  124. if (cmd.function)
  125. cmd.function(*this, _clients[i], array::begin(msg), cmd.user_data);
  126. else
  127. error(_clients[i], "Unknown command");
  128. }
  129. }
  130. // Remove clients
  131. for (u32 i = 0; i < array::size(to_remove); ++i)
  132. {
  133. const u32 last = array::size(_clients) - 1;
  134. const u32 c = to_remove[i];
  135. _clients[c].close();
  136. _clients[c] = _clients[last];
  137. array::pop_back(_clients);
  138. }
  139. }
  140. void ConsoleServer::register_command(const char* type, CommandFunction function, void* user_data)
  141. {
  142. CE_ENSURE(NULL != type);
  143. CE_ENSURE(NULL != function);
  144. Command cmd;
  145. cmd.function = function;
  146. cmd.user_data = user_data;
  147. hash_map::set(_commands, StringId32(type), cmd);
  148. }
  149. namespace console_server_globals
  150. {
  151. ConsoleServer* _console_server = NULL;
  152. void init()
  153. {
  154. _console_server = CE_NEW(default_allocator(), ConsoleServer)(default_allocator());
  155. }
  156. void shutdown()
  157. {
  158. _console_server->shutdown();
  159. CE_DELETE(default_allocator(), _console_server);
  160. _console_server = NULL;
  161. }
  162. } // namespace console_server_globals
  163. ConsoleServer* console_server()
  164. {
  165. return console_server_globals::_console_server;
  166. }
  167. } // namespace crown