Server.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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, size_t bytes_transferred));
  50. /**
  51. * Sends json data over the network.
  52. *
  53. * @param json json object
  54. */
  55. void sendJson(const Json::Value &json);
  56. /**
  57. * Parses a line of the buffer to a json value object.
  58. *
  59. * @return json object
  60. */
  61. Json::Value parseMessage();
  62. public:
  63. /**
  64. * Pointer to a con_handler.
  65. */
  66. typedef boost::shared_ptr<con_handler> pointer;
  67. /**
  68. * con_handler constructor
  69. *
  70. * Creates a con_handler
  71. *
  72. * @param io_service connection info
  73. */
  74. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  75. /**
  76. * con_handler destructor.
  77. */
  78. ~con_handler();
  79. /**
  80. * Creates a new con_handler.
  81. *
  82. * Allocates a new con_hanlder and returns the pointer.
  83. *
  84. * @param io_service connection info
  85. *
  86. * @return con_handler pointer
  87. */
  88. static pointer create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  89. /**
  90. * socket getter
  91. *
  92. * Returns the socket of the connection.
  93. *
  94. * @return the socket
  95. */
  96. tcp::socket &socket();
  97. /**
  98. * Starts a connection.
  99. *
  100. * Negotiates a connection and sets everything important to the connection up.
  101. */
  102. void start();
  103. /**
  104. * Read handler
  105. *
  106. * Will be called if the server expected a version and received something.
  107. *
  108. * @param err error
  109. * @param bytes_transferred amount of transferred bytes
  110. */
  111. void handle_read_version(const boost::system::error_code &err, size_t bytes_transferred);
  112. /**
  113. * Read handler
  114. *
  115. * Will be called after the version was verifyed. Checks login data.
  116. *
  117. * @param err error
  118. * @param bytes_transferred amount of transferred bytes
  119. */
  120. void handle_read_login(const boost::system::error_code &err, size_t bytes_transferred);
  121. /**
  122. * Read handler
  123. *
  124. * Will be called after the version and login was verifyed. Executes commands.
  125. *
  126. * @param err error
  127. * @param bytes_transferred amount of transferred bytes
  128. */
  129. void handle_read_command(const boost::system::error_code &err, size_t bytes_transferred);
  130. /**
  131. * Write handler
  132. *
  133. * Will be called if the server sends something.
  134. *
  135. * @param err error
  136. * @param bytes_transferred amount of transferred bytes
  137. */
  138. void handle_write(const boost::system::error_code &err, size_t bytes_transferred);
  139. };
  140. /**
  141. * @class Server
  142. *
  143. * boost server
  144. *
  145. * boost server which creates the handler for the connections.
  146. */
  147. class Server {
  148. private:
  149. tcp::acceptor acceptor_;
  150. /**
  151. * Accepts traffic
  152. */
  153. void start_accept();
  154. public:
  155. /**
  156. * Creates a boost server
  157. *
  158. * @param io_service connection info
  159. */
  160. Server(io_service &io_service);
  161. /**
  162. * Destroys the server
  163. */
  164. ~Server();
  165. /**
  166. * Handles traffic?
  167. *
  168. * @param connection con_handler
  169. * @param err error
  170. */
  171. void handle_accept(con_handler::pointer connection, const boost::system::error_code &err);
  172. };
  173. #endif