From: Mikko Rasa Date: Thu, 22 Jan 2015 12:10:44 +0000 (+0200) Subject: Add shutdown support for client sockets X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=commitdiff_plain;h=30f362b6a1257aa0a01a926de8b774fe90d4be39 Add shutdown support for client sockets --- diff --git a/source/net/clientsocket.cpp b/source/net/clientsocket.cpp index 80303de..d28d0e9 100644 --- a/source/net/clientsocket.cpp +++ b/source/net/clientsocket.cpp @@ -32,6 +32,32 @@ ClientSocket::~ClientSocket() delete peer_addr; } +void ClientSocket::shutdown(IO::Mode m) +{ + int how; + m = m&IO::M_RDWR; +#ifdef WIN32 + if(m==IO::M_READ) + how = SD_RECEIVE; + else if(m==IO::M_WRITE) + how = SD_SEND; + else if(m==IO::M_RDWR) + how = SD_BOTH; +#else + if(m==IO::M_READ) + how = SHUT_RD; + else if(m==IO::M_WRITE) + how = SHUT_WR; + else if(m==IO::M_RDWR) + how = SHUT_RDWR; +#endif + else + return; + + ::shutdown(priv->handle, how); + mode = mode&~m; +} + const SockAddr &ClientSocket::get_peer_address() const { if(peer_addr==0) @@ -41,6 +67,7 @@ const SockAddr &ClientSocket::get_peer_address() const unsigned ClientSocket::do_write(const char *buf, unsigned size) { + check_access(IO::M_WRITE); if(!connected) throw bad_socket_state("not connected"); @@ -61,6 +88,7 @@ unsigned ClientSocket::do_write(const char *buf, unsigned size) unsigned ClientSocket::do_read(char *buf, unsigned size) { + check_access(IO::M_READ); if(!connected) throw bad_socket_state("not connected"); diff --git a/source/net/clientsocket.h b/source/net/clientsocket.h index db684f4..0b570cd 100644 --- a/source/net/clientsocket.h +++ b/source/net/clientsocket.h @@ -38,6 +38,8 @@ public: bool is_connecting() const { return connecting; } bool is_connected() const { return connected; } + void shutdown(IO::Mode); + const SockAddr &get_peer_address() const; protected: virtual unsigned do_write(const char *, unsigned); diff --git a/source/net/socket.cpp b/source/net/socket.cpp index 2378207..99a2bc4 100644 --- a/source/net/socket.cpp +++ b/source/net/socket.cpp @@ -46,6 +46,8 @@ Socket::Socket(const Private &p): priv(new Private), local_addr(0) { + mode = IO::M_RDWR; + priv->handle = p.handle; SockAddr::SysAddr sa; @@ -63,6 +65,8 @@ Socket::Socket(Family af, int type, int proto): priv(new Private), local_addr(0) { + mode = IO::M_RDWR; + priv->handle = socket(family_to_sys(af), type, proto); #ifdef WIN32