Server.h 4.3 KB

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