IdSet InputFrame::unpackIdSet(){ IdSet set; uint32_t msize = unpackInt(); for(uint32_t i = 0; i < msize; ++i){ set.insert( unpackInt() ); } return set; }
IdMap InputFrame::unpackMap(){ IdMap map; uint32_t msize = unpackInt(); for(uint32_t i = 0; i < msize; ++i){ uint32_t idx = unpackInt(); map[idx] = unpackInt(); } return map; }
VRDataQueue::serialData VRNetInterface::waitForAndReceiveEventData(SOCKET socketID) { // 1. receive 1-byte message header waitForAndReceiveOneByte(socketID, EVENTS_MSG); // 2. receive int that tells us the size of the data portion of the // message in bytes unsigned char buf1[VRNET_SIZEOFINT]; int status = receiveall(socketID, buf1, VRNET_SIZEOFINT); if (status == -1) { std::cerr << "NetInterface error: receiveall failed." << std::endl; exit(1); } int dataSize = unpackInt(buf1); //std::cout << "dataSize = " << dataSize << std::endl; // 3. receive dataSize bytes, then decode these as InputEvents unsigned char *buf2 = new unsigned char[dataSize+1]; status = receiveall(socketID, buf2, dataSize); if ((status == -1) || (status != dataSize)) { std::cerr << "NetInterface error: receiveall failed." << std::endl; exit(1); } buf2[dataSize] = '\0'; std::string data(reinterpret_cast<const char*>(buf2)); delete buf2; return data; }
std::string InputFrame::unpackString(){ uint32_t len = unpackInt(); if ( unpackptr + len > size ) { throw FrameException( fec_FrameError, "Not enough raw_data.to unpack string!" ); } const char* pos = body_buffer + unpackptr; unpackptr += len; if ( pad_strings ) { int pad = unpackptr % 4; if ( pad != 0 ) { if ( unpackptr + (4 - pad) > size ) { throw FrameException( fec_FrameError, "Not enough raw_data.to unpack string padding!" ); } unpackptr += 4-pad; } } return std::string(pos, len); }
std::string InputFrame::unpackString(){ uint32_t len = unpackInt(); if(unpackptr + len > data.length()){ throw FrameException( fec_FrameError, "Not enough data to unpack string!" ); } const char* pos = data.c_str() + unpackptr; unpackptr += len; if(padstrings){ int pad = unpackptr % 4; if(pad != 0){ if(unpackptr + (4 - pad) > data.length()){ throw FrameException( fec_FrameError, "Not enough data to unpack string padding!" ); } unpackptr += 4-pad; } } return std::string(pos, len); }
/*! \brief Unpacks a string from the buffer. \return The string unpacked. */ std::string Buffer::unpackString(){ uint32_t len = unpackInt(); uint32_t pad = 0; if(stringpadding){ pad = 4 - (len % 4); if(pad == 4) pad = 0; } if (len >= 0 && datalen >= dataptr + len + pad) { std::string rtnstr(data+dataptr, len); dataptr += len + pad; return rtnstr; } else { //Logger::getLogger()->debug("len < 0 or length < upackptr + len"); std::cerr << "Buffer::unpackString(): len < 0 or length < upackptr + len + pad" << std::endl; std::cout << "len: " << len << " pad: " << pad << " length: " << datalen << " upackptr: " << dataptr << std::endl; throw new std::exception(); } //printf("unpackptr %d\n", unpackptr); return std::string(); }