console_server.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "bundle_compiler.h"
  15. namespace crown
  16. {
  17. ConsoleServer::ConsoleServer(uint16_t port, bool wait)
  18. : _clients(default_allocator())
  19. {
  20. _server.bind(port);
  21. _server.listen(5);
  22. if (wait)
  23. {
  24. AcceptResult ar;
  25. TCPSocket client;
  26. do
  27. {
  28. ar = _server.accept(client);
  29. }
  30. while (ar.error != AcceptResult::NO_ERROR);
  31. add_client(client);
  32. }
  33. }
  34. void ConsoleServer::shutdown()
  35. {
  36. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  37. _clients[i].close();
  38. _server.close();
  39. }
  40. void ConsoleServer::send(TCPSocket client, const char* json)
  41. {
  42. uint32_t len = strlen32(json);
  43. client.write((const char*)&len, 4);
  44. client.write(json, len);
  45. }
  46. void ConsoleServer::send(const char* json)
  47. {
  48. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  49. send(_clients[i], json);
  50. }
  51. void ConsoleServer::update()
  52. {
  53. TCPSocket client;
  54. AcceptResult result = _server.accept_nonblock(client);
  55. if (result.error == AcceptResult::NO_ERROR)
  56. add_client(client);
  57. TempAllocator256 alloc;
  58. Array<uint32_t> to_remove(alloc);
  59. // Update all clients
  60. for (uint32_t i = 0; i < vector::size(_clients); ++i)
  61. {
  62. ReadResult rr = update_client(_clients[i]);
  63. if (rr.error != ReadResult::NO_ERROR)
  64. array::push_back(to_remove, i);
  65. }
  66. // Remove clients
  67. for (uint32_t i = 0; i < array::size(to_remove); ++i)
  68. {
  69. const uint32_t last = vector::size(_clients) - 1;
  70. const uint32_t c = to_remove[i];
  71. _clients[c].close();
  72. _clients[c] = _clients[last];
  73. vector::pop_back(_clients);
  74. }
  75. }
  76. void ConsoleServer::add_client(TCPSocket socket)
  77. {
  78. vector::push_back(_clients, socket);
  79. }
  80. ReadResult ConsoleServer::update_client(TCPSocket client)
  81. {
  82. uint32_t msg_len = 0;
  83. ReadResult rr = client.read_nonblock(&msg_len, 4);
  84. // If no data received, return
  85. if (rr.error == ReadResult::NO_ERROR && rr.bytes_read == 0) return rr;
  86. if (rr.error == ReadResult::REMOTE_CLOSED) return rr;
  87. if (rr.error != ReadResult::NO_ERROR) return rr;
  88. // Else read the message
  89. TempAllocator4096 ta;
  90. Array<char> msg_buf(ta);
  91. array::resize(msg_buf, msg_len);
  92. ReadResult msg_result = client.read(array::begin(msg_buf), msg_len);
  93. array::push_back(msg_buf, '\0');
  94. if (msg_result.error == ReadResult::REMOTE_CLOSED) return msg_result;
  95. if (msg_result.error != ReadResult::NO_ERROR) return msg_result;
  96. process(client, array::begin(msg_buf));
  97. return msg_result;
  98. }
  99. void ConsoleServer::process(TCPSocket client, const char* json)
  100. {
  101. TempAllocator4096 ta;
  102. JsonObject root(ta);
  103. json::parse(json, root);
  104. DynamicString type(ta);
  105. json::parse_string(root["type"], type);
  106. if (type == "ping") process_ping(client, json);
  107. else if (type == "script") process_script(client, json);
  108. else if (type == "command") process_command(client, json);
  109. else CE_FATAL("Request unknown.");
  110. }
  111. void ConsoleServer::process_ping(TCPSocket client, const char* /*json*/)
  112. {
  113. send(client, "{\"type\":\"pong\"}");
  114. }
  115. void ConsoleServer::process_script(TCPSocket /*client*/, const char* json)
  116. {
  117. TempAllocator4096 ta;
  118. JsonObject root(ta);
  119. json::parse(json, root);
  120. DynamicString script(ta);
  121. json::parse_string(root["script"], script);
  122. device()->lua_environment()->execute_string(script.c_str());
  123. }
  124. void ConsoleServer::process_command(TCPSocket /*client*/, const char* json)
  125. {
  126. TempAllocator4096 ta;
  127. JsonObject root(ta);
  128. json::parse(json, root);
  129. DynamicString cmd(ta);
  130. json::parse_string(root["command"], cmd);
  131. if (cmd == "compile")
  132. {
  133. DynamicString type(ta);
  134. DynamicString name(ta);
  135. DynamicString platform(ta);
  136. json::parse_string(root["resource_type"], type);
  137. json::parse_string(root["resource_name"], name);
  138. json::parse_string(root["platform"], platform);
  139. bundle_compiler_globals::compiler()->compile(type.c_str(), name.c_str(), platform.c_str());
  140. }
  141. else if (cmd == "reload")
  142. {
  143. DynamicString type(ta);
  144. DynamicString name(ta);
  145. json::parse_string(root["resource_type"], type);
  146. json::parse_string(root["resource_name"], name);
  147. device()->reload(StringId64(type.c_str()), StringId64(name.c_str()));
  148. }
  149. else if (cmd == "pause")
  150. {
  151. device()->pause();
  152. }
  153. else if (cmd == "unpause")
  154. {
  155. device()->unpause();
  156. }
  157. }
  158. namespace console_server_globals
  159. {
  160. char _buffer[sizeof(ConsoleServer)];
  161. ConsoleServer* _console = NULL;
  162. void init(uint16_t port, bool wait)
  163. {
  164. _console = new (_buffer) ConsoleServer(port, wait);
  165. }
  166. void shutdown()
  167. {
  168. _console->~ConsoleServer();
  169. _console = NULL;
  170. }
  171. void update()
  172. {
  173. #if CROWN_DEBUG
  174. _console->update();
  175. #endif // CROWN_DEBUG
  176. }
  177. ConsoleServer* console()
  178. {
  179. return _console;
  180. }
  181. } // namespace console_server
  182. } // namespace crown