Example #1
0
void OutputMessage::addU16(uint16 value)
{
    checkWrite(2);
    stdext::writeLE16(m_buffer + m_writePos, value);
    m_writePos += 2;
    m_messageSize += 2;
}
Example #2
0
void OutputMessage::addU32(uint32 value)
{
    checkWrite(4);
    stdext::writeLE32(m_buffer + m_writePos, value);
    m_writePos += 4;
    m_messageSize += 4;
}
Example #3
0
void OutputMessage::addU16(uint16 value)
{
    checkWrite(2);
    *(uint16_t*)(m_buffer + m_writePos) = value;
    m_writePos += 2;
    m_messageSize += 2;
}
Example #4
0
void OutputMessage::addU64(uint64 value)
{
    checkWrite(8);
    stdext::writeLE64(m_buffer + m_writePos, value);
    m_writePos += 8;
    m_messageSize += 8;
}
Example #5
0
static VALUE
memory_put_bytes(int argc, VALUE* argv, VALUE self)
{
    AbstractMemory* ptr = MEMORY(self);
    VALUE offset = Qnil, str = Qnil, rbIndex = Qnil, rbLength = Qnil;
    long off, len, idx;
    int nargs = rb_scan_args(argc, argv, "22", &offset, &str, &rbIndex, &rbLength);

    Check_Type(str, T_STRING);

    off = NUM2LONG(offset);
    idx = nargs > 2 ? NUM2LONG(rbIndex) : 0;
    if (idx < 0) {
        rb_raise(rb_eRangeError, "index canot be less than zero");
        return Qnil;
    }
    len = nargs > 3 ? NUM2LONG(rbLength) : (RSTRING_LEN(str) - idx);
    if ((idx + len) > RSTRING_LEN(str)) {
        rb_raise(rb_eRangeError, "index+length is greater than size of string");
        return Qnil;
    }

    checkWrite(ptr);
    checkBounds(ptr, off, len);

    if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
        rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
        return Qnil;
    }
    memcpy(ptr->address + off, RSTRING_PTR(str) + idx, len);

    return self;
}
Example #6
0
void GaduSocketNotifiers::createSocketNotifiers()
{
	kdebugf();

	deleteSocketNotifiers();

	if (0 >= Socket)
		return;

	ReadNotifier = new QSocketNotifier(Socket, QSocketNotifier::Read, this);
	connect(ReadNotifier, SIGNAL(activated(int)), this, SLOT(dataReceived()));
	if (!checkRead())
		ReadNotifier->setEnabled(false);

	WriteNotifier = new QSocketNotifier(Socket, QSocketNotifier::Write, this);
	connect(WriteNotifier, SIGNAL(activated(int)), this, SLOT(dataSent()));
	if (!checkWrite())
		WriteNotifier->setEnabled(false);

	TimeoutTimer = new QTimer();
	TimeoutTimer->setSingleShot(true);
	connect(TimeoutTimer, SIGNAL(timeout()), this, SLOT(socketTimeout()));

	Started = true;

	int tout = timeout();
	if (0 < tout)
		TimeoutTimer->start(tout);

	kdebugf2();
}
Example #7
0
BOOL LLVOCache::updateEntry(const HeaderEntryInfo* entry)
{
	LLAPRFile* apr_file = new LLAPRFile(mHeaderFileName, APR_WRITE|APR_BINARY, mLocalAPRFilePoolp);
	apr_file->seek(APR_SET, entry->mIndex * sizeof(HeaderEntryInfo) + sizeof(HeaderMetaInfo)) ;

	return checkWrite(apr_file, (void*)entry, sizeof(HeaderEntryInfo)) ;
}
Example #8
0
void OutputMessage::addU32(uint32 value)
{
    checkWrite(4);
    *(uint32*)(m_buffer + m_writePos) = value;
    m_writePos += 4;
    m_messageSize += 4;
}
Example #9
0
void OutputMessage::addU64(uint64 value)
{
    checkWrite(8);
    *(uint64*)(m_buffer + m_writePos) = value;
    m_writePos += 8;
    m_messageSize += 8;
}
Example #10
0
void OutputMessage::addU8(uint8 value)
{
    checkWrite(1);
    m_buffer[m_writePos] = value;
    m_writePos += 1;
    m_messageSize += 1;
}
Example #11
0
void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
{
    if(bytes <= 0)
        return;
    checkWrite(bytes);
    memset((void*)&m_buffer[m_writePos], byte, bytes);
    m_writePos += bytes;
    m_messageSize += bytes;
}
Example #12
0
void InputMessage::setBuffer(const std::string& buffer)
{
    int len = buffer.size();
    checkWrite(MAX_HEADER_SIZE + len);
    memcpy(m_buffer + MAX_HEADER_SIZE, buffer.c_str(), len);
    m_readPos = MAX_HEADER_SIZE;
    m_headerPos = MAX_HEADER_SIZE;
    m_messageSize = len;
}
Example #13
0
void LLVOCache::writeCacheHeader()
{
	if(mReadOnly)
	{
		return ;
	}	

	LLAPRFile* apr_file = new LLAPRFile(mHeaderFileName, APR_CREATE|APR_WRITE|APR_BINARY, mLocalAPRFilePoolp);

	//write the meta element
	if(!checkWrite(apr_file, &mMetaInfo, sizeof(HeaderMetaInfo)))
	{
		return ;
	}

	mNumEntries = 0 ;
	for(header_entry_queue_t::iterator iter = mHeaderEntryQueue.begin() ; iter != mHeaderEntryQueue.end(); ++iter)
	{
		(*iter)->mIndex = mNumEntries++ ;
		if(!checkWrite(apr_file, (void*)*iter, sizeof(HeaderEntryInfo)))
		{
			return ;
		}
	}

	mNumEntries = mHeaderEntryQueue.size() ;
	if(mNumEntries < MAX_NUM_OBJECT_ENTRIES)
	{
		HeaderEntryInfo* entry = new HeaderEntryInfo() ;
		for(S32 i = mNumEntries ; i < MAX_NUM_OBJECT_ENTRIES ; i++)
		{
			//fill the cache with the default entry.
			if(!checkWrite(apr_file, entry, sizeof(HeaderEntryInfo)))
			{
				mReadOnly = TRUE ; //disable the cache.
				return ;
			}
		}
		delete entry ;
	}
	delete apr_file ;
}
Example #14
0
void OutputMessage::addString(const std::string& buffer)
{
    int len = buffer.length();
    if(len > MAX_STRING_LENGTH)
        g_lua.throwError(stdext::format("string length > %d", MAX_STRING_LENGTH));
    checkWrite(len + 2);
    addU16(len);
    memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len);
    m_writePos += len;
    m_messageSize += len;
}
Example #15
0
void OutputMessage::addString(const char* value)
{
    size_t stringLength = strlen(value);
    if(stringLength > 65535)
        throw NetworkException("[OutputMessage::addString] string length > 65535");
    checkWrite(stringLength + 2);
    addU16(stringLength);
    strcpy((char*)(m_buffer + m_writePos), value);
    m_writePos += stringLength;
    m_messageSize += stringLength;
}
Example #16
0
void RingQueue::Write(const uint8_t* pbuffer, uint32_t len)
{
	checkWrite(len);
	len = std::min(len,(_capacity-_size));
	/* first put the data starting from fifo->in to buffer end */
	uint32_t n = std::min(len,_capacity - _in);
	memcpy(_buffer + _in, pbuffer,n);
	/* then put the rest (if any) at the beginning of the buffer */
	memcpy(_buffer, pbuffer + n, len - n);
	_in += len;
	_in %=_capacity;
	_size += len;
}
Example #17
0
void GaduSocketNotifiers::enable()
{
	kdebugf();

	if (!Started || Lock)
		return;

	if (checkRead())
		ReadNotifier->setEnabled(true);
	if (checkWrite())
		WriteNotifier->setEnabled(true);

	int tout = timeout();
	if (0 < tout)
		TimeoutTimer->start(tout);
}
Example #18
0
/*
 * call-seq: memory.put_string(offset, str)
 * @param [Numeric] offset
 * @param [String] str
 * @return [self]
 * @raise {SecurityError} when writing unsafe string to memory
 * @raise {IndexError} if +offset+ is too great
 * @raise {NullPointerError} if memory not initialized
 * Put a string in memory.
 */
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
{
    AbstractMemory* ptr = MEMORY(self);
    long off, len;

    Check_Type(str, T_STRING);
    off = NUM2LONG(offset);
    len = RSTRING_LEN(str);

    checkWrite(ptr);
    checkBounds(ptr, off, len + 1);

    memcpy(ptr->address + off, RSTRING_PTR(str), len);
    *((char *) ptr->address + off + len) = '\0';

    return self;
}
Example #19
0
File: Struct.c Project: mjaric/ffi
/*
 * call-seq: []=(index, value)
 * @param [Numeric] index
 * @param [Type, Struct]
 * @return [value]
 */
