• 
    

    
    

      99热精品在线国产_美女午夜性视频免费_国产精品国产高清国产av_av欧美777_自拍偷自拍亚洲精品老妇_亚洲熟女精品中文字幕_www日本黄色视频网_国产精品野战在线观看 ?

      Wayland協(xié)議分析

      2014-10-21 12:49:13劉琳
      科技視界 2014年34期
      關(guān)鍵詞:協(xié)議

      劉琳

      【摘 要】Wayland是一個新興的開源顯示通信協(xié)議,用于替代X窗口系統(tǒng)。本文以Wayland的1.3.0版本作為基礎(chǔ),通過分析源代碼,從而分析Wayland協(xié)議的具體實現(xiàn)。

      【關(guān)鍵詞】Wayland;Display;協(xié)議;Compositor

      0 引言

      Wayland是一款規(guī)定顯示合成器與其客戶端之間通信方式的協(xié)議,它最初由英特爾開源技術(shù)中心的雇員Kristian H?覬gsberg于2008年發(fā)起,用以取代X窗口系統(tǒng)。

      在本文中,顯示合成器、合成器以及服務(wù)端都是指提供顯示合成、輸出服務(wù)的一方;客戶端是指提供顯示數(shù)據(jù)的一方。

      1 模型

      Wayland源代碼編譯以后,會生成分別用于客戶端和服務(wù)端使用的動態(tài)鏈接庫libwayland-client和libwayland-server。

      圖1 客戶端和服務(wù)端的關(guān)系

      客戶端通過libwayland-client和服務(wù)端進行通信,圖形合成器通過libwayland-server和客戶端進行通信。

      libwayland-client 和libwayland-server 內(nèi)部封裝了 Wayland 通信協(xié)議。

      Wayland協(xié)議是基于面向?qū)ο笏枷攵O(shè)計的異步通信協(xié)議。

      圖2 客戶端和服務(wù)端的關(guān)系

      2 消息

      客戶端和服務(wù)端的控制信息是通過 unix domain socket 進行傳輸?shù)?。而圖形數(shù)據(jù)則通過其他方式傳輸,比如共享內(nèi)存技術(shù)。

      socket 上的數(shù)據(jù)是以消息為單位進行傳輸?shù)摹目蛻舳税l(fā)送給合成器消息叫做 request,從合成器發(fā)送給客戶端的消息有 event 和 error 兩種。

      每一個消息可以理解為要求某一個對象進行特定動作,或者某一個對象主動通知狀態(tài)發(fā)生變化。

      圖3 消息

      3 消息格式

      消息由消息頭和消息體組成。并且整個消息必須4字節(jié)對齊。

      消息頭的長度固定為8個字節(jié)。前4個字節(jié)存放的是對象ID,緊接著的2個字節(jié)是操作碼,最后2個字節(jié)存放的是整個消息的長度。

      消息體長度不固定,但都由4字節(jié)對齊的參數(shù)組成。不同的操作碼在Wayland協(xié)議里定義了不同的參數(shù)。

      4 參數(shù)格式

      消息體中的參數(shù),Wayland也定義了相關(guān)格式。

      表1 參數(shù)格式

      參數(shù)標(biāo)識是libwayland-client和libwayland-server中用于識別參數(shù)類型的標(biāo)識符號。libwayland-client和libwayland-server通過讀取協(xié)議數(shù)據(jù)常量部分的wl_message 中的signature 字符串,依次判斷字符串中的字符是某一個參數(shù)標(biāo)識,從而知道消息體中的數(shù)據(jù)類型。

      而文件描述符是特殊的參數(shù),通過socket傳送文件描述符的時候,需要將文件描述符通過socket的優(yōu)先數(shù)據(jù)區(qū)域才能發(fā)送。

      5 數(shù)據(jù)緩沖區(qū)

      無論是客戶端還是服務(wù)端,當(dāng)發(fā)送消息和接收消息的時候,都會首先將消息存放到數(shù)據(jù)緩沖區(qū)中進行緩存,借以減少IO的次數(shù)。

      數(shù)據(jù)緩沖區(qū)是一個環(huán)形緩沖,當(dāng)客戶端明確需要進行發(fā)送或者buffer滿的時候,才會進行發(fā)送動作。

      圖5 數(shù)據(jù)緩沖區(qū)

      6 連接(connection)

      連接用于描述物理通信層的信息,主要包括了輸入緩沖區(qū)和輸出緩沖區(qū)以及socket本身。

      由于文件描述符的傳輸需要使用特殊的方法,需要為文件描述符建立專用的數(shù)據(jù)緩沖區(qū),因此每一個連接包含4個數(shù)據(jù)緩沖區(qū)。2個用于輸入,2個用于輸出。

      圖6 connection

      7 主要對象

      由于Wayland 采用了面向?qū)ο蟮脑O(shè)計思想,客戶端和服務(wù)端的控制信息都是基于對象的。

      有一些對象希望所有的客戶端都可以訪問它,并且會廣播這些對象,這樣的對象叫做global。

      Wayland 環(huán)境中,有一些對象只有一個實體,而另外一些對象,是會頻繁創(chuàng)建和銷毀的。

      下面是最主要的幾個對象:

      表2 主要對象

      8 接口(interface)

      Wayland把對象提供的能力通過接口來進行描述,接口的類型就是消息的類型:request、event和error。

      在Wayland項目里面,接口是通過xml文件來描述的。

      下面是wayland.xml文件中描述 protocol、request和event的一部分摘抄,每一個部分將其描述已經(jīng)參數(shù)的類型都描述得十分清楚。

      The core global object. ?This is a special singleton object.

      It is used for internal Wayland protocol features.

      The sync request asks the server to emit the 'done' event

      on the returned wl_callback object. ?Since requests are

      handled in-order and events are delivered in-order, this can

      used as a barrier to ensure all previous requests and the

      resulting events have been handled.

      The object returned by this request will be destroyed by the

      compositor after the callback is fired and as such the client

      must not attempt to use it after that point.

      The error event is sent out when a fatal (non-recoverable)

      error has occurred. ?The object_id argument is the object

      where the error occurred, most often in response to a request

      to that object. ?The code identifies the error and is defined

      by the object interface. ?As such, each interface defines its

      own set of error codes. ?The message is an brief description

      of the error, for (debugging) convenience.

      9 代碼自動生成

      wayland-scanner是Wayland項目的一個小工具,用于將描述接口的xml文件翻譯成C語言的源文件和頭文件。

      接口的使用分為客戶端和合成器端,因此生成了2個頭文件,一個用于客戶端,一個用于服務(wù)端。

      而源文件主要包含了接口的C語言實現(xiàn),無論客戶端或者是合成器端,都是使用同一個。

      Wayland已經(jīng)實現(xiàn)了:

      客戶端、將C語言接口調(diào)用的數(shù)據(jù)做成消息并發(fā)送。

      服務(wù)端、將接收到的消息轉(zhuǎn)化成C語言的函數(shù)調(diào)用。

      因此對于開發(fā)者而言,只需要寫好接口描述xml文檔,而不用關(guān)注接口的C語言封裝。

      圖7 代碼自動生成

      下面是使用wayland-scanner將wayland.xml解析完成后生成的C語言源代碼的部分摘抄,能夠和接口部分對應(yīng)起來。

      static const struct wl_message wl_display_requests[] = {

      { "sync", "n", types + 8 },

      { "get_registry", "n", types + 9 },

      };

      static const struct wl_message wl_display_events[] = {

      { "error", "ous", types + 0 },

      { "delete_id", "u", types + 0 },

      };

      WL_EXPORT const struct wl_interface wl_display_interface = {

      "wl_display", 1,

      2, wl_display_requests,

      2, wl_display_events,

      };

      static inline struct wl_callback *

      wl_display_sync(struct wl_display *wl_display)

      {

      struct wl_proxy *callback;

      callback = wl_proxy_create((struct wl_proxy *) wl_display,

      &wl_callback_interface);

      if (!callback)

      return NULL;

      wl_proxy_marshal((struct wl_proxy *) wl_display,

      WL_DISPLAY_SYNC, callback);

      return (struct wl_callback *) callback;

      }

      struct wl_display_interface {

      /**

      * sync - asynchronous roundtrip

      * @callback: (none)

      *

      *The sync request asks the server to emit the 'done' event on

      * the returned wl_callback object. Since requests are handled

      * in-order and events are delivered in-order, this can used as a

      * barrier to ensure all previous requests and the

      * resulting events ?have been handled.

      *

      * The object returned by this request will be destroyed by the

      * compositor after the callback is fired and as such the client

      * must not attempt to use it after that point.

      */

      void (*sync)(struct wl_client *client,

      struct wl_resource *resource,

      uint32_t callback);

      /**

      * get_registry - get global registry object

      * @callback: (none)

      *

      * This request creates a registry object that allows the client

      * to list and bind the global objects available from the

      * compositor.

      */

      void (*get_registry)(struct wl_client *client,

      struct wl_resource *resource,

      uint32_t callback);

      };

      10 通信日志

      客戶端或者合成器端通過 export WAYLAND_DEBUG=1,可以將客戶端和合成器端的消息通信日志輸出到當(dāng)前終端。

      在日志中能夠看到實時的通信交互情況,有利于分析通信協(xié)議。

      下面是截取的一段通信日志, 符號"->"表示發(fā)送,沒有這個符號表示接收。@+數(shù)字表示對象ID。

      -> wl_display@1.get_registry(new id wl_registry@2)

      -> wl_display@1.sync(new id wl_callback@3)

      wl_display@1.delete_id(3)

      wl_registry@2.global(1, "wl_display", 1)

      wl_registry@2.global(2, "wl_compositor", 3)

      -> wl_registry@2.bind(2, "wl_compositor", 1, new id [unknown]@4)

      wl_registry@2.global(3, "wl_subcompositor", 1)

      wl_registry@2.global(4, "wl_scaler", 2)

      wl_registry@2.global(5, "wl_text_input_manager", 1)

      wl_registry@2.global(6, "wl_data_device_manager", 1)

      wl_registry@2.global(7, "wl_shm", 1)

      -> wl_registry@2.bind(7, "wl_shm", 1, new id [unknown]@5)

      wl_registry@2.global(8, "wl_drm", 2)

      wl_registry@2.global(9, "wl_seat", 3)

      wl_registry@2.global(10, "wl_input_method", 1)

      wl_registry@2.global(11, "wl_output", 2)

      11 結(jié)束語

      Wayland協(xié)議以其高效簡潔的設(shè)計,贏得越來越多第三方開發(fā)人員以及軟件的支持。本文通過對Wayland協(xié)議進行概要的介紹,使讀者可以更了解Wayland的具體實現(xiàn),從而添加更符合自己需求的自定義擴展協(xié)議。相信Wayland項目將來會對開源圖形系統(tǒng)產(chǎn)生更加深遠(yuǎn)的影響。

      【參考文獻(xiàn)】

      [1]http://wayland.freedesktop.org/docs/html/[OL].

      [2]http://en.wikipedia.org/wiki/Wayland_%28display_server_protocol%29[OL].

      [責(zé)任編輯:楊玉潔]

      猜你喜歡
      協(xié)議
      基于云的高校計算機機房的設(shè)計研究
      基于數(shù)字化變電站SV報文通信可靠性問題研究
      基于IATAHost—To—Host協(xié)議的GDS互聯(lián)適配器設(shè)計
      Modbus設(shè)備在機房溫度監(jiān)控系統(tǒng)中的應(yīng)用
      負(fù)面清單的管理研究
      中國市場(2016年36期)2016-10-19 04:20:43
      對無線傳感器網(wǎng)絡(luò)MAC層協(xié)議優(yōu)化的研究與設(shè)計
      科技視界(2016年22期)2016-10-18 15:25:08
      基于對等網(wǎng)協(xié)議的BotNet 防御系統(tǒng)的設(shè)計
      PKI技術(shù)在SSLVPN中的應(yīng)用
      挪用還是貪污
      《網(wǎng)絡(luò)原理》課程中協(xié)議可靠性探討
      玛多县| 留坝县| 屯昌县| 双流县| 烟台市| 洪洞县| 合肥市| 湘阴县| 弥渡县| 贵南县| 花莲市| 汾阳市| 桦川县| 苍山县| 信丰县| 黎川县| 宁都县| 彭山县| 龙陵县| 桃江县| 龙山县| 泰和县| 离岛区| 蒙自县| 开江县| 来安县| 阜新市| 栾城县| 祥云县| 巨野县| 保德县| 昭苏县| 辉县市| 陵川县| 衡山县| 宜章县| 平定县| 西华县| 大石桥市| 确山县| 武山县|