console_server.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 result;
  25. TCPSocket client;
  26. do
  27. {
  28. result = _server.accept(client);
  29. }
  30. while (result.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 = strlen(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].socket, 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].socket);
  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. Client cl;
  79. cl.socket = socket;
  80. vector::push_back(_clients, cl);
  81. }
  82. ReadResult ConsoleServer::update_client(TCPSocket client)
  83. {
  84. uint32_t msg_len = 0;
  85. ReadResult rr = client.read_nonblock(&msg_len, 4);
  86. // If no data received, return
  87. if (rr.error == ReadResult::NO_ERROR && rr.bytes_read == 0) return rr;
  88. if (rr.error == ReadResult::REMOTE_CLOSED) return rr;
  89. if (rr.error != ReadResult::NO_ERROR) return rr;
  90. // Else read the message
  91. TempAllocator4096 ta;
  92. Array<char> msg_buf(ta);
  93. array::resize(msg_buf, msg_len);
  94. ReadResult msg_result = client.read(array::begin(msg_buf), msg_len);
  95. array::push_back(msg_buf, '\0');
  96. if (msg_result.error == ReadResult::REMOTE_CLOSED) return msg_result;
  97. if (msg_result.error != ReadResult::NO_ERROR) return msg_result;
  98. process(client, array::begin(msg_buf));
  99. return msg_result;
  100. }
  101. void ConsoleServer::process(TCPSocket client, const char* json)
  102. {
  103. TempAllocator4096 ta;
  104. Map<DynamicString, const char*> root(ta);
  105. json::parse_object(json, root);
  106. DynamicString type(ta);
  107. json::parse_string(root["type"], type);
  108. if (type == "ping") process_ping(client, json);
  109. else if (type == "script") process_script(client, json);
  110. else if (type == "command") process_command(client, json);
  111. else CE_FATAL("Request unknown.");
  112. }
  113. void ConsoleServer::process_ping(TCPSocket client, const char* /*json*/)
  114. {
  115. send(client, "{\"type\":\"pong\"}");
  116. }
  117. void ConsoleServer::process_script(TCPSocket /*client*/, const char* json)
  118. {
  119. TempAllocator4096 ta;
  120. Map<DynamicString, const char*> root(ta);
  121. json::parse_object(json, root);
  122. DynamicString script(ta);
  123. json::parse_string(root["script"], script);
  124. device()->lua_environment()->execute_string(script.c_str());
  125. }
  126. void ConsoleServer::process_command(TCPSocket /*client*/, const char* json)
  127. {
  128. TempAllocator4096 ta;
  129. Map<DynamicString, const char*> root(ta);
  130. json::parse_object(json, root);
  131. DynamicString cmd(ta);
  132. json::parse_string(root["command"], cmd);
  133. if (cmd == "compile")
  134. {
  135. DynamicString type(ta);
  136. DynamicString name(ta);
  137. DynamicString platform(ta);
  138. json::parse_string(root["resource_type"], type);
  139. json::parse_string(root["resource_name"], name);
  140. json::parse_string(root["platform"], platform);
  141. bundle_compiler_globals::compiler()->compile(type.c_str(), name.c_str(), platform.c_str());
  142. }
  143. else if (cmd == "reload")
  144. {
  145. DynamicString type(ta);
  146. DynamicString name(ta);
  147. json::parse_string(root["resource_type"], type);
  148. json::parse_string(root["resource_name"], name);
  149. device()->reload(StringId64(type.c_str()), StringId64(name.c_str()));
  150. }
  151. else if (cmd == "pause")
  152. {
  153. device()->pause();
  154. }
  155. else if (cmd == "unpause")
  156. {
  157. device()->unpause();
  158. }
  159. }
  160. namespace console_server_globals
  161. {
  162. char _buffer[sizeof(ConsoleServer)];
  163. ConsoleServer* _console = NULL;
  164. void init(uint16_t port, bool wait)
  165. {
  166. _console = new (_buffer) ConsoleServer(port, wait);
  167. }
  168. void shutdown()
  169. {
  170. _console->~ConsoleServer();
  171. _console = NULL;
  172. }
  173. void update()
  174. {
  175. #if CROWN_DEBUG
  176. _console->update();
  177. #endif // CROWN_DEBUG
  178. }
  179. ConsoleServer& console()
  180. {
  181. return *_console;
  182. }
  183. } // namespace console_server
  184. } // namespace crown