local_addr = addr.copy();
}
+const SockAddr &Socket::get_local_address() const
+{
+ if(local_addr==0)
+ throw bad_socket_state("not bound");
+ return *local_addr;
+}
+
void Socket::set_timeout(const Time::TimeDelta &timeout)
{
#ifndef WIN32
#endif
}
-const SockAddr &Socket::get_local_address() const
-{
- if(local_addr==0)
- throw bad_socket_state("not bound");
- return *local_addr;
-}
-
int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
{
#ifdef WIN32
public:
~Socket();
- void set_block(bool);
- const IO::Handle &get_event_handle();
+ virtual void set_block(bool);
+ virtual const IO::Handle &get_event_handle();
/** Associates the socket with a local address. There must be no existing
users of the address. */
void bind(const SockAddr &);
- void set_timeout(const Time::TimeDelta &);
const SockAddr &get_local_address() const;
+
+ void set_timeout(const Time::TimeDelta &);
protected:
int set_option(int, int, const void *, socklen_t);
int get_option(int, int, void *, socklen_t *) const;
ClientSocket(af, SOCK_STREAM, proto)
{ }
-bool StreamSocket::poll_connect(const Time::TimeDelta &timeout)
-{
- if(!connecting)
- throw bad_socket_state("not connecting");
-
- int res = poll(*this, IO::P_OUTPUT, timeout);
- if(res==-1)
-#ifdef WIN32
- throw system_error("poll", WSAGetLastError());
-#else
- throw system_error("poll");
-#endif
- else if(res>0)
- {
- connecting = false;
-
- int err;
- socklen_t len = sizeof(int);
- get_option(SOL_SOCKET, SO_ERROR, &err, &len);
-
- if(err!=0)
- {
- set_events(IO::P_NONE);
-#ifdef WIN32
- throw system_error("connect", WSAGetLastError());
-#else
- throw system_error("connect");
-#endif
- }
-
-#ifdef WIN32
- WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
-#endif
- set_events(IO::P_INPUT);
-
- connected = true;
- }
-
- return connected;
-}
-
bool StreamSocket::connect(const SockAddr &addr)
{
if(connected)
return connected;
}
+bool StreamSocket::poll_connect(const Time::TimeDelta &timeout)
+{
+ if(!connecting)
+ throw bad_socket_state("not connecting");
+
+ int res = poll(*this, IO::P_OUTPUT, timeout);
+ if(res==-1)
+#ifdef WIN32
+ throw system_error("poll", WSAGetLastError());
+#else
+ throw system_error("poll");
+#endif
+ else if(res>0)
+ {
+ connecting = false;
+
+ int err;
+ socklen_t len = sizeof(int);
+ get_option(SOL_SOCKET, SO_ERROR, &err, &len);
+
+ if(err!=0)
+ {
+ set_events(IO::P_NONE);
+#ifdef WIN32
+ throw system_error("connect", WSAGetLastError());
+#else
+ throw system_error("connect");
+#endif
+ }
+
+#ifdef WIN32
+ WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE);
+#endif
+ set_events(IO::P_INPUT);
+
+ connected = true;
+ }
+
+ return connected;
+}
+
void StreamSocket::on_event(IO::PollEvent ev)
{
if((ev&(IO::P_OUTPUT|IO::P_ERROR)) && connecting)