Server.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. char data[max_length];
  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. public:
  74. /**
  75. * Pointer to a con_handler.
  76. */
  77. typedef boost::shared_ptr<con_handler> pointer;
  78. /**
  79. * con_handler constructor
  80. *
  81. * Creates a con_handler
  82. *
  83. * @param io_service connection info
  84. */
  85. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  86. /**
  87. * con_handler destructor.
  88. */
  89. ~con_handler();
  90. /**
  91. * Creates a new con_handler.
  92. *
  93. * Allocates a new con_hanlder and returns the pointer.
  94. *
  95. * @param io_service connection info
  96. *
  97. * @return con_handler pointer
  98. */
  99. static pointer
  100. create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  101. /**
  102. * socket getter
  103. *
  104. * Returns the socket of the connection.
  105. *
  106. * @return the socket
  107. */
  108. tcp::socket &socket();
  109. /**
  110. * Starts a connection.
  111. *
  112. * Negotiates a connection and sets everything important to the connection up.
  113. */
  114. void start();
  115. /**
  116. * Read handler
  117. *
  118. * Will be called if the server expected a version and received something.
  119. *
  120. * @param err error
  121. * @param bytes_transferred amount of transferred bytes
  122. */
  123. void handle_read_version(const boost::system::error_code &err,
  124. size_t bytes_transferred);
  125. /**
  126. * Read handler
  127. *
  128. * Will be called after the version was verifyed. Checks login data.
  129. *
  130. * @param err error
  131. * @param bytes_transferred amount of transferred bytes
  132. */
  133. void handle_read_login(const boost::system::error_code &err,
  134. size_t bytes_transferred);
  135. /**
  136. * Read handler
  137. *
  138. * Will be called after the version and login was verifyed. Executes commands.
  139. *
  140. * @param err error
  141. * @param bytes_transferred amount of transferred bytes
  142. */
  143. void handle_read_command(const boost::system::error_code &err,
  144. size_t bytes_transferred);
  145. /**
  146. * Write handler
  147. *
  148. * Will be called if the server sends something.
  149. *
  150. * @param err error
  151. * @param bytes_transferred amount of transferred bytes
  152. */
  153. void handle_write(const boost::system::error_code &err,
  154. size_t bytes_transferred);
  155. };
  156. /**
  157. * @class Server
  158. *
  159. * boost server
  160. *
  161. * boost server which creates the handler for the connections.
  162. */
  163. class Server {
  164. private:
  165. tcp::acceptor acceptor_;
  166. /**
  167. * Accepts traffic
  168. */
  169. void start_accept();
  170. public:
  171. /**
  172. * Creates a boost server
  173. *
  174. * @param io_service connection info
  175. */
  176. Server(io_service &io_service);
  177. /**
  178. * Destroys the server
  179. */
  180. ~Server();
  181. /**
  182. * Handles traffic?
  183. *
  184. * @param connection con_handler
  185. * @param err error
  186. */
  187. void handle_accept(con_handler::pointer connection,
  188. const boost::system::error_code &err);
  189. };
  190. #endif