X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fsocket.cpp;h=c845471a58ed13d20608b4768e13879389370016;hb=6102d830138013216241b6723527246764103fa0;hp=a4675f593b6bc95ed9e0d4ad40f442e5bcf78ee1;hpb=12b93b4f1655bf9c26f73e67cf78b86eeb1eaa97;p=libs%2Fnet.git diff --git a/source/socket.cpp b/source/socket.cpp index a4675f5..c845471 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -4,6 +4,8 @@ #include #endif #include +#include +#include #include #include #include @@ -50,6 +52,8 @@ Socket::Socket(SocketHandle h, const SockAddr &paddr): #ifdef WIN32 event = CreateEvent(0, false, false, 0); +#else + *event = handle; #endif } @@ -62,6 +66,8 @@ Socket::Socket(Family af, int type, int proto): #ifdef WIN32 event = CreateEvent(0, false, false, 0); +#else + *event = handle; #endif } @@ -85,13 +91,9 @@ void Socket::set_block(bool b) #endif } -IO::Handle Socket::get_event_handle() +const IO::Handle &Socket::get_event_handle() { -#ifdef WIN32 return event; -#else - return handle; -#endif } @@ -104,7 +106,7 @@ void Socket::bind(const SockAddr &addr) int err = ::bind(handle, reinterpret_cast(&sa), size); if(err==-1) - throw SystemError("Unable to bind", errno); + throw system_error("bind"); delete local_addr; local_addr = addr.copy(); @@ -150,23 +152,23 @@ void Socket::set_timeout(const Time::TimeDelta &timeout) const SockAddr &Socket::get_local_address() const { if(local_addr==0) - throw InvalidState("Local address not set"); + throw bad_socket_state("not bound"); return *local_addr; } const SockAddr &Socket::get_peer_address() const { if(peer_addr==0) - throw InvalidState("Peer address not set"); + throw bad_socket_state("not connected"); return *peer_addr; } void Socket::check_state(bool conn) const { if(handle==MSP_NET_INVALID_SOCKET_HANDLE) - throw Exception("Socket is closed"); + throw bad_socket_state("socket is closed"); if(conn && !connected) - throw Exception("Socket is not connected"); + throw bad_socket_state("not connected"); } int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) @@ -200,7 +202,7 @@ unsigned Socket::do_write(const char *buf, unsigned size) if(errno==EAGAIN) return 0; else - throw SystemError("Writing to socket failed", errno); + throw system_error("send"); } return ret; @@ -219,7 +221,7 @@ unsigned Socket::do_read(char *buf, unsigned size) if(errno==EAGAIN) return 0; else - throw SystemError("Reading from socket failed", errno); + throw system_error("recv"); } else if(ret==0 && !eof_flag) {