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<DWORD>(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)
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<const char *>(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<char *>(optval), optlen);
void bind(const SockAddr &);
virtual int connect(const SockAddr &) =0;
void close();
+ void set_timeout(const Time::TimeDelta &);
const SockAddr &get_local_address() const;
const SockAddr &get_peer_address() const;
protected:
void check_state(bool) const;
- int get_option(int, int, void *, socklen_t *);
+ int set_option(int, int, const void *, socklen_t);
+ int get_option(int, int, void *, socklen_t *) const;
unsigned do_write(const char *, unsigned);
unsigned do_read(char *, unsigned);
};