Server.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. using namespace boost::asio;
  8. using ip::tcp;
  9. /**
  10. * @class con_handler
  11. *
  12. * Handler for a connection.
  13. *
  14. * Handles and manages a connection between client and server.
  15. */
  16. class con_handler : public boost::enable_shared_from_this<con_handler> {
  17. private:
  18. tcp::socket sock;
  19. const std::string message = "Hello From Server!";
  20. const std::string protocolVersion = "0.1";
  21. const std::string fileDirectory = "./files/";
  22. /**
  23. * max buffer length
  24. */
  25. enum { max_length = 1024 };
  26. /**
  27. * data buffer
  28. */
  29. char data[max_length];
  30. /**
  31. * file stream for get command
  32. */
  33. std::ifstream getFile;
  34. /**
  35. * file stream for put command
  36. */
  37. std::ofstream putFile;
  38. /**
  39. * file stream for put command
  40. * (used to delete the file if the upload is canceled)
  41. */
  42. std::string putFileName;
  43. public:
  44. /**
  45. * Pointer to a con_handler.
  46. */
  47. typedef boost::shared_ptr<con_handler> pointer;
  48. /**
  49. * con_handler constructor
  50. *
  51. * Creates a con_handler
  52. *
  53. * @param io_service connection info
  54. */
  55. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  56. /**
  57. * con_handler destructor.
  58. */
  59. ~con_handler();
  60. /**
  61. * Creates a new con_handler.
  62. *
  63. * Allocates a new con_hanlder and returns the pointer.
  64. *
  65. * @param io_service connection info
  66. *
  67. * @return con_handler pointer
  68. */
  69. static pointer
  70. create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  71. /**
  72. * socket getter
  73. *
  74. * Returns the socket of the connection.
  75. *
  76. * @return the socket
  77. */
  78. tcp::socket &socket();
  79. /**
  80. * Starts a connection.
  81. *
  82. * Negotiates a connection and sets everything important to the connection up.
  83. */
  84. void start();
  85. /**
  86. * Read handler
  87. *
  88. * Will be called if something from the client has been received.
  89. *
  90. * @param err error
  91. * @param bytes_transferred amount of transferred bytes
  92. */
  93. void handle_read(const boost::system::error_code &err,
  94. size_t bytes_transferred);
  95. /**
  96. * Read handler
  97. *
  98. * Will be called if the server expected a version and received something.
  99. *
  100. * @param err error
  101. * @param bytes_transferred amount of transferred bytes
  102. */
  103. void handle_read_version(const boost::system::error_code &err,
  104. size_t bytes_transferred);
  105. /**
  106. * Read handler
  107. *
  108. * Will be called after the version was verifyed. Checks login data.
  109. *
  110. * @param err error
  111. * @param bytes_transferred amount of transferred bytes
  112. */
  113. void handle_read_login(const boost::system::error_code &err,
  114. size_t bytes_transferred);
  115. /**
  116. * Read handler
  117. *
  118. * Will be called after the version and login was verifyed. Executes commands.
  119. *
  120. * @param err error
  121. * @param bytes_transferred amount of transferred bytes
  122. */
  123. void handle_read_command(const boost::system::error_code &err,
  124. size_t bytes_transferred);
  125. /**
  126. * Write handler
  127. *
  128. * Will be called if the server sends something.
  129. *
  130. * @param err error
  131. * @param bytes_transferred amount of transferred bytes
  132. */
  133. void handle_write(const boost::system::error_code &err,
  134. size_t bytes_transferred);
  135. };
  136. /**
  137. * @class Server
  138. *
  139. * boost server
  140. *
  141. * boost server which creates the handler for the connections.
  142. */
  143. class Server {
  144. private:
  145. tcp::acceptor acceptor_;
  146. /**
  147. * Accepts traffic
  148. */
  149. void start_accept();
  150. public:
  151. /**
  152. * Creates a boost server
  153. *
  154. * @param io_service connection info
  155. */
  156. Server(io_service &io_service);
  157. /**
  158. * Destroys the server
  159. */
  160. ~Server();
  161. /**
  162. * Handles traffic?
  163. *
  164. * @param connection con_handler
  165. * @param err error
  166. */
  167. void handle_accept(con_handler::pointer connection,
  168. const boost::system::error_code &err);
  169. };
  170. #endif