bool CachedObjectMgr::SaveCachedToFile(const std::string &cacheDir, const PyRep *objectID) const {
    const std::string str = OIDToString(objectID);
    std::map<std::string, CacheRecord *>::const_iterator res;
    res = m_cachedObjects.find(str);
    if(res == m_cachedObjects.end())
        return false;   //cant save something we dont have...

    std::string filename(cacheDir);
    filename += "/";
    filename += str;
    filename += ".cache";

    FILE *f = fopen(filename.c_str(), "wb");
    if(f == NULL) {
        return false;
    }

    CacheFileHeader header;
    header.timestamp = res->second->timestamp;
    header.version = res->second->version;
    header.magic = CacheFileMagic;
    header.length = res->second->cache->content().size();
    if(fwrite(&header, sizeof(header), 1, f) != 1) {
        fclose(f);
        return false;
    }
    if(fwrite(&res->second->cache->content()[0], sizeof(uint8), header.length, f) != header.length) {
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}
Esempio n. 2
0
PyObject *CachedObjectMgr::GetCachedObject(const PyRep *objectID)
{
    const std::string str = OIDToString(objectID);

    CachedObjMapItr res = m_cachedObjects.find(str);
    if(res == m_cachedObjects.end())
        return NULL;

    PyCachedObject co;
    co.timestamp = res->second->timestamp;
    co.version = res->second->version;
    co.nodeID = HackCacheNodeID;    //hack, doesn't matter until we have multi-node networks.
    co.shared = true;
    co.objectID = res->second->objectID; PyIncRef(res->second->objectID);
    co.cache = res->second->cache;

    if(res->second->cache->content().size() == 0 || res->second->cache->content()[0] == MarshalHeaderByte)
        co.compressed = false;
    else
        co.compressed = true;

	sLog.Debug("CachedObjMgr","Returning cached object '%s' with checksum 0x%x", str.c_str(), co.version);

    PyObject *result = co.Encode();
    co.cache = NULL;    //avoid a copy

    return result;
}
Esempio n. 3
0
void CachedObjectMgr::_UpdateCache(const PyRep *objectID, PyBuffer **buffer)
{
    //this is the hard one..
    CacheRecord *r = new CacheRecord;
    r->timestamp = Win32TimeNow();
    r->objectID = (PyRep*)objectID; PyIncRef(objectID);

	// retake ownership
    r->cache = *buffer;
	*buffer = NULL;

    r->version = CRC32::Generate( &r->cache->content()[0], r->cache->content().size() );

    const std::string str = OIDToString(objectID);

    //find and destroy any older version of this object.
    CachedObjMapItr res = m_cachedObjects.find(str);

    if(res != m_cachedObjects.end()) {

		sLog.Debug("CachedObjMgr","Destroying old cached object with ID '%s' of length %u with checksum 0x%x", str.c_str(), res->second->cache->content().size(), res->second->version); 
		SafeDelete( res->second );
    }

	sLog.Debug("CachedObjMgr","Registering new cached object with ID '%s' of length %u with checksum 0x%x", str.c_str(), r->cache->content().size(), r->version);

    m_cachedObjects[str] = r;
}
PyObject *CachedObjectMgr::MakeCacheHint(const PyRep *objectID) {
    const std::string str = OIDToString(objectID);

    std::map<std::string, CacheRecord *>::iterator res = m_cachedObjects.find(str);
    if(res == m_cachedObjects.end())
        return NULL;
    return(res->second->EncodeHint());
}
Esempio n. 5
0
PyObject *CachedObjectMgr::MakeCacheHint(const PyRep *objectID)
{
    const std::string str = OIDToString(objectID);

    CachedObjMapItr res = m_cachedObjects.find(str);
    if(res == m_cachedObjects.end())
        return NULL;

    return res->second->EncodeHint();
}
Esempio n. 6
0
void CachedObjectMgr::InvalidateCache(const PyRep *objectID)
{
    const std::string str = OIDToString(objectID);
    CachedObjMapItr res = m_cachedObjects.find(str);

    if(res != m_cachedObjects.end()) {
        SafeDelete( res->second );
        m_cachedObjects.erase(res);
    }
}
void CachedObjectMgr::InvalidateCache(const PyRep *objectID) {
    const std::string str = OIDToString(objectID);

    std::map<std::string, CacheRecord *>::iterator res = m_cachedObjects.find(str);;
    if(res != m_cachedObjects.end())
    {
        SafeDelete( res->second );
        m_cachedObjects.erase(res);
    }
}
bool CachedObjectMgr::IsCacheUpToDate(const PyRep *objectID, uint32 version, uint64 timestamp) {
    const std::string str = OIDToString(objectID);

    std::map<std::string, CacheRecord *>::iterator res = m_cachedObjects.find(str);
    if(res == m_cachedObjects.end())
        return false;

    //for now, only support exact matches...
    return (   res->second->version == version
            && res->second->timestamp == timestamp);
}
Esempio n. 9
0
/**
 * LoadCachedFromFile
 *
 * Load a cached object from file.
 */
bool CachedObjectMgr::LoadCachedFromFile(const std::string &cacheDir, const PyRep *objectID)
{
    const std::string str = OIDToString(objectID);

    std::string filename(cacheDir);
    filename += "/" + str + ".cache";

    FILE *f = fopen(filename.c_str(), "rb");

    if(f == NULL)
        return false;

    CacheFileHeader header;
    if(fread(&header, sizeof(header), 1, f) != 1) {
        fclose(f);
        return false;
    }

    /* check if its a valid cache file */
    if(header.magic != CacheFileMagic) {
        fclose(f);
        return false;
    }

    Buffer* buf = new Buffer( header.length );

    if( fread( &(*buf)[0], sizeof( uint8 ), header.length, f ) != header.length ) {
        SafeDelete( buf );
        fclose( f );
        return false;
    }

    fclose( f );

    CachedObjMapItr res = m_cachedObjects.find( str );

    if( res != m_cachedObjects.end() )
        SafeDelete( res->second );

    CacheRecord* cache = new CacheRecord;
    
    cache->objectID = (PyRep*)objectID; PyIncRef(objectID);
    cache->cache = new PyBuffer( &buf );
    cache->timestamp = header.timestamp;
    cache->version = header.version;

    m_cachedObjects[ str ] = cache;

    return true;
}
Esempio n. 10
0
bool CachedObjectMgr::LoadCachedFromFile(const std::string &cacheDir, const PyRep *objectID) {
    const std::string str = OIDToString(objectID);

    std::string filename(cacheDir);
    filename += "/";
    filename += str;
    filename += ".cache";

    FILE *f = fopen(filename.c_str(), "rb");
    if(f == NULL) {
        return false;
    }

    CacheFileHeader header;
    if(fread(&header, sizeof(header), 1, f) != 1) {
        fclose(f);
        return false;
    }
    if(header.magic != CacheFileMagic) {
        //not a valid cache file.
        fclose(f);
        return false;
    }

    Buffer* buf = new Buffer( header.length );
    if( fread( &(*buf)[0], sizeof( uint8 ), header.length, f ) != header.length )
    {
        SafeDelete( buf );

        fclose( f );
        return false;
    }
    fclose( f );

    std::map<std::string, CacheRecord *>::iterator res = m_cachedObjects.find( str );
    if( res != m_cachedObjects.end() )
        SafeDelete( res->second );

    CacheRecord* cache = m_cachedObjects[ str ] = new CacheRecord;
    cache->objectID = objectID->Clone();
    cache->cache = new PyBuffer( &buf );
    cache->timestamp = header.timestamp;
    cache->version = header.version;

    return true;
}
Esempio n. 11
0
bool CachedObjectMgr::SaveCachedToFile(const std::string &cacheDir, const PyRep *objectID) const
{
    const std::string str = OIDToString(objectID);
    CachedObjMapConstItr res = m_cachedObjects.find(str);

    /* make sure we don't try to save a object we don't have */
    if(res == m_cachedObjects.end())
        return false;

    std::string filename(cacheDir);
    filename += "/";
    filename += str;
    filename += ".cache";

    FILE *f = fopen(filename.c_str(), "wb");

    if(f == NULL)
        return false;

    CacheFileHeader header;
    header.timestamp = res->second->timestamp;
    header.version = res->second->version;
    header.magic = CacheFileMagic;
    header.length = res->second->cache->content().size();
    
    if(fwrite(&header, sizeof(header), 1, f) != 1) {
        fclose(f);
        return false;
    }
    
    if(fwrite(&res->second->cache->content()[0], sizeof(uint8), header.length, f) != header.length) {
        __asm{int 3};
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}
Esempio n. 12
0
bool CachedObjectMgr::HaveCached(const PyRep *objectID) const
{
    const std::string str = OIDToString(objectID);

    return m_cachedObjects.find(str) != m_cachedObjects.end();
}