]> git.tdb.fi Git - libs/net.git/commitdiff
Add shutdown support for client sockets
authorMikko Rasa <tdb@tdb.fi>
Thu, 22 Jan 2015 12:10:44 +0000 (14:10 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 22 Jan 2015 12:10:44 +0000 (14:10 +0200)
source/net/clientsocket.cpp
source/net/clientsocket.h
source/net/socket.cpp

index 80303de7ea9a136aa61ce54337effb80e049d934..d28d0e90b930487b4a573cb751356baf10575666 100644 (file)
@@ -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");
 
index db684f4a5d32dc99ae873379eccc58eaea2df15a..0b570cd7afa634357274d620a5b8980d22f025f9 100644 (file)
@@ -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);
index 2378207048c9a9a91395eca79227ea6088c5a56b..99a2bc41e01f3ab030204c2db9c079f0b576cb89 100644 (file)
@@ -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