static VALUE
inline_array_aset(VALUE self, VALUE rbIndex, VALUE rbValue)
{
    InlineArray* array;

    Data_Get_Struct(self, InlineArray, array);

    if (array->op != NULL) {
        if (unlikely(array->componentType->nativeType == NATIVE_MAPPED)) {
            rbValue = rb_funcall(((MappedType *) array->componentType)->rbConverter, 
                    rb_intern("to_native"), 2, rbValue, Qnil);
        }
        array->op->put(array->memory, inline_array_offset(array, NUM2INT(rbIndex)),
            rbValue);
        
    } else if (array->componentType->nativeType == NATIVE_STRUCT) {
        int offset = inline_array_offset(array, NUM2INT(rbIndex));
        Struct* s;

        if (!rb_obj_is_kind_of(rbValue, rbffi_StructClass)) {
            rb_raise(rb_eTypeError, "argument not an instance of struct");
            return Qnil;
        }

        checkWrite(array->memory);
        checkBounds(array->memory, offset, array->componentType->ffiType->size);

        Data_Get_Struct(rbValue, Struct, s);
        checkRead(s->pointer);
        checkBounds(s->pointer, 0, array->componentType->ffiType->size);

        memcpy(array->memory->address + offset, s->pointer->address, array->componentType->ffiType->size);

    } else {
        ArrayType* arrayType;
        Data_Get_Struct(array->field->rbType, ArrayType, arrayType);

        rb_raise(rb_eArgError, "set not supported for %s", rb_obj_classname(arrayType->rbComponentType));
        return Qnil;
    }

    return rbValue;
}
Example #20
0
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
{
    AbstractMemory* ptr = MEMORY(self);
    long off, len;

    Check_Type(str, T_STRING);
    off = NUM2LONG(offset);
    len = RSTRING_LEN(str);

    checkWrite(ptr);
    checkBounds(ptr, off, len + 1);
    
    if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
        rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
        return Qnil;
    }

    memcpy(ptr->address + off, RSTRING_PTR(str), len);
    *((char *) ptr->address + off + len) = '\0';

    return self;
}
void conditionDrivenWritingFunctionObject::write()
{
    bool doWrite=false;

    switch(theState_) {
        case stateWaiting:
            doWrite=checkWrite();
            break;
        case stateWriting:
            switch(writeControlMode_) {
                case scmWriteAlways:
                    doWrite=checkStartWriting();
                    break;
                case scmWriteNTimesteps:
                    doWrite=(time().timeIndex()<=timestepForStateChange_);
                    break;
                case scmWriteIntervall:
                    doWrite=(time().value()<=timeForStateChange_);
                    break;
                case scmWriteUntilSwitch:
                    doWrite=!checkStopWriting();
                    break;
                default:
                    FatalErrorIn("conditionDrivenWritingFunctionObject::write")
                        << "Unimplemented 'writeControlMode'"
                            << endl
                            << exit(FatalError);
            }
            if(!doWrite) {
                Info << name() << ": Stopped writting. Starting cooldown" << endl;
                theState_=stateStartCooldown;
            }
            break;
        case stateCooldown:
            {
                bool stopCooldown=checkCooldown();
                if(stopCooldown) {
                    Info << name() << " cooldown ended. Writing possible" << endl;
                    theState_=stateWaiting;
                    doWrite=checkWrite();
                }
            }
            break;
        case stateStartCooldown:
            FatalErrorIn("conditionDrivenWritingFunctionObject::write")
                << "State 'startCooldown' should not be reached here"
                    << endl
                    << exit(FatalError);
        default:
            FatalErrorIn("conditionDrivenWritingFunctionObject::write")
                << "Unsupported state"
                    << endl
                    << exit(FatalError);
    }

    if(theState_==stateStartCooldown) {
        theState_=stateCooldown;
        switch(cooldownMode_) {
            case cdmNoCooldown:
                theState_=stateWaiting;
                break;
            case cdmNTimesteps:
                Info << name() << ": Waiting for " << cooldownTimesteps_
                    << " till next output" << endl;
                timestepForStateChange_=time().timeIndex()+cooldownTimesteps_;
                break;
            case cdmIntervall:
                timeForStateChange_=time().value()+cooldownIntervall_;
                Info << name() << ": Waiting till " << timeForStateChange_
                    << " till next output" << endl;
                break;
            case cdmRetrigger:
                Info << name() << ": waiting until triggered for next output"
                    << endl;
                break;
            default:
                FatalErrorIn("conditionDrivenWritingFunctionObject::write")
                    << "Unsupported cooldown mode"
                        << endl
                        << exit(FatalError);
        }
        bool stopCooldown=checkCooldown();
        if(stopCooldown) {
            Info << name() << " cooldown ended. Writing possible" << endl;
            theState_=stateWaiting;
            doWrite=checkWrite();
        }
    }

    if(doWrite) {
        writeNow();
    } else {
        // don't need to store the state if it was already written
        if(storeAndWritePreviousState_) {
            storePreviousState();
        }
    }
}
Example #22
0
void LLVOCache::writeToCache(U64 handle, const LLUUID& id, const LLVOCacheEntry::vocache_entry_map_t& cache_entry_map, BOOL dirty_cache) 
{
	llassert_always(mInitialized);

	if(mReadOnly)
	{
		return ;
	}

	HeaderEntryInfo* entry;
	handle_entry_map_t::iterator iter = mHandleEntryMap.find(handle) ;
	if(iter == mHandleEntryMap.end()) //new entry
	{		
		if(mNumEntries >= mCacheSize)
		{
			purgeEntries() ;
		}
		
		entry = new HeaderEntryInfo();
		entry->mHandle = handle ;
		entry->mTime = time(NULL) ;
		entry->mIndex = mNumEntries++ ;
		mHeaderEntryQueue.insert(entry) ;
		mHandleEntryMap[handle] = entry ;
	}
	else
	{
		entry = iter->second ;
		entry->mTime = time(NULL) ;

		//resort
		mHeaderEntryQueue.erase(entry) ;
		mHeaderEntryQueue.insert(entry) ;
	}

	//update cache header
	if(!updateEntry(entry))
	{
		return ; //update failed.
	}

	if(!dirty_cache)
	{
		return ; //nothing changed, no need to update.
	}

	//write to cache file
	std::string filename;
	getObjectCacheFilename(handle, filename);
	LLAPRFile* apr_file = new LLAPRFile(filename, APR_CREATE|APR_WRITE|APR_BINARY, mLocalAPRFilePoolp);
	
	if(!checkWrite(apr_file, (void*)id.mData, UUID_BYTES))
	{
		return ;
	}

	S32 num_entries = cache_entry_map.size() ;
	if(!checkWrite(apr_file, &num_entries, sizeof(S32)))
	{
		return ;
	}

	for (LLVOCacheEntry::vocache_entry_map_t::const_iterator iter = cache_entry_map.begin(); iter != cache_entry_map.end(); ++iter)
	{
		if(!iter->second->writeToFile(apr_file))
		{
			//failed
			delete apr_file ;
			removeCache() ;
			return ;
		}
	}

	delete apr_file ;
	return ;
}
Example #23
0
void BufWriter::writeBlock(const void* p, size_t bytes)
{
	checkWrite(bytes);
	memcpy(m_pPos, p, bytes);
	m_pPos += bytes;
}
Example #24
0
/*
 * call-seq: put(pointer, value)
 * @param [AbstractMemory] pointer pointer on a {Struct}
 * @param [String, Array] value +value+ may be a String only if array's type is a kind of +int8+
 * @return [value]
 * Set an array in a {Struct}.
 */
