=?UTF-8?Q?Re=3A_High_CPU_usage_under_MS_Windows=E2=80=8F?=



Hi,
thanks for your reply.

I enabled G_IO_WIN32_DEBUG but it did not reveal anything special.

Please find below a sample program that exhibits the problem. Besides
glib and libsoup, you'll need an mp3 file and a player that can read
mp3 over http, WMP/iTunes/Totem will do.

/* drownedinsoup.c */
#include <glib.h>
#include <libsoup/soup.h>

#define LISTENING_PORT		80
#define SERVED_FILE		".\\sample.mp3"

#define CHUNK_SIZE	16*1024

typedef struct ChunkData {
	SoupServer *server;
	GInputStream *stream;
} ChunkData;

static void
write_next_chunk (SoupMessage *message, ChunkData *cd)
{
	gssize read_size;
	GError *error = NULL;
	gchar *chunk = g_malloc (CHUNK_SIZE);

	read_size = g_input_stream_read (cd->stream,
					 chunk,
					 CHUNK_SIZE,
					 NULL,
					 &error);
	if (read_size > 0) {
		soup_message_body_append (message->response_body,
					  SOUP_MEMORY_TAKE,
					  chunk,
					  read_size);
	} else {
		if (error != NULL) {
			g_warning ("Error reading from input stream: %s",  error->message);
			g_error_free (error);
		}
		g_free (chunk);
		soup_message_body_complete (message->response_body);
	}

	soup_server_unpause_message (cd->server, message);
}

static void
chunked_message_finished (SoupMessage *message, ChunkData *cd)
{
	g_debug ("Finished sending chunked file.");
	g_input_stream_close (cd->stream, NULL, NULL);
	g_free (cd);
}

static void
force_idle (SoupMessage *message, ChunkData *cd)
{
	Sleep(70);
}

static void
url_handler (SoupServer *server,
			       SoupMessage *message,
			       const char *path,
			       GHashTable *query,
			       SoupClientContext *context)
{
	GInputStream *stream;
	GFile *file;
	GError *error = NULL;

	ChunkData *cd = g_new (ChunkData, 1);
	cd->server = server;
	
	file = g_file_new_for_path (SERVED_FILE);
	stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));

	if (error != NULL)
	{
		g_error("Error opening mp3 file %s", SERVED_FILE);
		g_error_free (error);
	}

	cd->stream = stream;

	g_debug("Serving request...");
	
	g_signal_connect (message, "wrote_headers", G_CALLBACK (write_next_chunk), cd);
	g_signal_connect (message, "wrote_chunk", G_CALLBACK (write_next_chunk), cd);
	g_signal_connect (message, "finished", G_CALLBACK
(chunked_message_finished), cd);

	soup_message_headers_append (message->response_headers,
"Content-Type", "audio/mpeg");
	soup_message_headers_append (message->response_headers,
"Accept-Ranges", "bytes");

	soup_message_set_status (message, SOUP_STATUS_OK);

	soup_message_headers_set_encoding (message->response_headers,
SOUP_ENCODING_CHUNKED); /* Nota: The problem is the same if we use
SOUP_ENCODING_CONTENT_LENGTH */
	soup_message_headers_append (message->response_headers, "Connection", "Close");

	// ugly hack. Uncomment and see cpu usage going down. you may have to
tune the sleep time though.
	// g_signal_connect (message, "wrote-body-data", G_CALLBACK
(force_idle), NULL);
}

int main(void)
{
	SoupServer *server;

	g_type_init ();
	g_thread_init (NULL);
	
	server = soup_server_new ("port", LISTENING_PORT, NULL);
	
	soup_server_add_handler (server, "/",
				 (SoupServerCallback) url_handler,
				 NULL, NULL);	

	g_debug("Server running on port %u", LISTENING_PORT);
	
	soup_server_run(server);

	return 0;
}

/* end main.c */


--
vbe


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]