Exemplo n.º 1
0
static string spReadString(const ZStreamR& r)
	{
	if (size_t theLength = r.ReadCount())
		return r.ReadString(theLength);
	return string();
	}
Exemplo n.º 2
0
bool ZTSWatcherServerAsync::Read(const ZStreamR& iStreamR)
{
    if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
        s << "Read, start";

    EReq theReq = (EReq)iStreamR.ReadUInt8();

    switch (theReq)
    {
    case eReq_Close:
    {
        ZMutexLocker locker(fMutex);
        fSendClose = true;
        locker.Release();
        ZStreamerWriter::Wake();
        return false;
    }
    case eReq_IDs:
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Read, eReq_IDs";
        const size_t theIDsNeeded = iStreamR.ReadCount();
        ZMutexLocker locker(fMutex);
        fIDsNeeded += theIDsNeeded;
        locker.Release();
        ZStreamerWriter::Wake();
        break;
    }
    case eReq_Sync:
    {
        if (ZLOG(s, eDebug, "ZTSWatcherServerAsync"))
            s << "Read, eReq_Sync";
        vector<uint64> removedIDs;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            removedIDs.reserve(theCount);
            while (theCount--)
                removedIDs.push_back(iStreamR.ReadUInt64());
        }

        vector<uint64> addedIDs;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            addedIDs.reserve(theCount);
            while (theCount--)
                addedIDs.push_back(iStreamR.ReadUInt64());
        }

        vector<int64> removedQueries;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            removedQueries.reserve(theCount);
            while (theCount--)
                removedQueries.push_back(iStreamR.ReadInt64());
        }

        vector<ZTSWatcher::AddedQueryCombo> addedQueries;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            addedQueries.reserve(theCount);
            while (theCount--)
            {
                const int64 theRefcon = iStreamR.ReadInt64();
                const bool thePrefetch = iStreamR.ReadBool();
                const size_t theSize = iStreamR.ReadCount();

                ZTSWatcher::AddedQueryCombo theCombo(theSize);
                theCombo.fRefcon = theRefcon;
                theCombo.fPrefetch = thePrefetch;

                iStreamR.Read(theCombo.fMemoryBlock.GetPtrMutable(), theSize);

                addedQueries.push_back(theCombo);
            }
        }

        vector<uint64> writtenTupleIDs;
        vector<ZTuple> writtenTuples;
        bool writeNeededSort = false;
        if (uint32 theCount = iStreamR.ReadCount())
        {
            writtenTupleIDs.reserve(theCount);
            writtenTuples.reserve(theCount);
            uint64 priorID = 0;
            while (theCount--)
            {
                const uint64 currentID = iStreamR.ReadUInt64();
                if (priorID >= currentID)
                    writeNeededSort = true;
                priorID = currentID;

                writtenTupleIDs.push_back(currentID);
                writtenTuples.push_back(ZTuple(iStreamR));
            }

            if (writeNeededSort)
                spSort(writtenTupleIDs, writtenTuples);
        }

        ZMutexLocker locker(fMutex);

        ZAssert(fRemovedIDs.empty());
        ZAssert(fAddedIDs.empty());
        ZAssert(fRemovedQueries.empty());
        ZAssert(fAddedQueries.empty());
        ZAssert(fWrittenTupleIDs.empty());
        ZAssert(fWrittenTuples.empty());
        ZAssert(!fSyncNeeded);

        fRemovedIDs.swap(removedIDs);
        fAddedIDs.swap(addedIDs);
        fRemovedQueries.swap(removedQueries);
        fAddedQueries.swap(addedQueries);
        fWrittenTupleIDs.swap(writtenTupleIDs);
        fWrittenTuples.swap(writtenTuples);

        fSyncNeeded = true;

        locker.Release();
        ZStreamerWriter::Wake();
        break;
    }
    }
    return true;
}