console_server.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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::listen(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. TempAllocator4096 ta;
  52. StringStream ss(ta);
  53. ss << "{\"type\":\"error\",\"message\":\"" << msg << "\"}";
  54. send(client, string_stream::c_str(ss));
  55. }
  56. void ConsoleServer::success(TCPSocket client, const char* msg)
  57. {
  58. TempAllocator4096 ta;
  59. StringStream ss(ta);
  60. ss << "{\"type\":\"success\",\"message\":\"" << msg << "\"}";
  61. send(client, string_stream::c_str(ss));
  62. }
  63. void ConsoleServer::send(const char* json)
  64. {
  65. for (u32 i = 0; i < vector::size(_clients); ++i)
  66. send(_clients[i], json);
  67. }
  68. void ConsoleServer::update()
  69. {
  70. TCPSocket client;
  71. AcceptResult result = _server.accept_nonblock(client);
  72. if (result.error == AcceptResult::NO_ERROR)
  73. add_client(client);
  74. TempAllocator256 alloc;
  75. Array<u32> to_remove(alloc);
  76. // Update all clients
  77. for (u32 i = 0; i < vector::size(_clients); ++i)
  78. {
  79. ReadResult rr = update_client(_clients[i]);
  80. if (rr.error != ReadResult::NO_ERROR)
  81. array::push_back(to_remove, i);
  82. }
  83. // Remove clients
  84. for (u32 i = 0; i < array::size(to_remove); ++i)
  85. {
  86. const u32 last = vector::size(_clients) - 1;
  87. const u32 c = to_remove[i];
  88. _clients[c].close();
  89. _clients[c] = _clients[last];
  90. vector::pop_back(_clients);
  91. }
  92. }
  93. void ConsoleServer::add_client(TCPSocket socket)
  94. {
  95. vector::push_back(_clients, socket);
  96. }
  97. ReadResult ConsoleServer::update_client(TCPSocket client)
  98. {
  99. u32 msg_len = 0;
  100. ReadResult rr = client.read_nonblock(&msg_len, 4);
  101. // If no data received, return
  102. if (rr.error == ReadResult::NO_ERROR && rr.bytes_read == 0) return rr;
  103. if (rr.error == ReadResult::REMOTE_CLOSED) return rr;
  104. if (rr.error != ReadResult::NO_ERROR) return rr;
  105. // Else read the message
  106. TempAllocator4096 ta;
  107. Array<char> msg_buf(ta);
  108. array::resize(msg_buf, msg_len);
  109. ReadResult msg_result = client.read(array::begin(msg_buf), msg_len);
  110. array::push_back(msg_buf, '\0');
  111. if (msg_result.error == ReadResult::REMOTE_CLOSED) return msg_result;
  112. if (msg_result.error != ReadResult::NO_ERROR) return msg_result;
  113. process(client, array::begin(msg_buf));
  114. return msg_result;
  115. }
  116. void ConsoleServer::process(TCPSocket client, const char* json)
  117. {
  118. TempAllocator4096 ta;
  119. JsonObject root(ta);
  120. sjson::parse(json, root);
  121. DynamicString type(ta);
  122. sjson::parse_string(root["type"], type);
  123. if (type == "script")
  124. {
  125. DynamicString script(ta);
  126. sjson::parse_string(root["script"], script);
  127. device()->lua_environment()->execute_string(script.c_str());
  128. success(client, "Script executed.");
  129. }
  130. else if (type == "compile")
  131. {
  132. DynamicString rtype(ta);
  133. DynamicString rname(ta);
  134. DynamicString platform(ta);
  135. sjson::parse_string(root["resource_type"], rtype);
  136. sjson::parse_string(root["resource_name"], rname);
  137. sjson::parse_string(root["platform"], platform);
  138. bool succ = device()->bundle_compiler()->compile(rtype.c_str()
  139. , rname.c_str()
  140. , platform.c_str()
  141. );
  142. if (succ)
  143. success(client, "Resource compiled.");
  144. else
  145. error(client, "Failed to compile resource.");
  146. }
  147. else if (type == "reload")
  148. {
  149. StringId64 rtype = sjson::parse_resource_id(root["resource_type"]);
  150. StringId64 rname = sjson::parse_resource_id(root["resource_name"]);
  151. device()->reload(rtype, rname);
  152. }
  153. else if (type == "pause")
  154. {
  155. device()->pause();
  156. }
  157. else if (type == "unpause")
  158. {
  159. device()->unpause();
  160. }
  161. else
  162. {
  163. error(client, "Unknown command");
  164. }
  165. }
  166. } // namespace crown