Split out connection io buffer logic.

dev
Kristian Høgsberg 16 years ago
parent c5089878cb
commit 680f1c7025
  1. 2
      Makefile
  2. 193
      connection.c
  3. 21
      connection.h
  4. 273
      wayland.c

@ -3,7 +3,7 @@ LDLIBS += $(shell pkg-config --libs libffi libdrm)
all : wayland client all : wayland client
wayland_objs = wayland.o event-loop.o hash.o compositor.o wayland_objs = wayland.o event-loop.o connection.o hash.o compositor.o
wayland : $(wayland_objs) wayland : $(wayland_objs)
gcc -o $@ $(wayland_objs) $(LDLIBS) gcc -o $@ $(wayland_objs) $(LDLIBS)

@ -0,0 +1,193 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/uio.h>
#include "connection.h"
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
struct wl_buffer {
char data[4096];
int head, tail;
};
struct wl_connection {
struct wl_buffer in, out;
int fd;
void *data;
wl_connection_update_func_t update;
};
struct wl_connection *
wl_connection_create(int fd,
wl_connection_update_func_t update,
void *data)
{
struct wl_connection *connection;
connection = malloc(sizeof *connection);
memset(connection, 0, sizeof *connection);
connection->fd = fd;
connection->update = update;
connection->data = data;
connection->update(connection,
WL_CONNECTION_READABLE,
connection->data);
return connection;
}
void
wl_connection_destroy(struct wl_connection *connection)
{
free(connection);
}
void
wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
{
struct wl_buffer *b;
int tail, rest;
b = &connection->in;
tail = b->tail;
if (tail + size <= ARRAY_LENGTH(b->data)) {
memcpy(data, b->data + tail, size);
} else {
rest = ARRAY_LENGTH(b->data) - tail;
memcpy(data, b->data + tail, rest);
memcpy(data + rest, b->data, size - rest);
}
}
void
wl_connection_consume(struct wl_connection *connection, size_t size)
{
struct wl_buffer *b;
int tail, rest;
b = &connection->in;
tail = b->tail;
if (tail + size <= ARRAY_LENGTH(b->data)) {
b->tail += size;
} else {
rest = ARRAY_LENGTH(b->data) - tail;
b->tail = size - rest;
}
}
int wl_connection_data(struct wl_connection *connection, uint32_t mask)
{
struct wl_buffer *b;
struct iovec iov[2];
int len, head, tail, count, size, available;
if (mask & WL_CONNECTION_READABLE) {
b = &connection->in;
head = connection->in.head;
if (head < b->tail) {
iov[0].iov_base = b->data + head;
iov[0].iov_len = b->tail - head;
count = 1;
} else {
size = ARRAY_LENGTH(b->data) - head;
iov[0].iov_base = b->data + head;
iov[0].iov_len = size;
iov[1].iov_base = b->data;
iov[1].iov_len = b->tail;
count = 2;
}
len = readv(connection->fd, iov, count);
if (len < 0) {
fprintf(stderr,
"read error from connection %p: %m (%d)\n",
connection, errno);
return -1;
} else if (len == 0) {
/* FIXME: Handle this better? */
return -1;
} else if (head + len <= ARRAY_LENGTH(b->data)) {
b->head += len;
} else {
b->head = head + len - ARRAY_LENGTH(b->data);
}
/* We know we have data in the buffer at this point,
* so if head equals tail, it means the buffer is
* full. */
available = b->head - b->tail;
if (available == 0)
available = sizeof b->data;
else if (available < 0)
available += ARRAY_LENGTH(b->data);
} else {
available = 0;
}
if (mask & WL_CONNECTION_WRITABLE) {
b = &connection->out;
tail = b->tail;
if (tail < b->head) {
iov[0].iov_base = b->data + tail;
iov[0].iov_len = b->head - tail;
count = 1;
} else {
size = ARRAY_LENGTH(b->data) - tail;
iov[0].iov_base = b->data + tail;
iov[0].iov_len = size;
iov[1].iov_base = b->data;
iov[1].iov_len = b->head;
count = 2;
}
len = writev(connection->fd, iov, count);
if (len < 0) {
fprintf(stderr, "write error for connection %p: %m\n", connection);
return -1;
} else if (tail + len <= ARRAY_LENGTH(b->data)) {
b->tail += len;
} else {
b->tail = tail + len - ARRAY_LENGTH(b->data);
}
/* We just took data out of the buffer, so at this
* point if head equals tail, the buffer is empty. */
if (b->tail == b->head)
connection->update(connection,
WL_CONNECTION_READABLE,
connection->data);
}
return available;
}
void
wl_connection_write(struct wl_connection *connection, const void *data, size_t count)
{
struct wl_buffer *b;
size_t size;
int head;
b = &connection->out;
head = b->head;
if (head + count <= ARRAY_LENGTH(b->data)) {
memcpy(b->data + head, data, count);
b->head += count;
} else {
size = ARRAY_LENGTH(b->data) - head;
memcpy(b->data + head, data, size);
memcpy(b->data, data + size, count - size);
b->head = count - size;
}
if (b->tail == head)
connection->update(connection,
WL_CONNECTION_READABLE |
WL_CONNECTION_WRITABLE,
connection->data);
}

