栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Wayland 协议定义及分发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Wayland 协议定义及分发

1. 协议定义:

Wayland核心协议是通过protocol/wayland.xml这个文件定义的。它通过wayland_scanner这个程序扫描后会生成wayland-protocol.c, wayland-client-protocol.h和wayland-server-protocol.h三个文件。wayland-client-protocol.h是给Client用的;wayland-server-protocol.h是给Server用的; wayland-protocol.c描述了接口,Client和Server都会用。下面是各接口定义

static const struct wl_interface *wayland_types[] = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	&wl_callback_interface,
	&wl_registry_interface,
	&wl_surface_interface,
	&wl_region_interface,
	&wl_buffer_interface,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	&wl_shm_pool_interface,
	NULL,
	NULL,
	&wl_data_source_interface,
	&wl_surface_interface,
	&wl_surface_interface,
	NULL,
	&wl_data_source_interface,
	NULL,
	&wl_data_offer_interface,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	&wl_data_offer_interface,
	&wl_data_offer_interface,
	&wl_data_source_interface,
	&wl_data_device_interface,
	&wl_seat_interface,
	&wl_shell_surface_interface,
	&wl_surface_interface,
	&wl_seat_interface,
	NULL,
	&wl_seat_interface,
	NULL,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	&wl_output_interface,
	&wl_seat_interface,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	NULL,
	&wl_output_interface,
	&wl_buffer_interface,
	NULL,
	NULL,
	&wl_callback_interface,
	&wl_region_interface,
	&wl_region_interface,
	&wl_output_interface,
	&wl_output_interface,
	&wl_pointer_interface,
	&wl_keyboard_interface,
	&wl_touch_interface,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	NULL,
	&wl_surface_interface,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	&wl_surface_interface,
	NULL,
	NULL,
	NULL,
	&wl_subsurface_interface,
	&wl_surface_interface,
	&wl_surface_interface,
	&wl_surface_interface,
	&wl_surface_interface,
};

2. 术语上,Wayland中把Client发给Server的跨进程函数调用称为request,反方向的跨进程函数调用称为event。本质上,它们处理的方式是类似的。要让两个进程通过socket进行函数调用,首先需要将调用抽象成数据流的形式。函数的接口部分是同时链接到Client和Server端的库中的,其中包含了对象所支持的request和event的函数签名。因此这部分不用传输,只要传输目标对象id,方法id和参数列表这些信息就可以了。这些信息会通过wl_closure_marshal()写入wl_closure结构,再由serialize_closure()变成数据流。等到了目标进程后,会从数据流通过wl_connection_demarshal()转回wl_closure。 这个过程类似于Android中的Parcel机制。那么 问题来了,参数中的整形,字符串什么的都好搞,拷贝就行。但如果参数中包含对象,我们不能把整个对象拷贝过去,也不能传引用过去。那么需要一种机制来作同一对象在Server和Client端的映射,这是通过wl_map实现的。wl_map在Client和Server端各有一个,它们分别存了wl_proxy和wl_resource的数组,且是一一对应的。这些对象在这个数组中的索引作为它们的id。这样,参数中的对象只要传id,这个id被传到目的地后会通过查找这个wl_map表来得到本地相应的对象。在功能上类似于Android中的BpXXX和BnXXX。 wl_proxy和wl_resource都包含wl_object对象。这个wl_object和面向对象语言里的对象概念类似,它有interface成员描述了这个对象所实现的接口,implementation是这些接口的实现函数的函数指针数组,id就是在wl_map结构里数组中的索引。前面所说的Client绑定Server端资源的过程就是在Client端创建wl_proxy,在Server端创建wl_resource。然后Client就可以通过wl_proxy调用Server端对应wl_resource的request,Server端就可以通过wl_resource调用Client端对应wl_proxy的event。这个映射过程如下图所示(以wl_registry为例):

wl_registry_interface 为例:

static const struct wl_message wl_registry_requests[] = {
	{ "bind", "usun", wayland_types + 0 },
};

static const struct wl_message wl_registry_events[] = {
	{ "global", "usu", wayland_types + 0 },
	{ "global_remove", "u", wayland_types + 0 },
};
WL_EXPORT const struct wl_interface wl_registry_interface = {
	"wl_registry", 1,
	1, wl_registry_requests,
	2, wl_registry_events,
};

 3.  注册接口及调用:

server 端实现接口:request:

  registry_resource = wl_resource_create(client, &wl_registry_interface, 1, id);   // 传入对应的interface
   wl_resource_set_implementation(registry_resource,                     
                       ®istry_interface,
                       display, unbind_resource); //传入interface 对应的requests实现

client 调用server 端的request 接口:

 registry = wl_proxy_marshal_constructor((struct wl_proxy *) wl_display,
             WL_DISPLAY_GET_REGISTRY, &wl_registry_interface, NULL);                           找到wl_proxy 对应的wl_resoure  对应的interface

client 端实现接口 event 接口:

wl_proxy_add_listener((struct wl_proxy *) wl_registry,   //传入对应对象
                     (void (**)(void)) listener, data);
WL_EXPORT int
wl_proxy_add_listener(struct wl_proxy *proxy,
              void (**implementation)(void), void *data)
{
    if (proxy->flags & WL_PROXY_FLAG_WRAPPER)
        wl_abort("Proxy %p is a wrappern", proxy);

    if (proxy->object.implementation || proxy->dispatcher) {
        wl_log("proxy %p already has listenern", proxy);
        return -1;
    }

    proxy->object.implementation = implementation;
    proxy->user_data = data;

    return 0;
}

server 调用client 接口:

调用如下接口

WL_EXPORT void
wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
{
	union wl_argument args[WL_CLOSURE_MAX_ARGS];
	struct wl_object *object = &resource->object;
	va_list ap;

	va_start(ap, opcode);
	wl_argument_from_va_list(object->interface->events[opcode].signature,
				 args, WL_CLOSURE_MAX_ARGS, ap);
	va_end(ap);

	wl_resource_post_event_array(resource, opcode, args);
}

4. 网络数据处理:

server 端消息处理

main()
  weston_create_listening_socket
	wl_display_add_socket
	   _wl_display_add_socket
			 wl_event_loop_add_fd(display->loop, s->fd,WL_EVENT_READABLE,socket_data, display);
				socket_data
				  wl_client_create   // 每个client 连接server调用

每个client 消息处理:
     wl_client_create
		wl_event_loop_add_fd(display->loop, fd,WL_EVENT_READABLE,wl_client_connection_data, client);
		    wl_client_connection_data
				wl_connection_read
				wl_map_lookup   // object 对应的wl_resource
				wl_closure_invoke

client  端消息处理:

连接服务				
wl_display_connect
	connect_to_socket
	wl_display_connect_to_fd
循环
wl_display_dispatch
	wl_display_dispatch_queue
		wl_display_read_events // 读event
		wl_display_dispatch_queue_pending
			dispatch_queue
				dispatch_event
					wl_closure_invoke

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918989.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号