|
@@ -12,7 +12,7 @@ using ip::tcp;
|
|
|
|
|
|
con_handler::con_handler(
|
|
|
basic_socket_acceptor<ip::tcp>::executor_type &io_service)
|
|
|
- : sock(io_service) {
|
|
|
+ : sock(io_service), buf(max_length) {
|
|
|
// disable indentation for json
|
|
|
this->jsonStringBuilder.settings_["indentation"] = "";
|
|
|
|
|
@@ -36,15 +36,7 @@ void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
|
size_t bytes_transferred) {
|
|
|
if (!err) {
|
|
|
// set up json stuff
|
|
|
- JSONCPP_STRING err;
|
|
|
- Json::Value root;
|
|
|
-
|
|
|
- // parse data
|
|
|
- if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
- &root, &err)) {
|
|
|
- std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
+ Json::Value root = parseMessage();
|
|
|
|
|
|
// create answer
|
|
|
Json::Value answer;
|
|
@@ -79,15 +71,7 @@ void con_handler::handle_read_login(const boost::system::error_code &err,
|
|
|
size_t bytes_transferred) {
|
|
|
if (!err) {
|
|
|
// set up json stuff
|
|
|
- JSONCPP_STRING err;
|
|
|
- Json::Value root;
|
|
|
-
|
|
|
- // parse data
|
|
|
- if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
- &root, &err)) {
|
|
|
- std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
+ Json::Value root = parseMessage();
|
|
|
|
|
|
Json::Value answer;
|
|
|
|
|
@@ -121,15 +105,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
size_t bytes_transferred) {
|
|
|
if (!err) {
|
|
|
// set up json stuff
|
|
|
- JSONCPP_STRING err;
|
|
|
- Json::Value root;
|
|
|
-
|
|
|
- // parse data
|
|
|
- if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
- &root, &err)) {
|
|
|
- std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
+ Json::Value root = parseMessage();
|
|
|
|
|
|
Json::Value answer;
|
|
|
|
|
@@ -366,10 +342,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),
|
|
|
+ /*sock.async_read_some(buffer(data, max_length),
|
|
|
boost::bind(handler, shared_from_this(),
|
|
|
placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ placeholders::bytes_transferred));*/
|
|
|
+
|
|
|
+ async_read_until(sock, buf, '\n',
|
|
|
+ bind(handler, shared_from_this(), placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
}
|
|
|
|
|
|
void con_handler::sendJson(const Json::Value &json) {
|
|
@@ -382,6 +362,26 @@ void con_handler::sendJson(const Json::Value &json) {
|
|
|
placeholders::bytes_transferred));
|
|
|
}
|
|
|
|
|
|
+Json::Value con_handler::parseMessage() {
|
|
|
+ const char *data = buffer_cast<const char *>(buf.data());
|
|
|
+
|
|
|
+ std::string dataStr(data, buf.size());
|
|
|
+ const int lineEnd = dataStr.find('\n');
|
|
|
+
|
|
|
+ JSONCPP_STRING err;
|
|
|
+ Json::Value root;
|
|
|
+
|
|
|
+ // parse data
|
|
|
+ if (!this->jsonReader->parse(data, data + lineEnd, &root, &err)) {
|
|
|
+ std::cerr << "Json error: " << err << std::endl << "data: " << data;
|
|
|
+ sock.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ buf.consume(lineEnd + 1);
|
|
|
+
|
|
|
+ return root;
|
|
|
+}
|
|
|
+
|
|
|
/**********
|
|
|
* Server *
|
|
|
**********/
|