RPCServer.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  24. #include "OsSocket.h"
  25. #include "List.h"
  26. #include "Queue.h"
  27. #include "IdTable.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. class RPCHandler;
  32. typedef Id ClientId;
  33. #define MAX_RPC_CLIENTS 16
  34. struct RPCCallback
  35. {
  36. RPCHandler* handler;
  37. ClientId client;
  38. uint32_t message_index;
  39. };
  40. class RPCServer
  41. {
  42. public:
  43. RPCServer();
  44. void add_handler(RPCHandler* handler);
  45. void remove_handler(RPCHandler* handler);
  46. void init(bool wait);
  47. void shutdown();
  48. void update();
  49. void log_to_all(const char* message, LogSeverity::Enum severity);
  50. void send_message_to(ClientId client, const char* message);
  51. void send_message_to_all(const char* message);
  52. void execute_callbacks();
  53. private:
  54. void update_client(ClientId id);
  55. void add_client(TCPSocket& client);
  56. void remove_client(ClientId id);
  57. // Returns a handler for processing type messages or NULL.
  58. RPCHandler* find_handler(const char* type);
  59. void push_callback(RPCHandler* handler, ClientId client, uint32_t message_index);
  60. private:
  61. TCPListener m_listener;
  62. uint8_t m_num_clients;
  63. IdTable<MAX_RPC_CLIENTS> m_clients_table;
  64. TCPSocket m_clients[MAX_RPC_CLIENTS];
  65. List<char> m_receive_buffer;
  66. List<char> m_send_buffer;
  67. RPCHandler* m_handlers_head;
  68. Queue<RPCCallback> m_receive_callbacks;
  69. Queue<RPCCallback> m_send_callbacks;
  70. };
  71. } // namespace crown