void NNTPConnector::readFromSocket(std::ostream &writeToMe, int const count, std::string const &escapeSequence) { //m_sockMutex.lock(); qint64 bytesRead = 0; int breakCount = 0; StreamWriter streamWriter(writeToMe, count, escapeSequence); do { std::vector<char> buffer; ReadFunctor readFunctor(nntp, buffer); bytesRead = readFunctor(); m_bytesAll += bytesRead; emit bytesReadSignal(bytesRead); breakCount = streamWriter(buffer); if(breakCount == count) { break; } // // Note, when breakCount >= count, there will be no more // incoming data and so we can safely break out and not bother // issuing a waitForReadyRead // } while (bytesRead > 0); //m_sockMutex.unlock(); }
void VTreeToStream(VTree_t *vt, void (*streamWriter)(uchar *, uint)) { uint count = vt->GetLocalCount(); uint index; uchar buffer[9]; for(index = 0; index < count; index++) { char *file = vt->GetLocal(index); streamWriter(file, strlen(file) + 1); if(vt->IsDir(index)) { buffer[0] = SIGN_DIR; streamWriter(buffer, 1); vt->IntoDir(index); VTreeToStream(vt, streamWriter); vt->ExitDir(); } else { uint64 size = vt->GetSize(index); uint64 readPos; uint readSize; void *block = memAlloc(WRITER_BUFFSIZE); buffer[0] = SIGN_FILE; value64ToBlock(buffer + 1, size); streamWriter(buffer, 9); for(readPos = 0; readPos < size; readPos += readSize) { readSize = m_min(WRITER_BUFFSIZE, (uint)(size - readPos)); vt->GetEntity(index, readPos, readSize, block); streamWriter(block, readSize); } memFree(block); } memFree(file); } buffer[0] = SIGN_ENDDIR; streamWriter(buffer, 1); }
// // Saves the profile to the WININET cache // uint SourceDynamicProfileManager::SaveToProfileCache() { AssertMsg(CONFIG_FLAG(WininetProfileCache), "Profile caching should be enabled for us to get here"); Assert(startupFunctions); uint bytesWritten = 0; #ifdef ENABLE_WININET_PROFILE_DATA_CACHE //TODO: Add some diffing logic to not write unless necessary IStream* writeStream; HRESULT hr = profileDataCache->GetWriteDataStream(&writeStream); if(FAILED(hr)) { return 0; } Assert(writeStream != nullptr); // stream writer owns the stream and will close it on destruction SimpleStreamWriter streamWriter(writeStream); DWORD jscriptMajorVersion; DWORD jscriptMinorVersion; if(FAILED(AutoSystemInfo::GetJscriptFileVersion(&jscriptMajorVersion, &jscriptMinorVersion))) { return 0; } if(!streamWriter.Write(jscriptMajorVersion)) { return 0; } if(!streamWriter.Write(jscriptMinorVersion)) { return 0; } if(!streamWriter.Write(startupFunctions->Length())) { return 0; } if(streamWriter.WriteArray(startupFunctions->GetData(), startupFunctions->WordCount())) { STATSTG stats; if(SUCCEEDED(writeStream->Stat(&stats, STATFLAG_NONAME))) { bytesWritten = stats.cbSize.LowPart; Assert(stats.cbSize.LowPart > 0); AssertMsg(stats.cbSize.HighPart == 0, "We should not be writing such long data that the high part is non-zero"); } hr = profileDataCache->SaveWriteDataStream(writeStream); if(FAILED(hr)) { return 0; } #if DBG_DUMP if(PHASE_TRACE1(Js::DynamicProfilePhase) && Js::Configuration::Global.flags.Verbose) { OUTPUT_VERBOSE_TRACE(Js::DynamicProfilePhase, L"Saved profile:\n"); startupFunctions->Dump(); } #endif } #endif return bytesWritten; }
void SimulatorController::timerEvent(QTimerEvent *event) { if (event->timerId() == m_timer.timerId()) { QString payload; QString currentElementName; QString Name, messageID, messageAction, symbolID, type; QXmlStreamWriter streamWriter(&payload); QString currentTimeString = QDateTime::currentDateTimeUtc().toString(SimulatorController::DATE_FORMAT); streamWriter.writeStartElement(TAG_ROOT); for(int messageThroughput = 0 ; messageThroughput < m_messageThroughput ; messageThroughput++) { while (!m_inputReader.atEnd()) { if(QStringRef::compare(m_inputReader.name(), TAG_MESSAGES) == 0) { m_inputReader.readNext(); if(m_inputReader.atEnd()) { m_reachedEndOfFile=true; m_inputReader.clear(); m_inputFile.reset(); m_inputReader.setDevice(&m_inputFile); m_currentIndex = 0; } continue; } else if (m_inputReader.isEndElement()) { streamWriter.writeEndElement(); if(QStringRef::compare(m_inputReader.name(), TAG_MESSAGE) == 0) { //Only emit readGeomessage if this is the first time we have read the message... if (m_reachedEndOfFile == false) { emit readGeomessage(Geomessage(Name, messageID, messageAction, symbolID, type)); } //Only emit advancedToGeomessage when it's the last message in this set... if (messageThroughput + 1 == m_messageThroughput) { emit advancedToGeomessage(m_currentIndex); } m_currentIndex++; break; } m_inputReader.readNext(); } else if (m_inputReader.isStartElement()) { currentElementName = m_inputReader.name().toString(); if (m_inputReader.prefix().toString().isEmpty()) { streamWriter.writeStartElement(m_inputReader.name().toString()); } else { streamWriter.writeStartElement(m_inputReader.prefix().toString()+ ":" + m_inputReader.name().toString()); } if (m_inputReader.attributes().size() > 0) { streamWriter.writeAttributes(m_inputReader.attributes()); } } else if (m_inputReader.isCharacters()) { QString text = m_inputReader.text().toString(); { QMutexLocker locker(&timeOverrideFieldsMutex); if (m_timeOverrideFields.contains(currentElementName)) { text = currentTimeString; } } streamWriter.writeCharacters(text); if (TAG_NAME == currentElementName) { Name = text; } else if (TAG_ACTION == currentElementName) { messageAction = text; } else if (TAG_ID == currentElementName) { messageID = text; } else if (TAG_SIC == currentElementName) { symbolID = text; } else if (TAG_TYPE == currentElementName) { type = text; } } else if (m_inputReader.isComment()) { /* leave not import */ } else if (m_inputReader.isCDATA()) { /* take cdata */ } m_inputReader.readNext(); } m_inputReader.readNext(); } streamWriter.writeEndElement(); QByteArray datagram; datagram += payload; QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); for (int i = 0; i < ifaces.size(); i++) { QNetworkInterface::InterfaceFlags flags = ifaces[i].flags(); if (!(flags & QNetworkInterface::IsLoopBack) && (flags & QNetworkInterface::IsUp)) { QList<QNetworkAddressEntry> entries = ifaces[i].addressEntries(); for (int j = 0; j < entries.size(); j++) { m_udpSocket->writeDatagram(datagram.data(), datagram.size(), entries[j].broadcast(), getPort()); } } } if (verbose()) { consoleOut << payload << endl; } } else { QObject::timerEvent(event); } }