From: Mikko Rasa Date: Sat, 6 Aug 2011 20:30:00 +0000 (+0300) Subject: Move some functions around a bit X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=commitdiff_plain;h=6489612c788dd57721fc181ff26a981b35668d1e Move some functions around a bit --- diff --git a/source/socket.cpp b/source/socket.cpp index b5a6ec8..2bfdecc 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -117,6 +117,13 @@ void Socket::bind(const SockAddr &addr) 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 @@ -130,13 +137,6 @@ void Socket::set_timeout(const Time::TimeDelta &timeout) #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 diff --git a/source/socket.h b/source/socket.h index 38d65da..f975884 100644 --- a/source/socket.h +++ b/source/socket.h @@ -35,15 +35,16 @@ protected: 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; diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index 3fa6b9a..b06cf69 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -26,47 +26,6 @@ StreamSocket::StreamSocket(Family af, int proto): 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) @@ -120,6 +79,47 @@ bool StreamSocket::connect(const SockAddr &addr) 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)