Server.h 3.9 KB

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