2017-11-17 22 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'を入手できます方法を示しすることはできますか? – Ptaq666

+0

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

答えて

0

は、私は問題は、私は、ヘッダーを解析した後、ストリームを調整していなかったということだったと思います。正しいコードを知りたい人のために

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

:今、私は唯一のソケットヘッダをのぞく

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"); 
} 
関連する問題