Server.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include <boost/asio.hpp>
  4. #include <boost/bind.hpp>
  5. #include <boost/enable_shared_from_this.hpp>
  6. #include <json/json.h>
  7. #include "JsonCommander.h"
  8. using namespace boost::asio;
  9. using ip::tcp;
  10. /**
  11. * @class con_handler
  12. *
  13. * Handler for a connection.
  14. *
  15. * Handles and manages a connection between client and server.
  16. */
  17. class con_handler : public boost::enable_shared_from_this<con_handler> {
  18. private:
  19. tcp::socket sock;
  20. /**
  21. * max buffer length
  22. */
  23. enum { max_length = 1024 };
  24. /**
  25. * data buffer
  26. */
  27. streambuf buf;
  28. /**
  29. * string builder for json
  30. */
  31. Json::StreamWriterBuilder jsonStringBuilder;
  32. /**
  33. * json reader to parse json strings
  34. */
  35. std::unique_ptr<Json::CharReader> jsonReader;
  36. /**
  37. * Executes json commands
  38. */
  39. JsonCommander jsonCommander;
  40. /**
  41. * File manager used by jsonCommander
  42. */
  43. FileManager fileManager;
  44. /**
  45. * Reads data and binds it to a handler.
  46. *
  47. * @param handler method pointer to a handler_read_* method
  48. */
  49. void read(void (con_handler::*handler)(const boost::system::error_code &err,
  50. size_t bytes_transferred));
  51. /**
  52. * Sends json data over the network.
  53. *
  54. * @param json json object
  55. */
  56. void sendJson(const Json::Value &json);
  57. /**
  58. * Parses a line of the buffer to a json value object.
  59. *
  60. * @return json object
  61. */
  62. Json::Value parseMessage();
  63. public:
  64. /**
  65. * Pointer to a con_handler.
  66. */
  67. typedef boost::shared_ptr<con_handler> pointer;
  68. /**
  69. * con_handler constructor
  70. *
  71. * Creates a con_handler
  72. *
  73. * @param io_service connection info
  74. */
  75. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  76. /**
  77. * con_handler destructor.
  78. */
  79. ~con_handler();
  80. /**
  81. * Creates a new con_handler.
  82. *
  83. * Allocates a new con_hanlder and returns the pointer.
  84. *
  85. * @param io_service connection info
  86. *
  87. * @return con_handler pointer
  88. */
  89. static pointer
  90. create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  91. /**
  92. * socket getter
  93. *
  94. * Returns the socket of the connection.
  95. *
  96. * @return the socket
  97. */
  98. tcp::socket &socket();
  99. /**
  100. * Starts a connection.
  101. *
  102. * Negotiates a connection and sets everything important to the connection up.
  103. */
  104. void start();
  105. /**
  106. * Read handler
  107. *
  108. * Will be called if the server expected a version and received something.
  109. *
  110. * @param err error
  111. * @param bytes_transferred amount of transferred bytes
  112. */
  113. void handle_read_version(const boost::system::error_code &err,
  114. size_t bytes_transferred);
  115. /**
  116. * Read handler
  117. *
  118. * Will be called after the version was verifyed. Checks login data.
  119. *
  120. * @param err error
  121. * @param bytes_transferred amount of transferred bytes
  122. */
  123. void handle_read_login(const boost::system::error_code &err,
  124. size_t bytes_transferred);
  125. /**
  126. * Read handler
  127. *
  128. * Will be called after the version and login was verifyed. Executes commands.
  129. *
  130. * @param err error
  131. * @param bytes_transferred amount of transferred bytes
  132. */
  133. void handle_read_command(const boost::system::error_code &err,
  134. size_t bytes_transferred);
  135. /**
  136. * Write handler
  137. *
  138. * Will be called if the server sends something.
  139. *
  140. * @param err error
  141. * @param bytes_transferred amount of transferred bytes
  142. */
  143. void handle_write(const boost::system::error_code &err,
  144. size_t bytes_transferred);
  145. };
  146. /**
  147. * @class Server
  148. *
  149. * boost server
  150. *
  151. * boost server which creates the handler for the connections.
  152. */
  153. class Server {
  154. private:
  155. tcp::acceptor acceptor_;
  156. /**
  157. * Accepts traffic
  158. */
  159. void start_accept();
  160. public:
  161. /**
  162. * Creates a boost server
  163. *
  164. * @param io_service connection info
  165. */
  166. Server(io_service &io_service);
  167. /**
  168. * Destroys the server
  169. */
  170. ~Server();
  171. /**
  172. * Handles traffic?
  173. *
  174. * @param connection con_handler
  175. * @param err error
  176. */
  177. void handle_accept(con_handler::pointer connection,
  178. const boost::system::error_code &err);
  179. };
  180. #endif