非阻塞超时重传的MySQL的UDF编程
MySQL的UDF(用户自定义函数)是一个类似的API。用户写的一组函数(UDF)根据C / C++一定规格(或C调用规范语言),然后编译成动态链接库,加载和卸载函数语句滴下降。当UDF被加载,它可以像一个内置的MySQL函数调用它,和服务器会自动加载现有的UDF在启动。复制代码代码如下所示:
# ifdef标准 / *标准的定义,不使用任何MySQL的功能。
#包括
#包括
#包括
# ifdef __win__
typedef unsigned __int64 ulonglong微软的钻头类型; / * * / 64
typedef __int64龙龙;
其他#
typedef unsigned long long ulonglong;
typedef长长的龙龙;
# endif / * * / __win__
其他#
#包括
#包括
#如果定义(mysql_server)
#包括受(strmov) / * * /
其他#
当作为独立编译时
#包括
# endif
# endif
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
# ifdef have_dlopen
my_bool http_post_init(udf_init * initid,udf_args *参数,字符串信息);
无效http_post_deinit(udf_init * initid);
龙龙http_post(udf_init * initid,udf_args *参数,is_null字符串,字符串错误);
/ *************************************************************************
*** init函数示例
**论点:
*** initid指向一个结构的初始化函数应填写。
* * *字符常量的指针;指针,函数可以使用。
**消息错误消息
**如果出错,这个函数应该返回1。在这种情况下
************************************************************************** /
my_bool http_post_init(udf_init * initid,udf_args *参数,字符串信息)
{
如果(args -> arg_count<3)
{
Strcpy(消息,错误的论点http_post;);
返回1;
}
如果(args -> arg_count = 4参数->参数{ 3 } =零!)
{
国际flexiblelength = strlen(args ->参数{ 3 });
如果(flexiblelength > 160000)
{
国际alloclength = 200 + flexiblelength;
如果(!(initid -> ptr =(char *)malloc(alloclength)))
{
Strcpy(消息,无法分配内存在http_post_init );
返回1;
}
返回0;
}
其他的
{
initid -> ptr = null;
}
}
返回0;
}
/ ****************************************************************************
*** deinit功能。这应该是由
***这个函数。
**论点:
*** initid返回值xxxx_init
**************************************************************************** /
无效http_post_deinit(udf_init * initid)
{
如果(initid!= null initid -> PTR!= null)
{
自由(initid -> PTR);
initid -> ptr = null;
}
}
/ ***************************************************************************
*自定义字符串函数。
**论点:
*** initid结构由xxx_init
*参数相同的结构来xxx_init。这种结构
**这个函数应该返回一个指向结果字符串的指针。
***通常is'result'but也可能是一个alloced字符串。
*************************************************************************** /
龙龙http_post(udf_init * initid,udf_args *参数,
char * is_null __attribute__((未使用)),
char *的错误__attribute__(((未使用的))))
{
国际sockfd = 0;
国际numbytes = 0;
int标志= 0;
int周期= 0;
char * sendbuffer = null;
fd_set WSET;
当前tval结构;
tval.tv_sec = 0;
tval.tv_usec = 300000;
如果(initid -> ptr = null)
{
焦sendarray { 160000 } = 0 ;
sendbuffer = sendarray;
}
其他的
{
sendbuffer = initid -> PTR;
}
结构sockaddr_in serv_addr;
serv_addr.sin_family = af_inet;
serv_addr.sin_port = htons(atoi(args ->参数{ 1 }));
Serv_addr.sin_addr.s_addr = inet_addr (args->args{0});
BZero((serv_addr。sin_zero),8);
如果(args -> arg_count = = 4(参数->参数{ 3 }!= NULL)
{
国际argsnum = strlen(args ->参数{ 3 });
sprintf(sendbuffer,邮政/ %s HTTP / 1.1 R内容长度:%d%s
}
其他的
{
sprintf(sendbuffer,邮政/ %s HTTP / 1.1 R
}
如果((sockfd =插座(af_inet,sock_stream,0))= = 1)
{
关闭(sockfd);
返回2;
}
标志= fcntl(sockfd,f_getfl,0);
Fcntl(sockfd,f_setfl,旗| o_nonblock); / /非阻塞
做
{
连接(sockfd,(struct sockaddr *)serv_addr,sizeof(struct sockaddr));
fd_zero(WSET);
fd_set(sockfd,WSET);
如果(选择(sockfd + 1,零,零,WSET,TVAL)<= 0周期= = 5)
{
关闭(sockfd);
返回5;
}
numbytes =发送(sockfd,sendbuffer,strlen(sendbuffer),0);
如果(numbytes<0)
{
Usleep(20000);
}
周期+ +;
}而(numbytes<0周期!= 5);
如果(numbytes<0)
{
关闭(sockfd);
返回4;
}
关闭(sockfd);
返回0;
}
如果# have_dlopen / * * /