1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-08 14:02:17 +03:00

Fix for read/write timeout for sockets on non Windows platforms

This commit is contained in:
Georg Richter
2016-02-23 13:25:03 +01:00
parent d68b48f954
commit 38b78704e0
2 changed files with 37 additions and 3 deletions

View File

@@ -699,6 +699,7 @@ pvio_socket_connect_sync_or_async(MARIADB_PVIO *pvio,
my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo) my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo)
{ {
struct st_pvio_socket *csock= NULL; struct st_pvio_socket *csock= NULL;
struct timeval tm;
if (!pvio || !cinfo) if (!pvio || !cinfo)
return 1; return 1;
@@ -844,13 +845,27 @@ my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo)
if (pvio_socket_blocking(pvio, 1, 0) == SOCKET_ERROR) if (pvio_socket_blocking(pvio, 1, 0) == SOCKET_ERROR)
goto error; goto error;
} }
#ifdef _WIN32
/* apply timeouts */ /* apply timeouts */
if (pvio->timeout[PVIO_WRITE_TIMEOUT] > 0) if (pvio->timeout[PVIO_WRITE_TIMEOUT] > 0)
{
#ifndef _WIN32
tm.tv_sec= pvio->timeout[PVIO_WRITE_TIMEOUT] / 1000;
tm.tv_usec= pvio->timeout[PVIO_WRITE_TIMEOUT] % 1000;
setsockopt(csock->socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tm, sizeof(tm));
#else
setsockopt(csock->socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&pvio->timeout[PVIO_WRITE_TIMEOUT], sizeof(int)); setsockopt(csock->socket, SOL_SOCKET, SO_SNDTIMEO, (const char *)&pvio->timeout[PVIO_WRITE_TIMEOUT], sizeof(int));
if (pvio->timeout[PVIO_READ_TIMEOUT] > 0)
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&pvio->timeout[PVIO_READ_TIMEOUT], sizeof(int));
#endif #endif
}
if (pvio->timeout[PVIO_READ_TIMEOUT] > 0)
{
#ifndef _WIN32
tm.tv_sec= pvio->timeout[PVIO_READ_TIMEOUT] / 1000;
tm.tv_usec= pvio->timeout[PVIO_READ_TIMEOUT] % 1000;
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tm, sizeof(tm));
#else
setsockopt(csock->socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&pvio->timeout[PVIO_WRITE_TIMEOUT], sizeof(int));
#endif
}
return 0; return 0;
error: error:
if (pvio->data) if (pvio->data)

View File

@@ -973,6 +973,24 @@ static int test_conc117(MYSQL *mysql)
return OK; return OK;
} }
static int test_read_timeout(MYSQL *mysql)
{
int timeout= 5, rc;
MYSQL *my= mysql_init(NULL);
mysql_options(my, MYSQL_OPT_READ_TIMEOUT, &timeout);
FAIL_IF(!mysql_real_connect(my, hostname, username, password, schema,
port, socketname, 0), mysql_error(my));
rc= mysql_query(my, "SELECT SLEEP(50)");
FAIL_IF(rc == 0, "error expected");
diag("error: %s", mysql_error(my));
mysql_close(my);
return OK;
}
#ifdef HAVE_REMOTEIO #ifdef HAVE_REMOTEIO
void *remote_plugin; void *remote_plugin;
static int test_remote1(MYSQL *mysql) static int test_remote1(MYSQL *mysql)
@@ -1113,6 +1131,7 @@ static int test_zerofill(MYSQL *mysql)
} }
struct my_tests_st my_tests[] = { struct my_tests_st my_tests[] = {
{"test_read_timeout", test_read_timeout, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_zerofill", test_zerofill, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_zerofill", test_zerofill, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
#ifdef HAVE_REMOTEIO #ifdef HAVE_REMOTEIO
{"test_remote1", test_remote1, TEST_CONNECTION_NEW, 0, NULL, NULL}, {"test_remote1", test_remote1, TEST_CONNECTION_NEW, 0, NULL, NULL},