console_server.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "console_server.h"
  6. #include "temp_allocator.h"
  7. #include "string_stream.h"
  8. #include "device.h"
  9. #include "lua_environment.h"
  10. #include "memory.h"
  11. #include "dynamic_string.h"
  12. #include "json.h"
  13. #include "map.h"
  14. namespace crown
  15. {
  16. ConsoleServer::ConsoleServer(uint16_t port, bool wait)
  17. : _clients(default_allocator())
  18. {
  19. _server.bind(port);
  20. _server.listen(5);
  21. if (wait)
  22. {
  23. AcceptResult result;
  24. TCPSocket client;
  25. do
  26. {
  27. result = _server.accept(client);
  28. }
  29. while (result.error != AcceptResult::NO_ERROR);
  30. add_client(client);
  31. }
  32. }
  33. void ConsoleServer::shutdown()
  34. {
  35. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  36. _clients[i].close();
  37. _server.close();
  38. }
  39. namespace console_server_internal
  40. {
  41. StringStream& sanitize(StringStream& ss, const char* msg)
  42. {
  43. using namespace string_stream;
  44. const char* ch = msg;
  45. for (; *ch; ch++)
  46. {
  47. if (*ch == '"')
  48. ss << "\\";
  49. ss << *ch;
  50. }
  51. return ss;
  52. }
  53. }
  54. void ConsoleServer::log(const char* msg, LogSeverity::Enum severity)
  55. {
  56. using namespace string_stream;
  57. using namespace console_server_internal;
  58. static const char* stt[] = { "info", "warning", "error", "debug" };
  59. // Build json message
  60. TempAllocator2048 alloc;
  61. StringStream json(alloc);
  62. json << "{\"type\":\"message\",";
  63. json << "\"severity\":\"" << stt[severity] << "\",";
  64. json << "\"message\":\""; sanitize(json, msg) << "\"}";
  65. send(c_str(json));
  66. }
  67. void ConsoleServer::send(TCPSocket client, const char* json)
  68. {
  69. uint32_t len = strlen(json);
  70. client.write((const char*)&len, 4);
  71. client.write(json, len);
  72. }
  73. void ConsoleServer::send(const char* json)
  74. {
  75. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  76. send(_clients[i].socket, json);
  77. }
  78. void ConsoleServer::update()
  79. {
  80. TCPSocket client;
  81. AcceptResult result = _server.accept_nonblock(client);
  82. if (result.error == AcceptResult::NO_ERROR)
  83. add_client(client);
  84. TempAllocator256 alloc;
  85. Array<uint32_t> to_remove(alloc);
  86. // Update all clients
  87. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  88. {
  89. ReadResult rr = update_client(_clients[i].socket);
  90. if (rr.error != ReadResult::NO_ERROR)
  91. array::push_back(to_remove, i);
  92. }
  93. // Remove clients
  94. for (uint32_t i = 0; i < array::size(to_remove); ++i)
  95. {
  96. const uint32_t last = vector::size(_clients) - 1;
  97. const uint32_t c = to_remove[i];
  98. _clients[c].close();
  99. _clients[c] = _clients[last];
  100. vector::pop_back(_clients);
  101. }
  102. }
  103. void ConsoleServer::add_client(TCPSocket socket)
  104. {
  105. Client cl;
  106. cl.socket = socket;
  107. vector::push_back(_clients, cl);
  108. }
  109. ReadResult ConsoleServer::update_client(TCPSocket client)
  110. {
  111. uint32_t msg_len = 0;
  112. ReadResult rr = client.read_nonblock(&msg_len, 4);
  113. // If no data received, return
  114. if (rr.error == ReadResult::NO_ERROR && rr.bytes_read == 0) return rr;
  115. if (rr.error == ReadResult::REMOTE_CLOSED) return rr;
  116. if (rr.error != ReadResult::NO_ERROR) return rr;
  117. // Else read the message
  118. TempAllocator4096 ta;
  119. Array<char> msg_buf(ta);
  120. array::resize(msg_buf, msg_len);
  121. ReadResult msg_result = client.read(array::begin(msg_buf), msg_len);
  122. array::push_back(msg_buf, '\0');
  123. if (msg_result.error == ReadResult::REMOTE_CLOSED) return msg_result;
  124. if (msg_result.error != ReadResult::NO_ERROR) return msg_result;
  125. process(client, array::begin(msg_buf));
  126. return msg_result;
  127. }
  128. void ConsoleServer::process(TCPSocket client, const char* json)
  129. {
  130. TempAllocator4096 ta;
  131. Map<DynamicString, const char*> root(ta);
  132. json::parse_object(json, root);
  133. DynamicString type(ta);
  134. json::parse_string(root["type"], type);
  135. if (type == "ping") process_ping(client, json);
  136. else if (type == "script") process_script(client, json);
  137. else if (type == "command") process_command(client, json);
  138. else CE_FATAL("Request unknown.");
  139. }
  140. void ConsoleServer::process_ping(TCPSocket client, const char* /*json*/)
  141. {
  142. send(client, "{\"type\":\"pong\"}");
  143. }
  144. void ConsoleServer::process_script(TCPSocket /*client*/, const char* json)
  145. {
  146. TempAllocator4096 ta;
  147. Map<DynamicString, const char*> root(ta);
  148. json::parse_object(json, root);
  149. DynamicString script(ta);
  150. json::parse_string(root["script"], script);
  151. device()->lua_environment()->execute_string(script.c_str());
  152. }
  153. void ConsoleServer::process_command(TCPSocket /*client*/, const char* json)
  154. {
  155. TempAllocator4096 ta;
  156. Map<DynamicString, const char*> root(ta);
  157. json::parse_object(json, root);
  158. DynamicString cmd(ta);
  159. json::parse_string(root["command"], cmd);
  160. if (cmd == "reload")
  161. {
  162. DynamicString type(ta);
  163. DynamicString name(ta);
  164. json::parse_string(root["resource_type"], type);
  165. json::parse_string(root["resource_name"], name);
  166. device()->reload(StringId64(type.c_str()), StringId64(name.c_str()));
  167. }
  168. else if (cmd == "pause")
  169. {
  170. device()->pause();
  171. }
  172. else if (cmd == "unpause")
  173. {
  174. device()->unpause();
  175. }
  176. }
  177. namespace console_server_globals
  178. {
  179. char _buffer[sizeof(ConsoleServer)];
  180. ConsoleServer* _console = NULL;
  181. void init(uint16_t port, bool wait)
  182. {
  183. _console = new (_buffer) ConsoleServer(port, wait);
  184. }
  185. void shutdown()
  186. {
  187. _console->~ConsoleServer();
  188. _console = NULL;
  189. }
  190. void update()
  191. {
  192. #if CROWN_DEBUG
  193. _console->update();
  194. #endif // CROWN_DEBUG
  195. }
  196. ConsoleServer& console()
  197. {
  198. return *_console;
  199. }
  200. } // namespace console_server
  201. } // namespace crown