@@ -211,7 +211,7 @@ static int connect2(my_socket s, const struct sockaddr *name, size_t namelen,
211211 poll_fd .fd = s ;
212212
213213 /* connection timeout in milliseconds */
214- res = poll (& poll_fd , 1 , ( timeout > -1 ) ? timeout * 1000 : timeout );
214+ res = poll (& poll_fd , 1 , timeout );
215215
216216 switch (res )
217217 {
@@ -248,6 +248,24 @@ static int connect2(my_socket s, const struct sockaddr *name, size_t namelen,
248248 return (0 ); /* ok */
249249}
250250
251+ static int
252+ connect_sync_or_async (MYSQL * mysql , NET * net , my_socket fd ,
253+ const struct sockaddr * name , uint namelen )
254+ {
255+ int vio_timeout = (mysql -> options .connect_timeout >= 0 ) ?
256+ mysql -> options .connect_timeout * 1000 : -1 ;
257+
258+ if (mysql -> options .extension && mysql -> options .extension -> async_context &&
259+ mysql -> options .extension -> async_context -> active )
260+ {
261+ my_bool old_mode ;
262+ vio_blocking (net -> vio , FALSE, & old_mode );
263+ return my_connect_async (mysql -> options .extension -> async_context , fd ,
264+ name , namelen , vio_timeout );
265+ }
266+
267+ return connect2 (fd , name , namelen , vio_timeout );
268+ }
251269/*
252270** Create a named pipe connection
253271*/
@@ -291,7 +309,7 @@ HANDLE create_named_pipe(NET *net, uint connect_timeout, char **arg_host,
291309 return INVALID_HANDLE_VALUE ;
292310 }
293311 /* wait for for an other instance */
294- if (! WaitNamedPipe (szPipeName , connect_timeout * 1000 ) )
312+ if (! WaitNamedPipe (szPipeName , connect_timeout ) )
295313 {
296314 net -> last_errno = CR_NAMEDPIPEWAIT_ERROR ;
297315 sprintf (net -> last_error ,ER (net -> last_errno ),host , unix_socket ,
@@ -936,7 +954,7 @@ static void mysql_read_default_options(struct st_mysql_options *options,
936954 options -> named_pipe = 1 ; /* Force named pipe */
937955 break ;
938956 case OPT_connect_timeout :
939- case OPT_timeout :
957+ case OPT_timeout :
940958 if (opt_arg )
941959 options -> connect_timeout = atoi (opt_arg );
942960 break ;
@@ -1577,10 +1595,9 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
15771595 bzero ((char * ) & UNIXaddr ,sizeof (UNIXaddr ));
15781596 UNIXaddr .sun_family = AF_UNIX ;
15791597 strmov (UNIXaddr .sun_path , unix_socket );
1580- if (connect2 ( sock ,( struct sockaddr * ) & UNIXaddr , sizeof ( UNIXaddr ) ,
1581- mysql -> options . connect_timeout ) < 0 )
1598+ if (connect_sync_or_async ( mysql , net , sock ,
1599+ ( struct sockaddr * ) & UNIXaddr , sizeof ( UNIXaddr )) )
15821600 {
1583- printf ("err\n" );
15841601 DBUG_PRINT ("error" ,("Got error %d on connect to local server" ,socket_errno ));
15851602 my_set_error (mysql , CR_CONNECTION_ERROR , SQLSTATE_UNKNOWN , ER (CR_CONNECTION_ERROR ),
15861603 unix_socket , socket_errno );
@@ -1675,17 +1692,20 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
16751692 freeaddrinfo (res );
16761693 goto error ;
16771694 }
1678- if (!(rc = connect2 (sock , save_res -> ai_addr , save_res -> ai_addrlen ,
1679- mysql -> options .connect_timeout )))
1695+ rc = connect_sync_or_async (mysql , net , sock ,
1696+ save_res -> ai_addr , save_res -> ai_addrlen );
1697+ if (!rc )
16801698 {
1681- if (socket_block (sock , 1 ) == SOCKET_ERROR )
1699+ if (mysql -> options .extension && mysql -> options .extension -> async_context &&
1700+ mysql -> options .extension -> async_context -> active )
1701+ break ;
1702+ else if (socket_block (sock , 1 ) == SOCKET_ERROR )
16821703 {
1683- closesocket (sock );
1684- continue ;
1704+ closesocket (sock );
1705+ continue ;
16851706 }
16861707 break ; /* success! */
16871708 }
1688-
16891709 vio_delete (mysql -> net .vio );
16901710 mysql -> net .vio = NULL ;
16911711 }
@@ -1708,6 +1728,13 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
17081728 }
17091729 }
17101730
1731+ /* set timeouts */
1732+ net -> vio -> read_timeout = mysql -> options .read_timeout ;
1733+ net -> vio -> write_timeout = mysql -> options .write_timeout ;
1734+
1735+ if (mysql -> options .extension && mysql -> options .extension -> async_context )
1736+ net -> vio -> async_context = mysql -> options .extension -> async_context ;
1737+
17111738 if (!net -> vio || my_net_init (net , net -> vio ))
17121739 {
17131740 vio_delete (net -> vio );
@@ -1720,16 +1747,16 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
17201747
17211748
17221749 /* set read timeout */
1723- if (mysql -> options .read_timeout )
1750+ if (mysql -> options .read_timeout >= 0 )
17241751 vio_read_timeout (net -> vio , mysql -> options .read_timeout );
17251752
17261753 /* set write timeout */
1727- if (mysql -> options .write_timeout )
1754+ if (mysql -> options .write_timeout >= 0 )
17281755 vio_write_timeout (net -> vio , mysql -> options .read_timeout );
17291756 /* Get version info */
17301757 mysql -> protocol_version = PROTOCOL_VERSION ; /* Assume this */
1731- if (mysql -> options .connect_timeout &&
1732- vio_poll_read (net -> vio , mysql -> options .connect_timeout ) )
1758+ if (mysql -> options .connect_timeout >= 0 &&
1759+ vio_wait_or_timeout (net -> vio , FALSE, mysql -> options .connect_timeout * 1000 ) < 1 )
17331760 {
17341761 my_set_error (mysql , CR_SERVER_LOST , SQLSTATE_UNKNOWN ,
17351762 ER (CR_SERVER_LOST_EXTENDED ),
0 commit comments