@ -0,0 +1,21 @@
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
struct wl_connection;
#define WL_CONNECTION_READABLE 0x01
#define WL_CONNECTION_WRITABLE 0x02
typedef int (*wl_connection_update_func_t)(struct wl_connection *connection,
uint32_t mask, void *data);
struct wl_connection *wl_connection_create(int fd,
wl_connection_update_func_t update,
void *data);
void wl_connection_destroy(struct wl_connection *connection);
void wl_connection_copy(struct wl_connection *connection, void *data, size_t size);
void wl_connection_consume(struct wl_connection *connection, size_t size);
int wl_connection_data(struct wl_connection *connection, uint32_t mask);
void wl_connection_write(struct wl_connection *connection, const void *data, size_t count);
#endif

@ -7,22 +7,18 @@
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <sys/uio.h>
#include <ffi.h> #include <ffi.h>
#include "wayland.h" #include "wayland.h"
#include "connection.h"
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
struct wl_region { struct wl_region {
}; };
struct wl_buffer {
char data[4096];
int head, tail;
};
struct wl_client { struct wl_client {
struct wl_buffer in, out; struct wl_connection *connection;
struct wl_event_source *source; struct wl_event_source *source;
struct wl_display *display; struct wl_display *display;
}; };
@ -156,83 +152,8 @@ wl_surface_get_data(struct wl_surface *surface)
return surface->compositor_data; return surface->compositor_data;
} }
static void
wl_client_data(int fd, uint32_t mask, void *data);
static void
wl_client_write(struct wl_client *client, const void *data, size_t count);
static void
advertise_object(struct wl_client *client, struct wl_object *object)
{
const struct wl_interface *interface;
static const char pad[4];
uint32_t length, p[2];
interface = object->interface;
length = strlen(interface->name);
p[0] = object->id;
p[1] = length;
wl_client_write(client, p, sizeof p);
wl_client_write(client, interface->name, length);
wl_client_write(client, pad, -length & 3);
}
struct wl_client *
wl_client_create(struct wl_display *display, int fd)
{
struct wl_client *client;
client = malloc(sizeof *client);
if (client == NULL)
return NULL;
memset(client, 0, sizeof *client);
client->display = display;
client->source = wl_event_loop_add_fd(display->loop, fd,
WL_EVENT_READABLE,
wl_client_data, client);
advertise_object(client, &display->base);
return client;
}
void void
wl_client_destroy(struct wl_client *client) wl_client_destroy(struct wl_client *client);
{
printf("disconnect from client %p\n", client);
wl_event_loop_remove_source(client->display->loop, client->source);
free(client);
}
static void
wl_client_copy(struct wl_client *client, void *data, size_t size)
{
int tail, rest;
tail = client->in.tail;
if (tail + size <= ARRAY_LENGTH(client->in.data)) {
memcpy(data, client->in.data + tail, size);
} else {
rest = ARRAY_LENGTH(client->in.data) - tail;
memcpy(data, client->in.data + tail, rest);
memcpy(data + rest, client->in.data, size - rest);
}
}
static void
wl_client_consume(struct wl_client *client, size_t size)
{
int tail, rest;
tail = client->in.tail;
if (tail + size <= ARRAY_LENGTH(client->in.data)) {
client->in.tail += size;
} else {
rest = ARRAY_LENGTH(client->in.data) - tail;
client->in.tail = size - rest;
}
}
static void static void
wl_client_demarshal(struct wl_client *client, struct wl_object *target, wl_client_demarshal(struct wl_client *client, struct wl_object *target,
@ -270,7 +191,7 @@ wl_client_demarshal(struct wl_client *client, struct wl_object *target,
values[1].object = target; values[1].object = target;
args[1] = &values[1]; args[1] = &values[1];
wl_client_copy(client, data, size); wl_connection_copy(client->connection, data, size);
p = &data[2]; p = &data[2];
for (i = 0; i < method->argument_count; i++) { for (i = 0; i < method->argument_count; i++) {
switch (method->arguments[i].type) { switch (method->arguments[i].type) {
@ -314,31 +235,6 @@ wl_client_demarshal(struct wl_client *client, struct wl_object *target,
ffi_call(&cif, FFI_FN(method->func), &result, args); ffi_call(&cif, FFI_FN(method->func), &result, args);
} }
static void
wl_client_write(struct wl_client *client, const void *data, size_t count)
{
size_t size;
int head;
head = client->out.head;
if (head + count <= ARRAY_LENGTH(client->out.data)) {
memcpy(client->out.data + head, data, count);
client->out.head += count;
} else {
size = ARRAY_LENGTH(client->out.data) - head;
memcpy(client->out.data + head, data, size);
memcpy(client->out.data, data + size, count - size);
client->out.head = count - size;
}
if (client->out.tail == head)
wl_event_loop_update_source(client->display->loop,
client->source,
WL_EVENT_READABLE |
WL_EVENT_WRITEABLE);
}
static void static void
wl_client_event(struct wl_client *client, struct wl_object *object, uint32_t event) wl_client_event(struct wl_client *client, struct wl_object *object, uint32_t event)
{ {
@ -349,31 +245,39 @@ wl_client_event(struct wl_client *client, struct wl_object *object, uint32_t eve
p[0] = object->id; p[0] = object->id;
p[1] = event | (8 << 16); p[1] = event | (8 << 16);
wl_client_write(client, p, sizeof p); wl_connection_write(client->connection, p, sizeof p);
} }
#define WL_DISPLAY_INVALID_OBJECT 0 #define WL_DISPLAY_INVALID_OBJECT 0
#define WL_DISPLAY_INVALID_METHOD 1 #define WL_DISPLAY_INVALID_METHOD 1
static void static void
wl_client_process_input(struct wl_client *client) wl_client_connection_data(int fd, uint32_t mask, void *data)
{ {
struct wl_client *client = data;
struct wl_connection *connection = client->connection;
const struct wl_method *method; const struct wl_method *method;
struct wl_object *object; struct wl_object *object;
uint32_t p[2], opcode, size, available; uint32_t p[2], opcode, size;
uint32_t cmask = 0;
int len;
/* We know we have data in the buffer at this point, so if if (mask & WL_EVENT_READABLE)
* head equals tail, it means the buffer is full. */ cmask |= WL_CONNECTION_READABLE;
if (mask & WL_EVENT_WRITEABLE)
cmask |= WL_CONNECTION_WRITABLE;
available = client->in.head - client->in.tail; len = wl_connection_data(connection, cmask);
if (available <= 0) if (len < 0) {
available = sizeof client->in.data; wl_client_destroy(client);
return;
}
while (available > sizeof p) { while (len > sizeof p) {
wl_client_copy(client, p, sizeof p); wl_connection_copy(connection, p, sizeof p);
opcode = p[1] & 0xffff; opcode = p[1] & 0xffff;
size = p[1] >> 16; size = p[1] >> 16;
if (available < size) if (len < size)
break; break;
object = wl_hash_lookup(&client->display->objects, object = wl_hash_lookup(&client->display->objects,
@ -381,99 +285,88 @@ wl_client_process_input(struct wl_client *client)
if (object == NULL) { if (object == NULL) {
wl_client_event(client, &client->display->base, wl_client_event(client, &client->display->base,
WL_DISPLAY_INVALID_OBJECT); WL_DISPLAY_INVALID_OBJECT);
wl_client_consume(client, size); wl_connection_consume(connection, size);
available -= size; len -= size;
continue; continue;
} }
if (opcode >= object->interface->method_count) { if (opcode >= object->interface->method_count) {
wl_client_event(client, &client->display->base, wl_client_event(client, &client->display->base,
WL_DISPLAY_INVALID_METHOD); WL_DISPLAY_INVALID_METHOD);
wl_client_consume(client, size); wl_connection_consume(connection, size);
available -= size; len -= size;
continue; continue;
} }
method = &object->interface->methods[opcode]; method = &object->interface->methods[opcode];
wl_client_demarshal(client, object, method, size); wl_client_demarshal(client, object, method, size);
wl_client_consume(client, size); wl_connection_consume(connection, size);
available -= size; len -= size;
} }
} }
static void static int
wl_client_data(int fd, uint32_t mask, void *data) wl_client_connection_update(struct wl_connection *connection,
uint32_t mask, void *data)
{ {
struct wl_client *client = data; struct wl_client *client = data;
struct iovec iov[2]; uint32_t emask = 0;
int len, head, tail, count, size;
if (mask & WL_EVENT_READABLE) {
head = client->in.head;
if (head < client->in.tail) {
iov[0].iov_base = client->in.data + head;
iov[0].iov_len = client->in.tail - head;
count = 1;
} else {
size = ARRAY_LENGTH(client->out.data) - head;
iov[0].iov_base = client->in.data + head;
iov[0].iov_len = size;
iov[1].iov_base = client->in.data;
iov[1].iov_len = client->in.tail;
count = 2;
}
len = readv(fd, iov, count);
if (len < 0) {
fprintf(stderr,
"read error from client %p: %m (%d)\n",
client, errno);
wl_client_destroy(client);
} else if (len == 0) {
wl_client_destroy(client);
} else if (head + len <= ARRAY_LENGTH(client->in.data)) {
client->in.head += len;
} else {
client->in.head =
head + len - ARRAY_LENGTH(client->in.data);
}
if (client->in.head != client->in.tail) if (mask & WL_CONNECTION_READABLE)
wl_client_process_input(client); emask |= WL_EVENT_READABLE;
} if (mask & WL_CONNECTION_WRITABLE)
emask |= WL_EVENT_WRITEABLE;
if (mask & WL_EVENT_WRITEABLE) { return wl_event_loop_update_source(client->display->loop,
tail = client->out.tail; client->source, mask);
if (tail < client->out.head) {
iov[0].iov_base = client->out.data + tail;
iov[0].iov_len = client->out.head - tail;
count = 1;
} else {
size = ARRAY_LENGTH(client->out.data) - tail;
iov[0].iov_base = client->out.data + tail;
iov[0].iov_len = size;
iov[1].iov_base = client->out.data;
iov[1].iov_len = client->out.head;
count = 2;
} }
len = writev(fd, iov, count);
if (len < 0) { static void
fprintf(stderr, "write error for client %p: %m\n", client); advertise_object(struct wl_client *client, struct wl_object *object)
wl_client_destroy(client); {
} else if (tail + len <= ARRAY_LENGTH(client->out.data)) { const struct wl_interface *interface;
client->out.tail += len; static const char pad[4];
} else { uint32_t length, p[2];
client->out.tail =
tail + len - ARRAY_LENGTH(client->out.data); interface = object->interface;
length = strlen(interface->name);
p[0] = object->id;
p[1] = length;
wl_connection_write(client->connection, p, sizeof p);
wl_connection_write(client->connection, interface->name, length);
wl_connection_write(client->connection, pad, -length & 3);
} }
/* We just took data out of the buffer, so at this struct wl_client *
* point if head equals tail, the buffer is empty. */ wl_client_create(struct wl_display *display, int fd)
{
struct wl_client *client;
client = malloc(sizeof *client);
if (client == NULL)
return NULL;
if (client->out.tail == client->out.head) memset(client, 0, sizeof *client);
wl_event_loop_update_source(client->display->loop, client->display = display;
client->source, client->source = wl_event_loop_add_fd(display->loop, fd,
WL_EVENT_READABLE); WL_EVENT_READABLE,
wl_client_connection_data, client);
client->connection = wl_connection_create(fd,
wl_client_connection_update,
client);
advertise_object(client, &display->base);
return client;
} }
void
wl_client_destroy(struct wl_client *client)
{
printf("disconnect from client %p\n", client);
wl_event_loop_remove_source(client->display->loop, client->source);
wl_connection_destroy(client->connection);
free(client);
} }
static int static int

Loading…
Cancel
Save