X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fsocket.cpp;h=49614df0c807bdf89f3d5772b3afb996b1bf0039;hp=6db6271a8a2710b01588a7ac9de4fa363cbe1727;hb=35009eb3f51a6fd0f9261f892a85d97d81e69886;hpb=e9b615da8c240aa3522c708951551828f5e0ed7b diff --git a/source/socket.cpp b/source/socket.cpp index 6db6271..49614df 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -144,6 +144,20 @@ void Socket::close() peer_addr=0; } +void Socket::set_timeout(const Time::TimeDelta &timeout) +{ +#ifndef WIN32 + timeval tv; + timeout.fill_timeval(tv); + set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval)); + set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval)); +#else + DWORD msecs = static_cast(timeout/Time::msec); + set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD)); + set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD)); +#endif +} + const SockAddr &Socket::get_local_address() const { if(local_addr==0) @@ -166,7 +180,16 @@ void Socket::check_state(bool conn) const throw Exception("Socket is not connected"); } -int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) +int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) +{ +#ifdef WIN32 + return setsockopt(handle, level, optname, reinterpret_cast(optval), optlen); +#else + return setsockopt(handle, level, optname, optval, optlen); +#endif +} + +int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const { #ifdef WIN32 return getsockopt(handle, level, optname, reinterpret_cast(optval), optlen);