|
@@ -24,23 +24,7 @@ con_handler::create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
|
|
|
|
|
|
tcp::socket &con_handler::socket() { return sock; }
|
|
|
|
|
|
-void con_handler::start() {
|
|
|
-
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_version,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
-}
|
|
|
-
|
|
|
-void con_handler::handle_read(const boost::system::error_code &err,
|
|
|
- size_t bytes_transferred) {
|
|
|
- if (!err) {
|
|
|
- std::cout << this->data << std::endl;
|
|
|
- } else {
|
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
-}
|
|
|
+void con_handler::start() { read(&con_handler::handle_read_version); }
|
|
|
|
|
|
void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
|
size_t bytes_transferred) {
|
|
@@ -69,11 +53,7 @@ void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
|
answer["accept"] = true;
|
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
- // read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_login,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_login);
|
|
|
|
|
|
// send answer
|
|
|
sock.async_write_some(buffer(answerString, max_length),
|
|
@@ -126,10 +106,7 @@ void con_handler::handle_read_login(const boost::system::error_code &err,
|
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
// send answer
|
|
|
sock.async_write_some(buffer(answerString, max_length),
|
|
@@ -177,10 +154,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
// check command
|
|
|
if (root["command"].compare("status") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "status";
|
|
|
|
|
@@ -206,10 +180,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
placeholders::bytes_transferred));
|
|
|
} else if (root["command"].compare("list") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "list";
|
|
|
|
|
@@ -230,10 +201,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
|
|
|
} else if (root["command"].compare("put") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "put";
|
|
|
|
|
@@ -311,10 +279,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
}
|
|
|
} else if (root["command"].compare("get") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "get";
|
|
|
|
|
@@ -448,6 +413,14 @@ void con_handler::handle_write(const boost::system::error_code &err,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void con_handler::read(void (con_handler::*handler)(
|
|
|
+ const boost::system::error_code &err, size_t bytes_transferred)) {
|
|
|
+ sock.async_read_some(buffer(data, max_length),
|
|
|
+ boost::bind(handler, shared_from_this(),
|
|
|
+ placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+}
|
|
|
+
|
|
|
/**********
|
|
|
* Server *
|
|
|
**********/
|