X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fsocket.cpp;h=150d8367948c7d0ed64a3c1547d2ae485ab089dc;hb=a4f75be32827b00d9de9d8a68855fec2f5157a10;hp=6db6271a8a2710b01588a7ac9de4fa363cbe1727;hpb=861c4e9c54df7594a362013fdf6b1e048e935ae4;p=libs%2Fnet.git diff --git a/source/socket.cpp b/source/socket.cpp index 6db6271..150d836 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspnet -Copyright © 2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2008, 2010-2011 Mikkosoft Productions, Mikko Rasa Distributed under the LGPL */ @@ -49,9 +49,9 @@ Socket::Socket(SocketHandle h, const SockAddr &paddr): local_addr(0), peer_addr(paddr.copy()) { - sockaddr sa; - socklen_t size=sizeof(sockaddr); - getsockname(handle, &sa, &size); + sockaddr_storage sa; + socklen_t size=sizeof(sockaddr_storage); + getsockname(handle, reinterpret_cast(&sa), &size); local_addr=SockAddr::create(sa); #ifdef WIN32 @@ -105,10 +105,10 @@ void Socket::bind(const SockAddr &addr) { check_state(false); - sockaddr sa; + sockaddr_storage sa; unsigned size=addr.fill_sockaddr(sa); - int err=::bind(handle, &sa, size); + int err=::bind(handle, reinterpret_cast(&sa), size); if(err==-1) throw SystemError("Unable to bind", errno); @@ -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);