Server.h 3.6 KB

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