console_server.cpp 4.8 KB

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