瀏覽代碼

async_read_until + json parsing method

daemon reads until the next newline comes and json parsing is done in a
method so you have less redundant code
anon 5 年之前
父節點
當前提交
cbd38f871e
共有 2 個文件被更改,包括 38 次插入31 次删除
  1. 8 1
      daemon/include/Server.h
  2. 30 30
      daemon/src/Server.cpp

+ 8 - 1
daemon/include/Server.h

@@ -36,7 +36,7 @@ private:
   /**
    * data buffer
    */
-  char data[max_length];
+  streambuf buf;
 
   /**
    * file stream for get command
@@ -90,6 +90,13 @@ private:
    */
   void sendJson(const Json::Value &json);
 
+  /**
+   * Parses a line of the buffer to a json value object.
+   *
+   * @return json object
+   */
+  Json::Value parseMessage();
+
 public:
   /**
    * Pointer to a con_handler.

+ 30 - 30
daemon/src/Server.cpp

@@ -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 *
  **********/