RPCServer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "RPCServer.h"
  24. #include "Log.h"
  25. #include "JSONParser.h"
  26. #include "RPCHandler.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. RPCServer::RPCServer()
  31. : m_num_clients(0)
  32. , m_receive_allocator(default_allocator(), 16 * 1024)
  33. , m_send_allocator(default_allocator(), 16 * 1024)
  34. , m_receive_buffer(m_receive_allocator)
  35. , m_send_buffer(m_send_allocator)
  36. , m_handlers_head(NULL)
  37. , m_receive_callbacks(default_allocator())
  38. , m_send_callbacks(default_allocator())
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. void RPCServer::add_handler(RPCHandler* handler)
  43. {
  44. CE_ASSERT_NOT_NULL(handler);
  45. if (m_handlers_head != NULL)
  46. {
  47. handler->m_next = m_handlers_head;
  48. }
  49. m_handlers_head = handler;
  50. }
  51. //-----------------------------------------------------------------------------
  52. void RPCServer::remove_handler(RPCHandler* handler)
  53. {
  54. CE_ASSERT_NOT_NULL(handler);
  55. RPCHandler* cur = m_handlers_head;
  56. RPCHandler* prev = NULL;
  57. for (; cur != NULL && cur != handler; prev = cur, cur = cur->m_next) ;
  58. if (cur == m_handlers_head)
  59. {
  60. m_handlers_head = cur->m_next;
  61. }
  62. else
  63. {
  64. prev->m_next = cur->m_next;
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. void RPCServer::init(bool wait)
  69. {
  70. m_listener.open(10001);
  71. if (wait)
  72. {
  73. TCPSocket client;
  74. while (!m_listener.listen(client)) ;
  75. add_client(client);
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. void RPCServer::shutdown()
  80. {
  81. for (uint32_t i = 0; i < MAX_RPC_CLIENTS; i++)
  82. {
  83. m_clients[i].close();
  84. }
  85. m_listener.close();
  86. }
  87. //-----------------------------------------------------------------------------
  88. void RPCServer::update()
  89. {
  90. // Check for new clients
  91. TCPSocket client;
  92. if (m_listener.listen(client))
  93. {
  94. add_client(client);
  95. }
  96. // Update all clients
  97. for (uint32_t i = 0; i < MAX_RPC_CLIENTS; i++)
  98. {
  99. ClientId id = *(m_clients_table.begin() + i);
  100. if (id.id != INVALID_ID)
  101. {
  102. update_client(id);
  103. }
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. void RPCServer::send_message_to(ClientId client, const char* message)
  108. {
  109. RPCCallback cb;
  110. cb.handler = NULL;
  111. cb.client = client;
  112. cb.message_index = m_send_buffer.size();
  113. m_send_buffer.push(message, string::strlen(message));
  114. m_send_buffer.push_back('\0');
  115. m_send_callbacks.push_back(cb);
  116. }
  117. //-----------------------------------------------------------------------------
  118. void RPCServer::send_message_to_all(const char* message)
  119. {
  120. // Update all clients
  121. for (uint32_t i = 0; i < MAX_RPC_CLIENTS; i++)
  122. {
  123. ClientId id = *(m_clients_table.begin() + i);
  124. if (id.id != INVALID_ID)
  125. {
  126. send_message_to(id, message);
  127. }
  128. }
  129. }
  130. //-----------------------------------------------------------------------------
  131. void RPCServer::execute_callbacks()
  132. {
  133. for (uint32_t i = 0; i < m_receive_callbacks.size(); i++)
  134. {
  135. RPCCallback cb = m_receive_callbacks.front();
  136. m_receive_callbacks.pop_front();
  137. cb.handler->execute_command(this, cb.client, &m_receive_buffer[cb.message_index]);
  138. }
  139. m_receive_callbacks.clear();
  140. m_receive_buffer.clear();
  141. m_receive_allocator.clear();
  142. for (uint32_t i = 0; i < m_send_callbacks.size(); i++)
  143. {
  144. RPCCallback cb = m_send_callbacks.front();
  145. m_send_callbacks.pop_front();
  146. m_clients[cb.client.index].write(&m_send_buffer[cb.message_index], string::strlen(&m_send_buffer[cb.message_index]));
  147. }
  148. m_send_callbacks.clear();
  149. m_send_buffer.clear();
  150. m_send_allocator.clear();
  151. }
  152. //-----------------------------------------------------------------------------
  153. void RPCServer::update_client(ClientId id)
  154. {
  155. size_t total_read = 0;
  156. uint32_t message_index = m_receive_buffer.size();
  157. TCPSocket& client = m_clients[id.index];
  158. char buf[1024];
  159. while (true)
  160. {
  161. ReadResult result = client.read(buf, 32);
  162. if (result.error == ReadResult::NO_ERROR)
  163. {
  164. if (result.received_bytes > 0)
  165. {
  166. m_receive_buffer.push(buf, result.received_bytes);
  167. total_read += result.received_bytes;
  168. continue;
  169. }
  170. break;
  171. }
  172. else
  173. {
  174. // Close remote connection
  175. client.close();
  176. remove_client(id);
  177. return;
  178. }
  179. }
  180. // Ensure NUL-terminated
  181. m_receive_buffer.push_back('\0');
  182. // Process only if received something
  183. // TODO: Bad JSON strings crash
  184. if (total_read > 0)
  185. {
  186. JSONParser parser(&m_receive_buffer[message_index]);
  187. JSONElement root = parser.root();
  188. JSONElement type = root.key_or_nil("type");
  189. if (!type.is_nil())
  190. {
  191. RPCHandler* handler = find_handler(type.string_value());
  192. if (handler != NULL)
  193. {
  194. push_callback(handler, id, message_index);
  195. }
  196. }
  197. else
  198. {
  199. Log::d("Wrong message, type is nil");
  200. }
  201. }
  202. }
  203. //-----------------------------------------------------------------------------
  204. void RPCServer::add_client(TCPSocket& client)
  205. {
  206. if (m_num_clients < MAX_RPC_CLIENTS)
  207. {
  208. ClientId id = m_clients_table.create();
  209. m_clients[id.index] = client;
  210. m_num_clients++;
  211. }
  212. else
  213. {
  214. Log::e("Too many clients, connection denied");
  215. }
  216. }
  217. //-----------------------------------------------------------------------------
  218. void RPCServer::remove_client(ClientId id)
  219. {
  220. CE_ASSERT(m_num_clients > 0, "No client connected");
  221. m_clients[id.index].close();
  222. m_clients_table.destroy(id);
  223. m_num_clients--;
  224. }
  225. //-----------------------------------------------------------------------------
  226. RPCHandler* RPCServer::find_handler(const char* type)
  227. {
  228. const RPCHandler* handler = m_handlers_head;
  229. while (handler != NULL)
  230. {
  231. if (handler->type() == hash::murmur2_32(type, string::strlen(type), 0))
  232. {
  233. return (RPCHandler*)handler;
  234. }
  235. handler = handler->m_next;
  236. }
  237. return NULL;
  238. }
  239. //-----------------------------------------------------------------------------
  240. void RPCServer::push_callback(RPCHandler* handler, ClientId client, uint32_t message_index)
  241. {
  242. RPCCallback cb;
  243. cb.handler = handler;
  244. cb.client = client;
  245. cb.message_index = message_index;
  246. m_receive_callbacks.push_back(cb);
  247. }
  248. } // namespace crown