• 中国计算机学会会刊
  • 中国科技核心期刊
  • 中文核心期刊

J4 ›› 2016, Vol. 38 ›› Issue (03): 437-442.

• 论文 • 上一篇    下一篇

一种轻量级的对象通信机制设计与实现

曾颖超,王茂芝,冯伟桓   

  1. (成都理工大学数学地质四川省重点实验室,四川 成都 610059)
  • 收稿日期:2015-04-07 修回日期:2015-05-26 出版日期:2016-03-25 发布日期:2016-03-25

Design and implementation of a lightweight
object communication mechanism  

ZENG Yingchao,WANG Maozhi,FENG Weihuan   

  1. (Geomathematics Key Laboratory of Sichuan Province,Chengdu University of Technology,Chengdu 610059,China)
  • Received:2015-04-07 Revised:2015-05-26 Online:2016-03-25 Published:2016-03-25

摘要:

针对对象间跨线程或跨DLL通信时容易增加对象之间耦合度的现象,借鉴Action Script 3.0语言中事件模型以及PC机总线通信的思想,提出一种基于C++11的轻量级对象间通信机制。在设计上,该机制采用事件作为通信载体,事件可以在总线上传输形成事件流,同时也能被设置了相应类型监听器的对象捕捉。对象(或DLL)实质上是在和总线进行交互,交互接口是用户预定义好的一些事件,从而有效降低对象之间的耦合度。在实现上,该机制定义了一个两层的体系结构——运输层和适配层。让适配层(顶层)的各个DLL模块都以隐式链接方式加载运输层提供的DLL,该DLL主要任务是导出一个包含一个静态对象队列、一个静态事件队列和一条总线的类,这样适配层中实例化了此导出类的对象就可以通过接口成员函数方便、安全地访问到运输层的总线,从而完成对象间的交互。本文中使用的方法对编写大型软件系统的基础通信架构具有参考价值。

关键词: 通信机制;跨DLL通信;软总线;跨线程通信;低耦合

Abstract:

When objects communicate with others across different DLLs or threads, the system coupling degree can be easily increased. In order to solve this problem, using the idea of Event model in Action Script 3.0 and the computer bus communication for reference, we design a lightweight object communication mechanism based on C++11. This lightweight object communication mechanism uses events as the message carrier, which can not only be transmitted and forms an event stream on the bus, but also can be caught by the objects with corresponding listener types. Objects or DLLs essentially interact with the bus, and the interface between the two is predefined events, thus the coupling between objects can be effectively reduced. This mechanism builds a twolayer architecture: the transport layer and the adaptation layer. The DLL belonging to the transport layer will load the DLL provided by the adaptation layer through implicit linking. The DLL provided by the adaptation layer contains a static object queue, a static event queue and a bus, thus all the individual objects in the adaptation layer can easily and safely complete the interaction between objects by calling its own member functions. 

Key words: communication mechanism;crossDLL communication;software bus;crossthread communication;loose coupling