static MateVFSResult
flush (MateVFSSocketBuffer *socket_buffer,
       MateVFSCancellation *cancellation)
{
	Buffer *output_buffer;
	MateVFSResult result;
	MateVFSFileSize bytes_written;

	output_buffer = &socket_buffer->output_buffer;

	while (output_buffer->byte_count > 0) {
		result = mate_vfs_socket_write (socket_buffer->socket, 
						 output_buffer->data,
						 output_buffer->byte_count,
						 &bytes_written,
						 cancellation);
		output_buffer->last_error = result;

		if (result != MATE_VFS_OK) {
			return result;
		}

		memmove (output_buffer->data,
			 output_buffer->data + bytes_written,
			 output_buffer->byte_count - bytes_written);
		output_buffer->byte_count -= bytes_written;
	}

	return MATE_VFS_OK;
}
int
ne_sock_fullwrite (ne_socket *sock, const char *data, size_t count)
{
	MateVFSResult result;
	MateVFSFileSize bytes_written;
	MateVFSCancellation *cancellation;
	char *pos;

	peek_cancellation (cancellation);
	   
	pos = (char *) data;
	   
	do {
		result = mate_vfs_socket_write (sock->socket,
						 pos,
						 count,
						 &bytes_written,
						 cancellation);
		count -= bytes_written;
		pos += bytes_written;
			 
	} while (result == MATE_VFS_OK && count > 0);

	check_error (result, sock);
	   
	return 0;
}
Ejemplo n.º 3
0
int
main (int argc, char **argv)
{
	MateVFSResult        result = MATE_VFS_OK;
	gchar                 buffer[1024];
	gchar		     *host;
	gint                  port;
	MateVFSFileSize      bytes_read;
	MateVFSSSL          *ssl = NULL;
	MateVFSSocket       *socket = NULL;
	MateVFSSocketBuffer *socketbuffer = NULL;

	if (argc != 3) {
		printf ("Usage: %s <host> <port>\n", argv[0]);
		return 1;
	}

	host = argv[1];
	port = atoi (argv[2]);

	if (port <= 0) {
		printf ("Invalid port\n");
		return 1;
	}

	if (! mate_vfs_init ()) {
		fprintf (stderr, "Cannot initialize mate-vfs.\n");
		return 1;
	}

	switch (abstraction) {
		case SOCKETBUFFER:
			g_print ("Testing MateVFSSocketBuffer");
		case SOCKET:
			g_print (" and MateVFSSocket");
		case SSL:
			g_print (" and MateVFSSSL");
	}
	g_print (".\n");

	result = mate_vfs_ssl_create (&ssl, host, port, NULL);

	show_result (result, "ssl_create", host, port);

	if (ssl == NULL) {
		fprintf (stderr, "couln't connect\n");
		return -1;
	}

	if (abstraction >= SOCKET) {
		socket = mate_vfs_ssl_to_socket (ssl);
		if (socket == NULL) {
			fprintf (stderr, "couldn't create socket object\n");
			return -1;
		}

		if (abstraction == SOCKETBUFFER) {
			socketbuffer = mate_vfs_socket_buffer_new (socket);
			if (socketbuffer == NULL) {
				fprintf (stderr, 
				       "couldn't create socketbuffer object\n");
				return -1;
			}
		}
	}

	switch (abstraction) {
		case SSL:
			result = mate_vfs_ssl_write (ssl, HTTP_REQUEST, 
						      strlen(HTTP_REQUEST), &bytes_read,
						      NULL);
			break;
		case SOCKET:
			result = mate_vfs_socket_write (socket, HTTP_REQUEST, 
							 strlen(HTTP_REQUEST), &bytes_read,
							 NULL);
			break;
		case SOCKETBUFFER:
			result = mate_vfs_socket_buffer_write (socketbuffer,
								HTTP_REQUEST, strlen(HTTP_REQUEST),
								&bytes_read,
								NULL);
			mate_vfs_socket_buffer_flush (socketbuffer, NULL);
			break;
	}

	show_result (result, "write", host, port);

	while( result==MATE_VFS_OK ) {
		switch (abstraction) {
			case SSL:
				result = mate_vfs_ssl_read (ssl, buffer, 
							     sizeof buffer - 1, &bytes_read,
							     NULL);
				break;
			case SOCKET:
				result = mate_vfs_socket_read (socket, buffer, 
								sizeof buffer - 1, &bytes_read,
								NULL);
				break;
			case SOCKETBUFFER:
				result = mate_vfs_socket_buffer_read (
						socketbuffer, buffer, 
						sizeof buffer - 1, &bytes_read,
						NULL);
				break;
		}
		show_result (result, "read", host, port);
	
		buffer[bytes_read] = 0;
		write (1,buffer,bytes_read);
		if(!bytes_read) break;
	}

	switch (abstraction) {
		case SSL:
			mate_vfs_ssl_destroy (ssl, NULL);
			break;
		case SOCKET:
			mate_vfs_socket_close (socket, NULL);
			break;
		case SOCKETBUFFER:
			mate_vfs_socket_buffer_destroy (socketbuffer, TRUE, NULL);
			break;
	}

	return 0;
}