//------------------------------------------------------------------------------ // Read a record //------------------------------------------------------------------------------ const DataRecordHandle* NetInput::readRecordImp() { // First pass? Does the file need to be opened? if (firstPassFlg) { if ( !networkInitialized ) { initNetworks(); } firstPassFlg = false; } DataRecordHandle* handle = 0; // When the file is open and ready ... if ( networkInitialized && netHandler->isConnected() ) { // --- // Try to read a message into 'ibuf' // --- unsigned int n = 0; n = netHandler->recvData( ibuf, MAX_INPUT_BUFFER_SIZE ); // --- // If we've successfully read a message from the network // then parse it as a DataRecord and put it into a Handle. // --- if (n > 0) { // Parse the data record std::string wireFormat(ibuf, n); Pb::DataRecord* dataRecord = new Pb::DataRecord(); bool ok = dataRecord->ParseFromString(wireFormat); if (ok) { // Create a handle for the data record (it now has ownership) handle = new DataRecordHandle(dataRecord); } else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) { std::cerr << "NetInput::readRecord() -- ParseFromString() error" << std::endl; delete dataRecord; dataRecord = 0; } } } return handle; }
//------------------------------------------------------------------------------ // Read a record //------------------------------------------------------------------------------ const DataRecordHandle* FileReader::readRecordImp() { DataRecordHandle* handle = nullptr; // First pass? Does the file need to be opened? if (firstPassFlg) { if ( !isOpen() && !isFailed() ) { openFile(); } firstPassFlg = false; } // When the file is open and ready ... if ( isOpen() && !isFailed() && !sin->eof() ) { // Number of bytes in the next serialized DataRecord unsigned int n = 0; // --- // Read the size of the next serialized DataRecord // --- char nbuff[8]; sin->read(nbuff, 4); // Check for error or eof if ( sin->eof() || sin->fail() ) { fileFailed = sin->fail(); if (fileFailed && isMessageEnabled(MSG_ERROR | MSG_WARNING)) { std::cerr << "FileReader::readRecord() -- error reading data record size" << std::endl; } } // Ok then get the size of the message from the buffer else { nbuff[4] = '\0'; n = std::atoi(nbuff); } // --- // Read the serialized DataRecord from the file, parse it as a DataRecord // and put it into a Handle. // --- if (n > 0) { // Read message into ibuf sin->read(ibuf, n); // Check for error or eof if ( sin->eof() || sin->fail() ) { if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) { std::cerr << "FileReader::readRecord() -- error reading data record" << std::endl; } fileFailed = true; } // Ok, create the DataRecord with handle else { // Parse the DataRecord std::string wireFormat(ibuf, n); Pb::DataRecord* dataRecord = new Pb::DataRecord(); bool ok = dataRecord->ParseFromString(wireFormat); // Create a handle for the DataRecord (it now has ownership) if (ok) { handle = new DataRecordHandle(dataRecord); } // parsing error else if (isMessageEnabled(MSG_ERROR | MSG_WARNING)) { std::cerr << "FileReader::readRecord() -- ParseFromString() error" << std::endl; delete dataRecord; dataRecord = nullptr; } } } } return handle; }