Server.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /**
  20. * max buffer length
  21. */
  22. enum { max_length = 1024 };
  23. /**
  24. * data buffer
  25. */
  26. char data[max_length];
  27. public:
  28. /**
  29. * Pointer to a con_handler.
  30. */
  31. typedef boost::shared_ptr<con_handler> pointer;
  32. /**
  33. * con_handler constructor
  34. *
  35. * Creates a con_handler
  36. *
  37. * @param io_service connection info
  38. */
  39. con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  40. /**
  41. * con_handler destructor.
  42. */
  43. ~con_handler();
  44. /**
  45. * Creates a new con_handler.
  46. *
  47. * Allocates a new con_hanlder and returns the pointer.
  48. *
  49. * @param io_service connection info
  50. *
  51. * @return con_handler pointer
  52. */
  53. static pointer
  54. create(basic_socket_acceptor<ip::tcp>::executor_type &io_service);
  55. /**
  56. * socket getter
  57. *
  58. * Returns the socket of the connection.
  59. *
  60. * @return the socket
  61. */
  62. tcp::socket &socket();
  63. /**
  64. * Starts a connection.
  65. *
  66. * Negotiates a connection and sets everything important to the connection up.
  67. */
  68. void start();
  69. /**
  70. * Read handler
  71. *
  72. * Will be called if something from the client has been received.
  73. *
  74. * @param err error
  75. * @param bytes_transferred amount of transferred bytes
  76. */
  77. void handle_read(const boost::system::error_code &err,
  78. size_t bytes_transferred);
  79. /**
  80. * Write handler
  81. *
  82. * Will be called if the server sends something.
  83. *
  84. * @param err error
  85. * @param bytes_transferred amount of transferred bytes
  86. */
  87. void handle_write(const boost::system::error_code &err,
  88. size_t bytes_transferred);
  89. };
  90. /**
  91. * @class Server
  92. *
  93. * boost server
  94. *
  95. * boost server which creates the handler for the connections.
  96. */
  97. class Server {
  98. private:
  99. tcp::acceptor acceptor_;
  100. /**
  101. * Accepts traffic
  102. */
  103. void start_accept();
  104. public:
  105. /**
  106. * Creates a boost server
  107. *
  108. * @param io_service connection info
  109. */
  110. Server(io_service &io_service);
  111. /**
  112. * Destroys the server
  113. */
  114. ~Server();
  115. /**
  116. * Handles traffic?
  117. *
  118. * @param connection con_handler
  119. * @param err error
  120. */
  121. void handle_accept(con_handler::pointer connection,
  122. const boost::system::error_code &err);
  123. };
  124. #endif