飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

freeswitch插件式模块接口实现方式

时间:2022-02-11  作者:qiuzhendezhen  

概述

freeswitch的外围模块是插件式的,可以动态的加载和卸载,使用起来非常的灵活和方便。

如果我们自己来设计一个开源的代码框架,相信这种插件式的模块结构是非常适合多人合作的模式。

本文对fs的模块加载接口进行一些分析和讨论,作为借鉴。

环境

centos:CentOS  release 7.0 (Final)或以上版本

freeswitch:v1.8.7

GCC:4.8.5

模块接口

freeswitch新增mod_task模块的介绍,见之前的文章。

在mod_task模块实现中,有3个最基本的宏定义。分别对应模块的加载,卸载和定义。

SWITCH_MODULE_LOAD_FUNCTION(mod_task_load);

SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_task_shutdown);

SWITCH_MODULE_DEFINITION(mod_task, mod_task_load, mod_task_shutdown, NULL);

宏定义展开后

switch_status_t mod_task_load (switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool);

switch_status_t mod_task_shutdown (void);

static const char modname[] =  mod_task ;

switch_loadable_module_function_table_t mod_task_module_interface = {

       5,

       mod_task_load,

       mod_task_shutdown,

       NULL,

       0

};

使用nm命令查看mod_task模块符号表

[root@localhost mod]# nm -s 域名

00000000002020c8 B __bss_start

                 U bzero@@GLIBC_2.2.5

00000000002020c8 b 域名

                 w __cxa_finalize@@GLIBC_2.2.5

0000000000000b10 t deregister_tm_clones

0000000000000b80 t __do_global_dtors_aux

0000000000201cd0 t __do_global_dtors_aux_fini_array_entry

0000000000201ce0 d __dso_handle

0000000000201ce8 d _DYNAMIC

00000000002020c8 D _edata

00000000002020d0 B _end

0000000000001060 T _fini

0000000000000bc0 t frame_dummy

0000000000201cc8 t __frame_dummy_init_array_entry

00000000000014c0 r __FRAME_END__

                 U free@@GLIBC_2.2.5

00000000000012c0 r 域名5

0000000000001300 r 域名2

00000000000012e0 r 域名0

00000000000012d2 r 域名7

00000000000012a0 r 域名5

0000000000202000 d _GLOBAL_OFFSET_TABLE_

                 w __gmon_start__

000000000000132c r __GNU_EH_FRAME_HDR

00000000000009f8 T _init

                 w _ITM_deregisterTMCloneTable

                 w _ITM_registerTMCloneTable

0000000000201cd8 d __JCR_END__

0000000000201cd8 d __JCR_LIST__

                 w _Jv_RegisterClasses

0000000000001323 r modname

0000000000000c40 t mod_task_load

00000000002020a0 D mod_task_module_interface

0000000000000c00 t mod_task_shutdown

0000000000000b40 t register_tm_clones

                 U __strdup@@GLIBC_2.2.5

                 U switch_channel_export_variable_var_check

                 U switch_console_set_complete

                 U switch_core_session_get_channel

                 U switch_event_bind

                 U switch_event_get_header_idx

                 U switch_loadable_module_create_interface

                 U switch_loadable_module_create_module_interface

                 U switch_log_printf

                 U switch_separate_string

0000000000000d80 t task_api_function

0000000000000ee0 t task_app_function

0000000000000f70 t task_event_channel_hangup_complete

0000000000001010 t task_event_handler

00000000002020c8 d __TMC_END__

模块加载

freeswitch启动过程中,会根据配置文件“/usr/local/freeswitch/conf/autoload_configs/域名”的内容,顺序加载所有模块。

对某一个模块的加载过程参考“src\switch_loadable_module.c”文件中的“switch_loadable_module_load_file”函数。

函数中主要使用了“dlopen”,“dlsym”等系统接口,来打开“域名”动态库文件和查找到”mod_task_module_interface”结构数据。

再调用”mod_task_module_interface->load”函数(指向“mod_task_load”),初始化mod_task模块。

模块卸载

模块卸载的流程参考”src\switch_loadable_module.c”文件中的“do_shutdown”函数。

在全局数据中查找到mod_task模块结构体,调用“module->switch_module_shutdown”函数(指向“mod_task_shutdown”),清理模块,回收资源。

总结

freeswitch使用了系统的动态库管理接口,来动态的加载和卸载外围模块,灵活方便。

空空如常

求真得真

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。