FreeSWITCH模块加载过程

一、模块接口 

FreeSWITCH的大部分功能是通过加载外部的模块来实现的。模块通过一个数据结构向外部暴露了自己的接口。该数据结构定义如下:

typedef struct switch_loadable_module_function_table {

       int switch_api_version;

       switch_module_load_t load;

       switch_module_shutdown_t shutdown;

       switch_module_runtime_t runtime;

       switch_module_flag_t flags;

} switch_loadable_module_function_table_t;

    模块被成功加载后,会搜索这个数据结构的变量,从其中获取到导出的函数。这个变量名由如下方式组成:

          模块名 + _module_interface


二、sofia模块

     以mod_sofia为例描述模块的加载过程。根据规则mod_sofia模块的接口变量名为:mod_sofia_module_interface。在动态加载mod_sofia.so库后,查找mod_sofia_module_interface变量,其中的load函数指针必须指向一个有效的函数,否则加载模块将失败。随后,调用load函数,对于mod_sofia模块来说,最终调用的是mod_sofia.c中的mod_sofia_load函数。

       在成功调用函数mod_sofia_load后,将获取到一个类型为switch_loadable_module_interface_t的变量module_interface,该数据类型定义如下:

struct switch_loadable_module_interface 

{

const char *module_name;

switch_endpoint_interface_t *endpoint_interface;

switch_timer_interface_t *timer_interface;

switch_dialplan_interface_t *dialplan_interface;

switch_codec_interface_t *codec_interface;

switch_application_interface_t *application_interface;

switch_chat_application_interface_t *chat_application_interface;

switch_api_interface_t *api_interface;

switch_json_api_interface_t *json_api_interface;

switch_file_interface_t *file_interface;

switch_speech_interface_t *speech_interface;

switch_directory_interface_t *directory_interface;

switch_chat_interface_t *chat_interface;

switch_say_interface_t *say_interface;

switch_asr_interface_t *asr_interface;

switch_management_interface_t *management_interface;

switch_limit_interface_t *limit_interface;

switch_thread_rwlock_t *rwlock;

int refs;

switch_memory_pool_t *pool;

};

    动态库mod_sofia.so加载成功后,会把最终的结果整理为一个结构,供上层使用,该数据结构定义如下:

struct switch_loadable_module {

char *key;

char *filename;

int perm;

switch_loadable_module_interface_t *module_interface;

switch_dso_lib_t lib;

switch_module_load_t switch_module_load;

switch_module_runtime_t switch_module_runtime;

switch_module_shutdown_t switch_module_shutdown;

switch_memory_pool_t *pool;

switch_status_t status;

switch_thread_t *thread;

switch_bool_t shutting_down;

};


      其中最重要的成员是来自变量mod_sofia_module_interface的三个函数switch_module_load、switch_module_runtime、switch_module_shutdown,以及通过load函数获取的接口module_interface。

       mod_sofia模块中只导出了mod_sofia_load、 mod_sofia_shutdown两个函数,switch_module_runtime未实现。在mod_sofia.c中,使用宏SWITCH_MODULE_DEFINITION定义了结构变量mod_sofia_module_interface供so文件导出

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,918评论 0 38
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,788评论 0 9
  • 模块通常是指编程语言所提供的代码组织机制,利用此机制可将程序拆解为独立且通用的代码单元。所谓模块化主要是解决代码分...
    MapleLeafFall阅读 1,194评论 0 0
  • 我啊,以前就是太傻,当初为了男朋友放弃一切离开生活了七八年的城市来到了深圳,却被抛弃了,那时候以泪洗面,一个朋友也...
    月牙儿的耳环阅读 349评论 0 0
  • 今儿老子罢工!每日一文很重要,有些事比每日一文更重要。 抽烟喝酒吃肉,陪伴倾听感受。
    Pingzi就是瓶子阅读 279评论 2 1