#ifndef SERVER_H #define SERVER_H #include #include #include #include #include using namespace boost::asio; using ip::tcp; /** * @class con_handler * * Handler for a connection. * * Handles and manages a connection between client and server. */ class con_handler : public boost::enable_shared_from_this { private: tcp::socket sock; const std::string message = "Hello From Server!"; const std::string protocolVersion = "0.1"; const std::string fileDirectory = "./files/"; /** * max buffer length */ enum { max_length = 1024, max_data_length = 512 }; /** * data buffer */ char data[max_length]; /** * file stream for get command */ std::ifstream getFile; /** * file stream for put command */ std::ofstream putFile; /** * file stream for put command * (used to delete the file if the upload is canceled) */ std::string putFileName; /** * Last chunk number which was sent. */ int getFileRemaining; /** * Last chunk number which was received. */ int putFileReceived; /** * string builder for json */ Json::StreamWriterBuilder jsonStringBuilder; /** * json reader to parse json strings */ std::unique_ptr jsonReader; /** * Reads data and binds it to a handler. * * @param handler method pointer to a handler_read_* method */ void read(void (con_handler::*handler)(const boost::system::error_code &err, size_t bytes_transferred)); /** * Sends json data over the network. * * @param json json object */ void sendJson(const Json::Value &json); public: /** * Pointer to a con_handler. */ typedef boost::shared_ptr pointer; /** * con_handler constructor * * Creates a con_handler * * @param io_service connection info */ con_handler(basic_socket_acceptor::executor_type &io_service); /** * con_handler destructor. */ ~con_handler(); /** * Creates a new con_handler. * * Allocates a new con_hanlder and returns the pointer. * * @param io_service connection info * * @return con_handler pointer */ static pointer create(basic_socket_acceptor::executor_type &io_service); /** * socket getter * * Returns the socket of the connection. * * @return the socket */ tcp::socket &socket(); /** * Starts a connection. * * Negotiates a connection and sets everything important to the connection up. */ void start(); /** * Read handler * * Will be called if the server expected a version and received something. * * @param err error * @param bytes_transferred amount of transferred bytes */ void handle_read_version(const boost::system::error_code &err, size_t bytes_transferred); /** * Read handler * * Will be called after the version was verifyed. Checks login data. * * @param err error * @param bytes_transferred amount of transferred bytes */ void handle_read_login(const boost::system::error_code &err, size_t bytes_transferred); /** * Read handler * * Will be called after the version and login was verifyed. Executes commands. * * @param err error * @param bytes_transferred amount of transferred bytes */ void handle_read_command(const boost::system::error_code &err, size_t bytes_transferred); /** * Write handler * * Will be called if the server sends something. * * @param err error * @param bytes_transferred amount of transferred bytes */ void handle_write(const boost::system::error_code &err, size_t bytes_transferred); }; /** * @class Server * * boost server * * boost server which creates the handler for the connections. */ class Server { private: tcp::acceptor acceptor_; /** * Accepts traffic */ void start_accept(); public: /** * Creates a boost server * * @param io_service connection info */ Server(io_service &io_service); /** * Destroys the server */ ~Server(); /** * Handles traffic? * * @param connection con_handler * @param err error */ void handle_accept(con_handler::pointer connection, const boost::system::error_code &err); }; #endif