static VALUE
array_field_put(VALUE self, VALUE pointer, VALUE value)
{
    StructField* f;
    ArrayType* array;
    

    Data_Get_Struct(self, StructField, f);
    Data_Get_Struct(f->rbType, ArrayType, array);
    
    if (isCharArray(array) && rb_obj_is_instance_of(value, rb_cString)) {
        VALUE argv[2];

        argv[0] = INT2FIX(f->offset);
        argv[1] = value;

        rb_funcall2(pointer, rb_intern("put_string"), 2, argv);

    } else {
#ifdef notyet
        MemoryOp* op;
        int count = RARRAY_LEN(value);
        int i;
        AbstractMemory* memory = MEMORY(pointer);

        if (count > array->length) {
            rb_raise(rb_eIndexError, "array too large");
        }

        /* clear the contents in case of a short write */
        checkWrite(memory);
        checkBounds(memory, f->offset, f->type->ffiType->size);
        if (count < array->length) {
            memset(memory->address + f->offset + (count * array->componentType->ffiType->size),
                    0, (array->length - count) * array->componentType->ffiType->size);
        }

        /* now copy each element in */
        if ((op = get_memory_op(array->componentType)) != NULL) {

            for (i = 0; i < count; ++i) {
                (*op->put)(memory, f->offset + (i * array->componentType->ffiType->size), rb_ary_entry(value, i));
            }

        } else if (array->componentType->nativeType == NATIVE_STRUCT) {

            for (i = 0; i < count; ++i) {
                VALUE entry = rb_ary_entry(value, i);
                Struct* s;

                if (!rb_obj_is_kind_of(entry, rbffi_StructClass)) {
                    rb_raise(rb_eTypeError, "array element not an instance of FFI::Struct");
                    break;
                }

                Data_Get_Struct(entry, Struct, s);
                checkRead(s->pointer);
                checkBounds(s->pointer, 0, array->componentType->ffiType->size);

                memcpy(memory->address + f->offset + (i * array->componentType->ffiType->size),
                        s->pointer->address, array->componentType->ffiType->size);
            }

        } else {
            rb_raise(rb_eNotImpError, "put not supported for arrays of type %s", rb_obj_classname(array->rbComponentType));
        }
#else
        rb_raise(rb_eNotImpError, "cannot set array field");
#endif
    }

    return value;
}
Example #25
0
void InputMessage::fillBuffer(uint8 *buffer, uint16 size)
{
    checkWrite(m_readPos + size);
    memcpy(m_buffer + m_readPos, buffer, size);
    m_messageSize += size;
}