]> git.tdb.fi Git - libs/net.git/blobdiff - source/net/clientsocket.cpp
Use nullptr instead of 0 for pointers
[libs/net.git] / source / net / clientsocket.cpp
index d28d0e90b930487b4a573cb751356baf10575666..89e2d082a65bddee2cdfd1f32334f2fb0f3a57bf 100644 (file)
@@ -1,9 +1,4 @@
-#ifdef WIN32
-#include <winsock2.h>
-#else
-#include <cerrno>
-#include <sys/socket.h>
-#endif
+#include "platform_api.h"
 #include <msp/core/systemerror.h>
 #include "clientsocket.h"
 #include "socket_private.h"
@@ -12,15 +7,11 @@ namespace Msp {
 namespace Net {
 
 ClientSocket::ClientSocket(Family af, int type, int proto):
-       Socket(af, type, proto),
-       connecting(false),
-       connected(false),
-       peer_addr(0)
+       Socket(af, type, proto)
 { }
 
 ClientSocket::ClientSocket(const Private &p, const SockAddr &paddr):
        Socket(p),
-       connecting(false),
        connected(true),
        peer_addr(paddr.copy())
 { }
@@ -36,7 +27,7 @@ void ClientSocket::shutdown(IO::Mode m)
 {
        int how;
        m = m&IO::M_RDWR;
-#ifdef WIN32
+#ifdef _WIN32
        if(m==IO::M_READ)
                how = SD_RECEIVE;
        else if(m==IO::M_WRITE)
@@ -60,12 +51,12 @@ void ClientSocket::shutdown(IO::Mode m)
 
 const SockAddr &ClientSocket::get_peer_address() const
 {
-       if(peer_addr==0)
+       if(!peer_addr)
                throw bad_socket_state("not connected");
        return *peer_addr;
 }
 
-unsigned ClientSocket::do_write(const char *buf, unsigned size)
+size_t ClientSocket::do_write(const char *buf, size_t size)
 {
        check_access(IO::M_WRITE);
        if(!connected)
@@ -74,19 +65,10 @@ unsigned ClientSocket::do_write(const char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       int ret = ::send(priv->handle, buf, size, 0);
-       if(ret<0)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw system_error("send");
-       }
-
-       return ret;
+       return check_sys_error(::send(priv->handle, buf, size, 0), "send");
 }
 
-unsigned ClientSocket::do_read(char *buf, unsigned size)
+size_t ClientSocket::do_read(char *buf, size_t size)
 {
        check_access(IO::M_READ);
        if(!connected)
@@ -95,19 +77,12 @@ unsigned ClientSocket::do_read(char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       int ret = ::recv(priv->handle, buf, size, 0);
-       if(ret<0)
-       {
-               if(errno==EAGAIN)
-                       return 0;
-               else
-                       throw system_error("recv");
-       }
-       else if(ret==0 && !eof_flag)
+       size_t ret = check_sys_error(::recv(priv->handle, buf, size, 0), "recv");
+       if(ret==0 && !eof_flag)
        {
                eof_flag = true;
+               set_socket_events(S_NONE);
                signal_end_of_file.emit();
-               set_events(IO::P_NONE);
        }
 
        return ret;