Non-blocking timeout retransmission _mysql for Mysql UDF programming

Source: Internet
Author: User
Tags mysql functions sprintf

The MySQL UDF (user Defined Function) is similar to an API that allows users to write a set of functions (UDF) in C + + (or a language using the C call specification) based on certain specifications, and then compile it into a dynamic link library, using the drop Function statement to load and unload the UDF. The UDF can be loaded and invoked as a built-in function of MySQL, and the server automatically loads the UDF that originally existed when it started.

Copy Code code as follows:

#ifdef standard/* STANDARD is defined, don ' t use any MySQL functions * *
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __INT64 ulonglong; /* Microsofts bit types * *
typedef __int64 Longlong;
#else
typedef unsigned long long ulonglong;
typedef long Long Longlong;
#endif/*__win__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined (mysql_server)
#include <m_string.h>/* to get Strmov () * *
#else
/* When compiled as standalone * *
#include <string.h>
#endif
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys time.h= "" >
#include <sys ioctl.h= "" >
#include <sys types.h= "" >
#include <netinet in.h= "" >
#include <sys socket.h= "" >
#include <sys wait.h= "" >
#include <arpa inet.h= "" >
#include <unistd.h>
#include <mysql.h>
#include <ctype.h>
#ifdef Have_dlopen

My_bool Http_post_init (Udf_init *initid, Udf_args *args, Char *message);
void Http_post_deinit (Udf_init *initid);
Longlong http_post (Udf_init *initid, Udf_args *args, char *is_null,char *error);
/*************************************************************************
* * Example of init function
* * Arguments:
* * Initid Points to a structure the INIT function should fill.
* * Char *ptr; A pointer that's function can use.
* * Message Error message
**return This function should return 1 if something goes wrong. In the case
**************************************************************************/
My_bool Http_post_init (Udf_init *initid, Udf_args *args, Char *message)
{
if (Args->arg_count < 3)
{
strcpy (Message, "wrong arguments to http_post; ");
return 1;
}

if (Args->arg_count = = 4 && args->args[3]!=null)
{
int flexiblelength = strlen (args->args[3]);
if (Flexiblelength > 160000)
{
int alloclength = + flexiblelength;
if (!) ( Initid->ptr= (char*) malloc (alloclength))
{
strcpy (Message, "couldn ' t allocate memory in Http_post_init");
return 1;
}
return 0;
}
Else
{
initid->ptr=null;
}

}
return 0;

}

/****************************************************************************
* * Deinit function. This should any allocated by
* * This function.
* * Arguments:
* * Initid return value from Xxxx_init
****************************************************************************/
void Http_post_deinit (Udf_init *initid)
{
if (initid!=null && initid->ptr!=null)
{
Free (INITID->PTR);
Initid->ptr = NULL;
}

}

/***************************************************************************
* * UDF string function.
* * Arguments:
* * Initid Structure filled by xxx_init
* * Args the same structure as to xxx_init. This structure
* * This function should return a pointer to the result string.
* * Normally this are ' result ' but may also to be a alloced string.
***************************************************************************/
Longlong http_post (Udf_init *initid, Udf_args *args,
Char *is_null __attribute__ ((unused)),
Char *error __attribute__ ((unused))
{
int sockfd=0;
int numbytes=0;
int flags=0;
int cycletimes=0;
char* Sendbuffer=null;

Fd_set Wset;
struct Timeval tval;
tval.tv_sec = 0;
Tval.tv_usec = 300000;

if (initid->ptr = NULL)
{
Char sendarray[160000] = "n";
Sendbuffer=sendarray;
}
Else
{
Sendbuffer = initid->ptr;
}

struct sockaddr_in serv_addr;
serv_addr.sin_family = af_inet;
Serv_addr.sin_port = htons (atoi (args->args[1));
SERV_ADDR.SIN_ADDR.S_ADDR = inet_addr (Args->args[0]);
Bzero (& (Serv_addr.sin_zero), 8);

if (Args->arg_count = 4 && (args->args[3]!=null))
{
int argsnum = strlen (args->args[3]);
sprintf (Sendbuffer, "POST/?%s http/1.1\r\ncontent-length:%d\r\n\r\n%s", Args->args[2],argsnum,args->args[3] );
}
Else
{
sprintf (Sendbuffer, "POST/?%s http/1.1\r\n", args->args[2]);
}

if ((SOCKFD = socket (af_inet,sock_stream,0)) = = 1)
{
Close (SOCKFD);
return 2;
}
Flags = FCNTL (sockfd,f_getfl,0);
Fcntl (sockfd,f_setfl,flags| O_nonblock);//Set to Non-blocking
Todo
{
Connect (SOCKFD, (struct sockaddr *) &serv_addr, sizeof (struct sockaddr));
Fd_zero (&wset);
Fd_set (Sockfd,&wset);
if (select (Sockfd+1, NULL, &wset, Null,&tval) <= 0 && cycletimes==5)
{
Close (SOCKFD);
return 5;
}
numbytes = Send (Sockfd,sendbuffer,strlen (Sendbuffer), 0);
if (numbytes<0)
{
Usleep (20000);
}
cycletimes++;
}while (numbytes<0 && cycletimes!=5);
if (numbytes<0)
{
Close (SOCKFD);
return 4;
}
Close (SOCKFD);
return 0;
}
#endif/* Have_dlopen * *
</ctype.h></mysql.h></unistd.h></arpa></sys></sys></netinet></sys ></sys></sys></fcntl.h></unistd.h></netdb.h></errno.h></stdlib.h> </m_string.h></m_ctype.h></mysql.h></string.h></m_string.h></my_sys.h>< /my_global.h></string.h></stdio.h></stdlib.h>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.