2017-11-17 7 views
0

나는 이것을 제대로하고 있는지 확실하지 않습니다. Merge와 ParseFromCodedStream을 모두 시도한 경우. 아래의 코드 및 출력 :Protofuf C++ - 역 직렬화 된 메시지에 데이터가 없음

bool tcp_connection::readDelimitedFrom(
    google::protobuf::io::ZeroCopyInputStream* rawInput, 
    jvm* message, uint32_t* payloadSize) { 

    DEBUG && (cerr << "------ readDelimitedFrom.begin " << '\n'); 
    // We create a new coded stream for each message. Don't worry, this is fast, 
    // and it makes sure the 64MB total size limit is imposed per-message rather 
    // than on the whole stream. (See the CodedInputStream interface for more 
    // info on this limit.) 
    google::protobuf::io::CodedInputStream input(rawInput); 

    // Read the size. 
    if (!input.ReadLittleEndian32(payloadSize)) return false; 

    DEBUG && (cerr << "------ readDelimitedFrom got size: " << *payloadSize << '\n'); 
    DEBUG && (cerr << "------ input.CurrentPosition: " << input.CurrentPosition() << '\n'); 

    DEBUG && (cerr << "------ " << '\n'); 
    try { 

     google::protobuf::io::CodedInputStream::Limit limit = 
      input.PushLimit(*payloadSize); 
     DEBUG && (cerr << "------ PushLimit:" << *payloadSize << '\n'); 

     // Parse the message. 
     if (!message->ParseFromCodedStream(&input)) return false; 
     DEBUG && (cerr << "------ MergeFromCodedStream" << '\n'); 
        cout << "----------- size:" << message->ByteSize() << endl; 

     if (!input.ConsumedEntireMessage()) return false; 
     DEBUG && (cerr << "------ ConsumedEntireMessage" << '\n'); 


     // Release the limit. 
     input.PopLimit(limit); 
     DEBUG && (cerr << "------ PopLimit" << '\n'); 
    } 
    catch (const std::exception &exc) 
    { 
     // catch anything thrown within try block that derives from std::exception 
     cerr << exc.what() << endl; 
     return false; 
    } 
    // Tell the stream not to read beyond that size. 
    DEBUG && (cerr << "------ readDelimitedFrom return true " << '\n'); 

    return true; 
} 

콘솔 아웃 :

---- connection.start (disabling Nagle) 
------ h.r.h 4 bytes 
------ readDelimitedFrom.begin 
------ readDelimitedFrom got size: 76 
------ input.CurrentPosition: 4 
------ 
------ PushLimit:76 
------ MergeFromCodedStream 
----------- size:0 
------ ConsumedEntireMessage 
------ PopLimit 
------ readDelimitedFrom return true 
+0

당신은 또한 당신이'rawInput'을 얻을 어떻게 보여줄 수 : 올바른 코드를 알고 싶은 사람들을위한

char buf[HEADER_SIZE]; socket().receive(boost::asio::buffer(buf), tcp::socket::message_peek); 

: 지금은 단지 소켓 헤더를 들여다? – Ptaq666

+0

boost :: asio :: async_read (socket(), mutable_buffers_1, –

답변

0

나는 문제가 내가 헤더를 구문 분석 후 스트림을 조정되지 않았 음을 생각.

ArrayInputStream ais(&m_readbuf[HEADER_SIZE], msg_len - HEADER_SIZE); 
CodedInputStream cis(&ais); 
jvm jvm; 
bool isParsed = jvm.ParseFromCodedStream(&cis); 

if (isParsed) { 
    DEBUG && (cerr << "jvm:[" << jvm.name() << "] size:" << jvm.ByteSize() << endl); 
    DEBUG && (cerr << "Got body:\n"); 
    DEBUG && (cerr << show_hex(m_readbuf) << endl); 
    handle_request(&jvm); 
} 
else { 
    DEBUG && (cerr << "didnt parse <----\n"); 
}