Beispiel #1
0
static uint64_t _pipe_stream_write( stream_t* stream, const void* source, uint64_t num )
{
	stream_pipe_t* pipestream = (stream_pipe_t*)stream;
	FOUNDATION_ASSERT( stream->type == STREAMTYPE_PIPE );
#if FOUNDATION_PLATFORM_WINDOWS
	if( pipestream->handle_write && ( ( pipestream->mode & STREAM_OUT ) != 0 ) )
	{
		uint64_t total_written = 0;
		do
		{
			unsigned long num_written = 0;
			if( !WriteFile( pipestream->handle_write, pointer_offset_const( source, total_written ), (unsigned int)( num - total_written ), &num_written, 0 ) )
				break;
			total_written += num_written;
		} while( total_written < num );
		return total_written;
	}
#elif FOUNDATION_PLATFORM_POSIX
	if( pipestream->fd_write && ( ( pipestream->mode & STREAM_OUT ) != 0 ) )
	{
		uint64_t total_written = 0;
		do
		{
			ssize_t num_written = write( pipestream->fd_write, pointer_offset_const( source, total_written ), (size_t)( num - total_written ) );
			if( num_written < 0 )
				break;
			total_written += num_written;
		} while( total_written < num );
		return total_written;
	}	
#endif

	return 0;
}
Beispiel #2
0
static uint64_t _ringbuffer_stream_write( stream_t* stream, const void* source, uint64_t num )
{
	stream_ringbuffer_t* rbstream = (stream_ringbuffer_t*)stream;
	ringbuffer_t* buffer = RINGBUFFER_FROM_STREAM( rbstream );

	unsigned int num_write = ringbuffer_write( buffer, source, (unsigned int)num );

	while( num_write < num )
	{
		rbstream->pending_write = 1;

		if( rbstream->pending_read )
			semaphore_post( &rbstream->signal_write );

		semaphore_wait( &rbstream->signal_read );
		rbstream->pending_write = 0;

		num_write += ringbuffer_write( buffer, source ? pointer_offset_const( source, num_write ) : 0, (unsigned int)( num - num_write ) );
	}

	if( rbstream->pending_read )
		semaphore_post( &rbstream->signal_write );

	return num_write;
}
Beispiel #3
0
void config_parse_commandline( const char* const* cmdline, unsigned int num )
{
	//TODO: Implement, format --section:key=value
	unsigned int arg;
	for( arg = 0; arg < num; ++arg )
	{
		if( string_match_pattern( cmdline[arg], "--*:*=*" ) )
		{
			unsigned int first_sep = string_find( cmdline[arg], ':', 0 );
			unsigned int second_sep = string_find( cmdline[arg], '=', 0 );
			if( ( first_sep != STRING_NPOS ) && ( second_sep != STRING_NPOS ) && ( first_sep < second_sep ) )
			{
				unsigned int section_length = first_sep - 2;
				unsigned int end_pos = first_sep + 1;
				unsigned int key_length = second_sep - end_pos;

				const char* section_str = cmdline[arg] + 2;
				const char* key_str = pointer_offset_const( cmdline[arg], end_pos );
				
				hash_t section = hash( section_str, section_length );
				hash_t key = hash( key_str, key_length );
				
				char* value = string_substr( cmdline[arg], second_sep + 1, STRING_NPOS );
				char* set_value = value;
				
				unsigned int value_length = string_length( value );
				
				if( !value_length )
					config_set_string( section, key, "" );
				else if( string_equal( value, "false" ) )
					config_set_bool( section, key, false );
				else if( string_equal( value, "true" ) )
					config_set_bool( section, key, true );
				else if( ( string_find( value, '.', 0 ) != STRING_NPOS ) && ( string_find_first_not_of( value, "0123456789.", 0 ) == STRING_NPOS ) && ( string_find( value, '.', string_find( value, '.', 0 ) + 1 ) == STRING_NPOS ) ) //Exactly one "."
					config_set_real( section, key, string_to_real( value ) );
				else if( string_find_first_not_of( value, "0123456789", 0 ) == STRING_NPOS )
					config_set_int( section, key, string_to_int64( value ) );
				else
				{
					if( ( value_length > 1 ) && ( value[0] == '"' ) && ( value[ value_length - 1 ] == '"' ) )
					{
						value[ value_length - 1 ] = 0;
						set_value = value + 1;
						config_set_string( section, key, set_value );
					}
					else
					{
						config_set_string( section, key, value );
					}
				}

				log_infof( HASH_CONFIG, "Config value from command line: %.*s:%.*s = %s", section_length, section_str, key_length, key_str, set_value );
				
				string_deallocate( value );
			}	
		}
	}
}
Beispiel #4
0
static size_t
_socket_stream_write(stream_t* stream, const void* buffer, size_t size) {
	socket_stream_t* sockstream;
	socket_t* sock;
	size_t was_written = 0;
	size_t remain;

	sockstream = (socket_stream_t*)stream;
	sock = sockstream->socket;
	
	if ((sock->fd == NETWORK_SOCKET_INVALID) ||
	    (sock->state != SOCKETSTATE_CONNECTED) ||
	    !size || !buffer)
		goto exit;

	remain = sockstream->buffer_out_size - sockstream->write_out;

	do {
		if (size <= remain) {
			memcpy(sockstream->buffer_out + sockstream->write_out, buffer, (size_t)size);

			sockstream->write_out += (unsigned int)size;
			was_written += size;
			size = 0;

			break;
		}

		if (remain) {
			memcpy(sockstream->buffer_out + sockstream->write_out, buffer, remain);
			buffer = pointer_offset_const(buffer, remain);

			size -= remain;
			was_written += remain;
			sockstream->write_out += remain;
		}

		_socket_stream_doflush(sockstream);

		if (sock->state != SOCKETSTATE_CONNECTED) {
			log_warnf(HASH_NETWORK, WARNING_SUSPICIOUS,
			          STRING_CONST("Socket stream (0x%" PRIfixPTR " : %d): partial write %" PRIsize " of %" PRIsize " bytes"),
			          (uintptr_t)sock, sock->fd, was_written, size);
			break;
		}

		remain = sockstream->buffer_out_size - sockstream->write_out;

	}
	while (remain);

exit:

	return was_written;
}
Beispiel #5
0
unsigned int ringbuffer_write( ringbuffer_t* buffer, const void* source, unsigned int num )
{
	unsigned int do_write;
	unsigned int max_write;
	unsigned int buffer_size;
	unsigned int offset_read;
	unsigned int offset_write;

	FOUNDATION_ASSERT( buffer );

	buffer_size = buffer->buffer_size;
	offset_read = buffer->offset_read;
	offset_write = buffer->offset_write;

	if( offset_write >= offset_read )
	{
		max_write = buffer_size - offset_write;
		if( max_write && !buffer->offset_read ) //Don't read so write aligns to read, then the entire buffer is discarded
			--max_write;
	}
	else
		max_write = offset_read - offset_write - 1; //Don't read so write aligns to read, then the entire buffer is discarded

	do_write = num;
	if( do_write > max_write )
		do_write = max_write;

	if( !do_write )
		return 0;

	if( source )
		memcpy( buffer->buffer + offset_write, source, do_write );

	offset_write += do_write;
	if( offset_write == buffer_size )
	{
		FOUNDATION_ASSERT_MSG( buffer->offset_read, "Ring buffer internal failure, discarded entire buffer" );
		offset_write = 0;
	}

	buffer->offset_write = offset_write;
	buffer->total_write += do_write;

	if( ( do_write < num ) && ( offset_write == 0 ) && ( offset_read > 0 ) )
		do_write += ringbuffer_write( buffer, pointer_offset_const( source, do_write ), num - do_write );

	return do_write;
}