Server.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include <fstream>
  4. #include <boost/asio.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/enable_shared_from_this.hpp>
  7. #include <json/json.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. const std::string message = "Hello From Server!";
  21. const std::string protocolVersion = "0.1";
  22. const std::string fileDirectory = "./files/";
  23. /**
  24. * max buffer length
  25. */
  26. enum { max_length = 1024, max_data_length = 512 };
  27. /**
  28. * data buffer
  29. */
  30. streambuf buf;
  31. /**
  32. * file stream for get command
  33. */
  34. std::ifstream getFile;
  35. /**
  36. * file stream for put command
  37. */
  38. std::ofstream putFile;
  39. /**
  40. * file stream for put command
  41. * (used to delete the file if the upload is canceled)
  42. */
  43. std::string putFileName;
  44. /**
  45. * Last chunk number which was sent.
  46. */
  47. int getFileRemaining;
  48. /**
  49. * Last chunk number which was received.
  50. */
  51. int putFileReceived;
  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. * Reads data and binds it to a handler.
  62. *
  63. * @param handler method pointer to a handler_read_* method
  64. */
  65. void read(void (con_handler::*handler)(const boost::system::error_code &err,
  66. size_t bytes_transferred));
  67. /**
  68. * Sends json data over the network.
  69. *
  70. * @param json json object
  71. */
  72. void sendJson(const Json::Value &json);
  73. /**
  74. * Parses a line of the buffer to a json value object.
  75. *
  76. * @return json object
  77. */
  78. Json::Value parseMessage();
  79. public:
  80. /**
  81. * Pointer to a con_handler.
  82. */
  83. typedef boost::shared_ptr<con_handler> pointer;
  84. /**
  85. * con_handler constructor
  86. *
  87. * Creates a con_handler
  88. *
  89. * @param io_service connection info
  90. */
  91. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  92. /**
  93. * con_handler destructor.
  94. */
  95. ~con_handler();
  96. /**
  97. * Creates a new con_handler.
  98. *
  99. * Allocates a new con_hanlder and returns the pointer.
  100. *
  101. * @param io_service connection info
  102. *
  103. * @return con_handler pointer
  104. */
  105. static pointer
  106. create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  107. /**
  108. * socket getter
  109. *
  110. * Returns the socket of the connection.
  111. *
  112. * @return the socket
  113. */
  114. tcp::socket &socket();
  115. /**
  116. * Starts a connection.
  117. *
  118. * Negotiates a connection and sets everything important to the connection up.
  119. */
  120. void start();
  121. /**
  122. * Read handler
  123. *
  124. * Will be called if the server expected a version and received something.
  125. *
  126. * @param err error
  127. * @param bytes_transferred amount of transferred bytes
  128. */
  129. void handle_read_version(const boost::system::error_code &err,
  130. size_t bytes_transferred);
  131. /**
  132. * Read handler
  133. *
  134. * Will be called after the version was verifyed. Checks login data.
  135. *
  136. * @param err error
  137. * @param bytes_transferred amount of transferred bytes
  138. */
  139. void handle_read_login(const boost::system::error_code &err,
  140. size_t bytes_transferred);
  141. /**
  142. * Read handler
  143. *
  144. * Will be called after the version and login was verifyed. Executes commands.
  145. *
  146. * @param err error
  147. * @param bytes_transferred amount of transferred bytes
  148. */
  149. void handle_read_command(const boost::system::error_code &err,
  150. size_t bytes_transferred);
  151. /**
  152. * Write handler
  153. *
  154. * Will be called if the server sends something.
  155. *
  156. * @param err error
  157. * @param bytes_transferred amount of transferred bytes
  158. */
  159. void handle_write(const boost::system::error_code &err,
  160. size_t bytes_transferred);
  161. };
  162. /**
  163. * @class Server
  164. *
  165. * boost server
  166. *
  167. * boost server which creates the handler for the connections.
  168. */
  169. class Server {
  170. private:
  171. tcp::acceptor acceptor_;
  172. /**
  173. * Accepts traffic
  174. */
  175. void start_accept();
  176. public:
  177. /**
  178. * Creates a boost server
  179. *
  180. * @param io_service connection info
  181. */
  182. Server(io_service &io_service);
  183. /**
  184. * Destroys the server
  185. */
  186. ~Server();
  187. /**
  188. * Handles traffic?
  189. *
  190. * @param connection con_handler
  191. * @param err error
  192. */
  193. void handle_accept(con_handler::pointer connection,
  194. const boost::system::error_code &err);
  195. };
  196. #endif