2017-05-09 53 views
1

我正在使用boost读取TCP流。该流的格式如下:用boost :: asio读取JSON流,获取完整的字符串?

"{\"animationValues\":{\"mouth_rightMouth_stretch\":0.0000000000000000,\"mouth_leftMouth_narrow\":0.00000000000000000,\"mouth_up\":0.0000000000000000,\"mouth_leftMouth_stretch\":0.0000000000000000,\"mouth_rightMouth_narrow\":0.00000000000000000,\"mouth_down\":0.00000000000000000,\"mouth_upperLip_left_up\":0.0000000000000000,\"mouth_upperLip_right_up\":0.0000000000000000,\"mouth_lowerLip_left_down\":0.0000000000000000,\"mouth_lowerLip_right_down\":0.0000000000000000,\"mouth_leftMouth_frown\":0.0000000000000000,\"mouth_rightMouth_frown\":0.0000000000000000,\"mouth_leftMouth_smile\":0.00000000000000000,\"mouth_rightMouth_smile\":0.00000000000000000,\"eyes_lookRight\":0.0000000000000000,\"eyes_lookLeft\":0.00000000000000000,\"eyes_lookDown\":0.0000000000000000,\"eyes_lookUp\":0.00000000000000000,\"eyes_leftEye_blink\":0.00000000000000000,\"eyes_rightEye_blink\":0.00000000000000000,\"eyes_leftEye_wide\":0.0000000000000000,\"eyes_rightEye_wide\":0.0000000000000000,\"brows_leftBrow_up\":0.0000000000000000,\"brows_leftBrow_down\":0.00000000000000000,\"brows_rightBrow_up\":0.0000000000000000,\"brows_rightBrow_down\":0.00000000000000000,\"brows_midBrows_up\":0.0000000000000000,\"brows_midBrows_down\":0.00000000000000000,\"jaw_open\":0.0000000000000000,\"jaw_left\":0.0000000000000000,\"jaw_right\":0.00000000000000000,\"mouth_phoneme_oo\":0.0000000000000000,\"mouth_right\":0.0000000000000000,\"mouth_left\":0.00000000000000000,\"mouth_phoneme_mbp\":0.0000000000000000,\"mouth_phoneme_ch\":0.0000000000000000},\"headRot\":[0.0,0.0, 0.0]}"; 

我想阅读和分析每个字符串作为它有那么我需要做的就是流分成看起来像上面的部分。我曾尝试过:

boost::system::error_code error; 
    boost::asio::streambuf buffer; 
    boost::asio::read_until(sock, buffer, "]}"", error); 
    std::istream str(&buffer); 

但这只给我一半的字符串,我需要。我已经尝试过:

boost::array<char, 2046> buf; 
size_t len = sock.read_some(boost::asio::buffer(buf), error); 
std::string data(buf.begin(), buf.end()); 

但是这让我更少。我怎样才能一次读取流的一部分?谢谢。

+0

你是否检查错误? – sehe

+0

代码编译并运行。它连接到套接字并传输数据。但它一次只能是弦的一部分,而不是整个事物。 – anti

+0

我的意思是,你检查了'error'的值。哪个会报告错误。 – sehe

回答

0

是的,我会使用read_until或boost::asio::read_*家族中的任何一个,因为它们组成读操作(与read_some构成)。

下面是这个工作的POC:

Live On Coliru

#include <boost/asio.hpp> 
#include <iostream> 

int main() 
{ 
    using namespace boost::asio; 
    using ip::tcp; 

    io_service io; 
    tcp::acceptor a(io, { {}, 6768 }); 
    a.listen(5); 

    tcp::socket s(io); 
    a.accept(s); 

    streambuf sb; 
    boost::system::error_code ec; 
    read_until(s, sb, "]}", ec); 

    std::cout << "Response: " << ec.message() << "\n"; 
    std::cout << "'" << &sb << "'\n"; 
} 

正如你所看到的,在发送时

{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]} 
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow": 1,"mouth_down": 1,"mouth_upperLip_left_up": 1,"mouth_upperLip_right_up": 1,"mouth_lowerLip_left_down": 1,"mouth_lowerLip_right_down": 1,"mouth_leftMouth_frown": 1,"mouth_rightMouth_frown": 1,"mouth_leftMouth_smile": 1,"mouth_rightMouth_smile": 1,"eyes_lookRight": 1,"eyes_lookLeft": 1,"eyes_lookDown": 1,"eyes_lookUp": 1,"eyes_leftEye_blink": 1,"eyes_rightEye_blink": 1,"eyes_leftEye_wide": 1,"eyes_rightEye_wide": 1,"brows_leftBrow_up": 1,"brows_leftBrow_down": 1,"brows_rightBrow_up": 1,"brows_rightBrow_down": 1,"brows_midBrows_up": 1,"brows_midBrows_down": 1,"jaw_open": 1,"jaw_left": 1,"jaw_right": 1,"mouth_phoneme_oo": 1,"mouth_right": 1,"mouth_left": 1,"mouth_phoneme_mbp": 1,"mouth_phoneme_ch": 1},"headRot": [1, 1, 1]} 
{"animationValues": {"mouth_rightMouth_stretch": 2,"mouth_leftMouth_narrow": 2,"mouth_up": 2,"mouth_leftMouth_stretch": 2,"mouth_rightMouth_narrow": 2,"mouth_down": 2,"mouth_upperLip_left_up": 2,"mouth_upperLip_right_up": 2,"mouth_lowerLip_left_down": 2,"mouth_lowerLip_right_down": 2,"mouth_leftMouth_frown": 2,"mouth_rightMouth_frown": 2,"mouth_leftMouth_smile": 2,"mouth_rightMouth_smile": 2,"eyes_lookRight": 2,"eyes_lookLeft": 2,"eyes_lookDown": 2,"eyes_lookUp": 2,"eyes_leftEye_blink": 2,"eyes_rightEye_blink": 2,"eyes_leftEye_wide": 2,"eyes_rightEye_wide": 2,"brows_leftBrow_up": 2,"brows_leftBrow_down": 2,"brows_rightBrow_up": 2,"brows_rightBrow_down": 2,"brows_midBrows_up": 2,"brows_midBrows_down": 2,"jaw_open": 2,"jaw_left": 2,"jaw_right": 2,"mouth_phoneme_oo": 2,"mouth_right": 2,"mouth_left": 2,"mouth_phoneme_mbp": 2,"mouth_phoneme_ch": 2},"headRot": [2, 2, 2]} 

它打印:

Response: Success 
'{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]} 
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow' 

事情需要注意的:

  1. 确保格式实际上这样]}发生的。这意味着,漂亮打印JSON无法识别:

     "mouth_phoneme_mbp": 2, 
         "mouth_phoneme_ch": 2 
        }, 
        "headRot": [2, 2, 2] 
    } 
    
  2. 也确保你都知道的事实,read_until可以尽快作为分隔符是返回一个包含更多数据的缓冲区(它会返回满意,但更多的数据可能已被阅读)