X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstreamsocket.cpp;h=cd810d5253e14e2990d204c4af12096f5be7278b;hb=6102d830138013216241b6723527246764103fa0;hp=cf1a7b535203a1b3b643923d0ca09443e063c5b7;hpb=f59eded7c3e162bbdfc6db424c9badc730017698;p=libs%2Fnet.git diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index cf1a7b5..cd810d5 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -1,63 +1,58 @@ -/* $Id$ - -This file is part of libmspnet -Copyright © 2008 Mikkosoft Productions, Mikko Rasa -Distributed under the LGPL -*/ - #ifndef WIN32 #include #endif -#include +#include +#include #include -#include +#include #include "streamsocket.h" namespace Msp { namespace Net { -/** -Constructs a new StreamSocket. -*/ +StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): + Socket(h, paddr), + connecting(false) +{ +#ifdef WIN32 + WSAEventSelect(handle, event, FD_READ|FD_CLOSE); +#endif + set_events(IO::P_INPUT); +} + StreamSocket::StreamSocket(Family af, int proto): Socket(af, SOCK_STREAM, proto), connecting(false) { } -/** -Checks the status of an ongoing connection attempt. If the connection fails -with an error, an exception is thrown. - -@return 0 if the connection finished, 1 if not -*/ int StreamSocket::poll_connect(const Time::TimeDelta &timeout) { check_state(false); if(!connecting) - throw InvalidState("No connection attempt going on"); + throw bad_socket_state("not connecting"); - int res=poll(*this, IO::P_OUTPUT, timeout); + int res = poll(*this, IO::P_OUTPUT, timeout); if(res==-1) #ifdef WIN32 - throw Exception(format("Connection polling failed: %d", WSAGetLastError())); + throw system_error("poll", WSAGetLastError()); #else - throw Exception(format("Connection polling failed: %s", strerror(errno))); + throw system_error("poll"); #endif else if(res>0) { - connecting=false; + connecting = false; int err; - socklen_t len=sizeof(int); + socklen_t len = sizeof(int); get_option(SOL_SOCKET, SO_ERROR, &err, &len); if(err!=0) { set_events(IO::P_NONE); #ifdef WIN32 - throw Exception(format("Connection failed: %d", err)); + throw system_error("connect", WSAGetLastError()); #else - throw Exception(format("Connection failed: %s", strerror(err))); + throw system_error("connect"); #endif } @@ -66,7 +61,7 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout) #endif set_events(IO::P_INPUT); - connected=true; + connected = true; return 0; } @@ -74,99 +69,78 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout) return 1; } -/** -Connects the socket to a remote address. In non-blocking mode, this function -may return before the connection is finished. The caller must then use either -the poll_connect function or an EventDispatcher to determine when the -connection is finished. - -@return 0 if the connection finished, 1 if it is in progress -*/ int StreamSocket::connect(const SockAddr &addr) { check_state(false); if(connected) - throw InvalidState("Socket is already connected"); + throw bad_socket_state("already connected"); - sockaddr sa; - socklen_t size=addr.fill_sockaddr(sa); + sockaddr_storage sa; + socklen_t size = addr.fill_sockaddr(sa); #ifdef WIN32 - int err=WSAConnect(handle, &sa, size, 0, 0, 0, 0); - if(err=SOCKET_ERROR) + int err = WSAConnect(handle, reinterpret_cast(&sa), size, 0, 0, 0, 0); + if(err==SOCKET_ERROR) { - int err_code=WSAGetLastError(); + int err_code = WSAGetLastError(); if(err_code==WSAEWOULDBLOCK) { - connecting=true; + connecting = true; WSAEventSelect(handle, event, FD_CONNECT); set_events(IO::P_OUTPUT); } else - throw Exception(format("Unable to connect: %d", err_code)); + throw system_error("connect", err_code); } #else - int err=::connect(handle, &sa, size); + int err = ::connect(handle, reinterpret_cast(&sa), size); if(err==-1) { if(errno==EINPROGRESS) { - connecting=true; + connecting = true; set_events(IO::P_OUTPUT); } else - throw Exception(format("Unable to connect: %s", strerror(errno))); + throw system_error("connect"); } #endif delete peer_addr; - peer_addr=addr.copy(); + peer_addr = addr.copy(); delete local_addr; - size=sizeof(sockaddr); - getsockname(handle, &sa, &size); - local_addr=SockAddr::create(sa); + size = sizeof(sockaddr_storage); + getsockname(handle, reinterpret_cast(&sa), &size); + local_addr = SockAddr::create(sa); if(err==0) { - connected=true; + connected = true; + set_events(IO::P_INPUT); signal_connect_finished.emit(0); } return (err==0)?0:1; } -/** -Used by StreamListenSocket to construct a new socket from accept. -*/ -StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): - Socket(h, paddr), - connecting(false) -{ -#ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); -#endif - set_events(IO::P_INPUT); -} - void StreamSocket::on_event(IO::PollEvent ev) { - //cout<<"StreamSocket::on_event "<