在unreal中,有时需要根据自已的需要添加自已的定时运行的功能程序,如果你想实现这样的功能,请按照下列步骤进行,以我添加的定时检查Mysql连接情况(因为Mysql系统默认48还是多少小时后断开一个不活动的连接,如果在这个时间内,收到连接的mysql_ping消息,则该连接不会断掉):
1,在include\h.h文件最末,添加如下定义:
2,在src\events.c 212行附件,修改为如下代码:
3,继续,在src\events.c最后,添加如下代码:
上面的代码中,会每三分钟检查一次mysql连接是否正常,如果不正常,则重新连接。关于mysql的操作及代码,请参考前面的文章。
1,在include\h.h文件最末,添加如下定义:
extern EVENT(check_mysql_conn);
2,在src\events.c 212行附件,修改为如下代码:
/* Start events */
//add by oldhawk
EventAddEx(NULL, "check_mysql_conn", 600, 0, check_mysql_conn, NULL); //这里的600表是3分钟执行一次
//add end
EventAddEx(NULL, "tunefile", 300, 0, save_tunefile, NULL);
EventAddEx(NULL, "garbage", GARBAGE_COLLECT_EVERY, 0, garbage_collect, NULL);
//add by oldhawk
EventAddEx(NULL, "check_mysql_conn", 600, 0, check_mysql_conn, NULL); //这里的600表是3分钟执行一次
//add end
EventAddEx(NULL, "tunefile", 300, 0, save_tunefile, NULL);
EventAddEx(NULL, "garbage", GARBAGE_COLLECT_EVERY, 0, garbage_collect, NULL);
3,继续,在src\events.c最后,添加如下代码:
//add by oldhawk
EVENT(check_mysql_conn)
{
if(strcmp(MYSQL_HOST,"0")){
//ircd_log(LOG_SERVER,"in event check_mysql_conn");
if(!my_conn){ //数据库未连接,可能是首次起动
ircd_log(LOG_CLIENT,"Begin to connect to Mysql");
my_conn=my_mysql_connect();
if(!my_conn){
sendto_ops("Mysql 数据库服务器出现故障,请通知管理员排除!");
ircd_log(LOG_SERVER,"Mysql Connect : ERROR!");
return;
}else{
mysql_query(my_conn,"SET NAMES 'gb2312'");
ircd_log(LOG_SERVER,"Mysql Connect : OK!");
}
}else{ //数据库已连接,则测试激活
if(my_mysql_ping(my_conn)!=0){
ircd_log(LOG_SERVER,"Mysql Ping : mysql ping error");
my_conn=my_mysql_connect();
if(!my_conn){
sendto_ops("Mysql 数据库服务器出现Ping故障,请通知管理员排除!");
ircd_log(LOG_SERVER,"Mysql Ping reConnect : ERROR!");
return;
}else{
mysql_query(my_conn,"SET NAMES 'gb2312'");
ircd_log(LOG_SERVER,"Mysql Ping reConnect : OK!");
}
}else{
ircd_log(LOG_SERVER,"Mysql Ping: OK!");
}
}
}
}
//add end
EVENT(check_mysql_conn)
{
if(strcmp(MYSQL_HOST,"0")){
//ircd_log(LOG_SERVER,"in event check_mysql_conn");
if(!my_conn){ //数据库未连接,可能是首次起动
ircd_log(LOG_CLIENT,"Begin to connect to Mysql");
my_conn=my_mysql_connect();
if(!my_conn){
sendto_ops("Mysql 数据库服务器出现故障,请通知管理员排除!");
ircd_log(LOG_SERVER,"Mysql Connect : ERROR!");
return;
}else{
mysql_query(my_conn,"SET NAMES 'gb2312'");
ircd_log(LOG_SERVER,"Mysql Connect : OK!");
}
}else{ //数据库已连接,则测试激活
if(my_mysql_ping(my_conn)!=0){
ircd_log(LOG_SERVER,"Mysql Ping : mysql ping error");
my_conn=my_mysql_connect();
if(!my_conn){
sendto_ops("Mysql 数据库服务器出现Ping故障,请通知管理员排除!");
ircd_log(LOG_SERVER,"Mysql Ping reConnect : ERROR!");
return;
}else{
mysql_query(my_conn,"SET NAMES 'gb2312'");
ircd_log(LOG_SERVER,"Mysql Ping reConnect : OK!");
}
}else{
ircd_log(LOG_SERVER,"Mysql Ping: OK!");
}
}
}
}
//add end
上面的代码中,会每三分钟检查一次mysql连接是否正常,如果不正常,则重新连接。关于mysql的操作及代码,请参考前面的文章。