Commit 283e466d authored by Matteo Quintiliani's avatar Matteo Quintiliani

Added function nmxp_setsockopt_RCVTIMEO()


git-svn-id: file:///home/quintiliani/svncopy/nmxptool/trunk@989 3cd66e75-5955-46cb-a940-c26e5fc5497d
parent a71845bf
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Istituto Nazionale di Geofisica e Vulcanologia - Italy * Istituto Nazionale di Geofisica e Vulcanologia - Italy
* quintiliani@ingv.it * quintiliani@ingv.it
* *
* $Id: nmxp_base.h,v 1.28 2008-03-11 07:28:55 mtheo Exp $ * $Id: nmxp_base.h,v 1.29 2008-03-30 06:07:47 mtheo Exp $
* *
*/ */
...@@ -64,6 +64,17 @@ int nmxp_closeSocket(int isock); ...@@ -64,6 +64,17 @@ int nmxp_closeSocket(int isock);
int nmxp_send_ctrl(int isock, void *buffer, int length); int nmxp_send_ctrl(int isock, void *buffer, int length);
/*! \brief Receives length bytes in a buffer from a socket.
*
* \param isock A descriptor referencing the socket.
* \param timeoutsec Time-out in seconds
*
* \return getsockopt() return value
*
*/
int nmxp_setsockopt_RCVTIMEO(int isock, int timeoutsec);
/*! \brief Receives length bytes in a buffer from a socket. /*! \brief Receives length bytes in a buffer from a socket.
* *
* \param isock A descriptor referencing the socket. * \param isock A descriptor referencing the socket.
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Istituto Nazionale di Geofisica e Vulcanologia - Italy * Istituto Nazionale di Geofisica e Vulcanologia - Italy
* quintiliani@ingv.it * quintiliani@ingv.it
* *
* $Id: nmxp_base.c,v 1.67 2008-03-28 13:21:24 mtheo Exp $ * $Id: nmxp_base.c,v 1.68 2008-03-30 06:07:47 mtheo Exp $
* *
*/ */
...@@ -170,59 +170,65 @@ int nmxp_recv_select_timeout(int s, char *buf, int len, int timeout) ...@@ -170,59 +170,65 @@ int nmxp_recv_select_timeout(int s, char *buf, int len, int timeout)
} }
#endif #endif
int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *recv_errno ) int nmxp_setsockopt_RCVTIMEO(int isock, int timeoutsec) {
{ int ret = 0;
int recvCount;
int cc;
char *buffer_char = buffer;
#ifdef HAVE_WINDOWS_H #ifdef HAVE_WINDOWS_H
char *recv_errno_str; int timeos;
#else #else
#ifdef HAVE_STRERROR_R
#define MAXLEN_RECV_ERRNO_STR 200 #ifndef HAVE_BROKEN_SO_RCVTIMEO
char recv_errno_str[MAXLEN_RECV_ERRNO_STR]; struct timeval timeo;
#else
char *recv_errno_str;
#endif #endif
#endif #endif
if(timeoutsec > 0) {
#ifdef HAVE_WINDOWS_H #ifdef HAVE_WINDOWS_H
int timeos; timeos = timeoutsec * 1000;
ret = setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeos, sizeof(timeos));
if (ret < 0)
{
perror("setsockopt SO_RCVTIMEO");
}
#else #else
#ifndef HAVE_BROKEN_SO_RCVTIMEO #ifndef HAVE_BROKEN_SO_RCVTIMEO
struct timeval timeo; timeo.tv_sec = timeoutsec;
timeo.tv_usec = 0;
ret = setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
if (ret < 0) {
perror("setsockopt SO_RCVTIMEO");
}
#else
#warning nmxp_setsockopt_RCVTIMEO() do nothing for your system.
#endif #endif
#endif #endif
}
return ret;
}
/* int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *recv_errno )
struct timeval timeout; {
socklen_t size_timeout = sizeof(timeout); int recvCount;
getsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, &timeout, &size_timeout); int cc;
*/ char *buffer_char = buffer;
if(timeoutsec > 0) {
#ifdef HAVE_WINDOWS_H #ifdef HAVE_WINDOWS_H
timeos = timeoutsec * 1000; char *recv_errno_str;
if (setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeos, sizeof(timeos)) < 0)
{
perror("setsockopt SO_RCVTIMEO");
}
#else #else
#ifndef HAVE_BROKEN_SO_RCVTIMEO #ifdef HAVE_STRERROR_R
timeo.tv_sec = timeoutsec; #define MAXLEN_RECV_ERRNO_STR 200
timeo.tv_usec = 0; char recv_errno_str[MAXLEN_RECV_ERRNO_STR];
if (setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)) < 0) { #else
perror("setsockopt SO_RCVTIMEO"); char *recv_errno_str;
}
#endif #endif
#endif #endif
}
nmxp_setsockopt_RCVTIMEO(isock, timeoutsec);
cc = 1; cc = 1;
*recv_errno = 0; *recv_errno = 0;
...@@ -263,35 +269,19 @@ int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *rec ...@@ -263,35 +269,19 @@ int nmxp_recv_ctrl(int isock, void *buffer, int length, int timeoutsec, int *rec
} }
} }
if(timeoutsec > 0) { nmxp_setsockopt_RCVTIMEO(isock, 0);
#ifdef HAVE_WINDOWS_H
timeos = 0;
if (setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeos, sizeof(timeos))
< 0) {
perror("setsockopt SO_RCVTIMEO");
}
#else
#ifndef HAVE_BROKEN_SO_RCVTIMEO
timeo.tv_sec = 0;
timeo.tv_usec = 0;
if (setsockopt(isock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)) < 0) {
perror("setsockopt SO_RCVTIMEO");
}
#endif
#endif
}
if (recvCount != length || *recv_errno != 0 || cc <= 0) { if (recvCount != length || *recv_errno != 0 || cc <= 0) {
#ifdef HAVE_WINDOWS_H #ifdef HAVE_WINDOWS_H
recv_errno_str = WSAGetLastErrorMessage(*recv_errno); recv_errno_str = WSAGetLastErrorMessage(*recv_errno);
#else #else
#ifdef HAVE_STRERROR_R #ifdef HAVE_STRERROR_R
strerror_r(*recv_errno, recv_errno_str, MAXLEN_RECV_ERRNO_STR); strerror_r(*recv_errno, recv_errno_str, MAXLEN_RECV_ERRNO_STR);
#else #else
recv_errno_str = strerror(*recv_errno); recv_errno_str = strerror(*recv_errno);
#endif #endif
#endif #endif
#ifdef HAVE_WINDOWS_H #ifdef HAVE_WINDOWS_H
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment