(1)将各函数生成so动态库
(2)主函数中调用dlopen、dlsym、dlclose等函数
例子:
#define FUNCPARA (ST_HASHTABLE*,short, unsigned char*, short*,
unsigned char*, unsigned short, unsigned char, char ,char, char,
PROTOTREE*)
typedef struct
{
unsigned int decodeid;
//唯一对应解码函数的一个标识,人为定义,不可重复
unsigned int
relay; //承载介质,在启用decodeID的情况下此项可空,一般为E1,光纤,以太网线
unsigned int protocol; //协议(大)类,一般为GSM,CDMA,TD,GPRS
unsigned int
layerproto; //协议开始层,表明协议的某个层次,数据即从该层次开始提供并解码,例如GSM的SCCP开始
char
dllname[256]; //解码函数所在动态库文件名称,全路径,例如 /root/pub/libktssp.so
char
funname[32]; //解码函数名称,一般由decodeID唯一对应
int (*fun)FUNCPARA;
void *handle;
//状态,0初始状态 1句柄未打开 2正常调用过 3调用异常需要处理
//4虽然配置了但该dll不存在 5虽然配置了但function不存在 6----其他
unsigned char status;
} DECODEPLAT; //解码函数列表
#define MAX_FUNC_NUM 1000
DECODEPLAT functions[MAX_FUNC_NUM]; //解码函数列表
//初始化函数列表数组
//读取函数列表文件,初始化数组
int loadfuncfromfile(char *fname)
{
FILE *funcfile;
char TmpStr[256],
decodeid_s[10], dllname[128], funname[64];
int
decodeid=0;
funcfile = fopen(fname, "r");
if(!funcfile)
{
printf("open function file[%s]
failed, %s!\n",fname,strerror(errno));
exit(-1);
};
fseek(funcfile, 0, SEEK_SET);
memset(functions, 0, sizeof(functions));
while(!feof(funcfile)) //配置文件里必须严格保证decodeid递增,并保证其他合法性,暂不做检查
{
void *handle=NULL;
int (*fun)FUNCPARA;
fgets(TmpStr, 256,
funcfile);
Trim(TmpStr);
memset(decodeid_s, 0,
10);
memset(dllname, 0, 128);
memset(funname, 0, 64);
LeftStrIndex(TmpStr,
decodeid_s, ',', 1);
LeftStrIndex(TmpStr, dllname,
',', 2);
LeftStrIndex(TmpStr, funname,
',', 3);
decodeid =
atoi(decodeid_s);
if (decodeid >
MAX_FUNC_NUM) break;
functions[decodeid].decodeid
= decodeid;
strcpy(functions[decodeid].dllname,
dllname);
strcpy(functions[decodeid].funname,
funname);
handle =
dlopen(functions[decodeid].dllname,RTLD_LAZY); //RTLD_NOW
if
(handle==NULL)
{
// printf("%s\n", dlerror());
functions[decodeid].status = 4;
// printf("222id %d, dll %s, fun %s %d, hand
%d\n",decodeid,
// functions[decodeid].dllname,functions[decodeid].funname,
// functions[decodeid].fun, functions[decodeid].handle);
}
else
{
functions[decodeid].handle = handle;
fun = (int (*)
FUNCPARA)dlsym(handle,functions[decodeid].funname);
if( fun==NULL)
{
functions[decodeid].status = 5;
dlclose(handle);
// printf("111id %d, dll %s, fun %s %d, hand
%d\n",decodeid,
// functions[decodeid].dllname,functions[decodeid].funname,
// functions[decodeid].fun, functions[decodeid].handle);
}
else //we can use this function
{
functions[decodeid].fun = fun;
functions[decodeid].status = 2;
// printf("id %d, dll %s, fun %s %d, hand
%d\n",decodeid,
// functions[decodeid].dllname,functions[decodeid].funname,
// functions[decodeid].fun, functions[decodeid].handle);
};
};
};
fclose(funcfile);
return 0;
};
ret =
functions[decodeid].fun(&stHashTable,datalen,
pData, &outlen, outdata,
indata->protocol,
indata->relay, 0, 0, 0, datatree);