]> git.tdb.fi Git - libs/net.git/blobdiff - source/socket.cpp
Header changes
[libs/net.git] / source / socket.cpp
index decdbeb9e2fd663c05a7e07819a30bdd0ca6e4bb..5afb9c275ffa47e599fcdb02533f79e35ab52626 100644 (file)
@@ -1,17 +1,10 @@
-/* $Id$
-
-This file is part of libmspnet
-Copyright © 2008  Mikkosoft Productions, Mikko Rasa
-Distributed under the LGPL
-*/
-
 #ifndef WIN32
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #endif
 #include <iostream>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include "socket.h"
 
@@ -24,7 +17,7 @@ public:
        WinSockHelper()
        {
                WSADATA wsa_data;
-               int err=WSAStartup(0x0002, &wsa_data);
+               int err = WSAStartup(0x0002, &wsa_data);
                if(err)
                        std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
        }
@@ -43,17 +36,50 @@ WinSockHelper wsh;
 namespace Msp {
 namespace Net {
 
+Socket::Socket(SocketHandle h, const SockAddr &paddr):
+       handle(h),
+       connected(true),
+       local_addr(0),
+       peer_addr(paddr.copy())
+{
+       sockaddr_storage sa;
+       socklen_t size = sizeof(sockaddr_storage);
+       getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
+       local_addr = SockAddr::create(sa);
+
+#ifdef WIN32
+       event = CreateEvent(0, false, false, 0);
+#endif
+}
+
+Socket::Socket(Family af, int type, int proto):
+       connected(false),
+       local_addr(0),
+       peer_addr(0)
+{
+       handle = socket(af, type, proto);
+
+#ifdef WIN32
+       event = CreateEvent(0, false, false, 0);
+#endif
+}
+
+Socket::~Socket()
+{
+       close();
+}
+
 void Socket::set_block(bool b)
 {
-       mode=(mode&~IO::M_NONBLOCK);
+       mode = (mode&~IO::M_NONBLOCK);
        if(b)
-               mode=(mode|IO::M_NONBLOCK);
+               mode = (mode|IO::M_NONBLOCK);
 
 #ifdef WIN32
-       u_long flag=!b;
+       u_long flag = !b;
        ioctlsocket(handle, FIONBIO, &flag);
 #else
-       int flags=fcntl(handle, F_GETFL);
+       int flags = fcntl(handle, F_GETFL);
        fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
 #endif
 }
@@ -72,21 +98,17 @@ void Socket::bind(const SockAddr &addr)
 {
        check_state(false);
 
-       sockaddr sa;
-       unsigned size=addr.fill_sockaddr(sa);
+       sockaddr_storage sa;
+       unsigned size = addr.fill_sockaddr(sa);
 
-       int err=::bind(handle, &sa, size);
+       int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
        if(err==-1)
-               throw Exception(format("Unable to bind: %s", strerror(errno)));
+               throw SystemError("Unable to bind", errno);
 
        delete local_addr;
-       local_addr=addr.copy();
+       local_addr = addr.copy();
 }
 
-/**
-Closes the socket.  Most operations on the socket will return an error after
-this.
-*/
 void Socket::close()
 {
        if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
@@ -101,13 +123,28 @@ void Socket::close()
 #else
        ::close(handle);
 #endif
-       handle=MSP_NET_INVALID_SOCKET_HANDLE;
+       handle = MSP_NET_INVALID_SOCKET_HANDLE;
+       connected = false;
        signal_closed.emit();
 
        delete local_addr;
-       local_addr=0;
+       local_addr = 0;
        delete peer_addr;
-       peer_addr=0;
+       peer_addr = 0;
+}
+
+void Socket::set_timeout(const Time::TimeDelta &timeout)
+{
+#ifndef WIN32
+       timeval tv;
+       timeout.fill_timeval(tv);
+       set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
+       set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
+#else
+       DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
+       set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
+       set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
+#endif
 }
 
 const SockAddr &Socket::get_local_address() const
@@ -124,39 +161,6 @@ const SockAddr &Socket::get_peer_address() const
        return *peer_addr;
 }
 
-Socket::~Socket()
-{
-       close();
-}
-
-Socket::Socket(SocketHandle h, const SockAddr &paddr):
-       handle(h),
-       connected(true),
-       local_addr(0),
-       peer_addr(paddr.copy())
-{
-       sockaddr sa;
-       socklen_t size=sizeof(sockaddr);
-       getsockname(handle, &sa, &size);
-       local_addr=SockAddr::create(sa);
-
-#ifdef WIN32
-       event=CreateEvent(0, false, false, 0);
-#endif
-}
-
-Socket::Socket(Family af, int type, int proto):
-       connected(false),
-       local_addr(0),
-       peer_addr(0)
-{
-       handle=socket(af, type, proto);
-
-#ifdef WIN32
-       event=CreateEvent(0, false, false, 0);
-#endif
-}
-
 void Socket::check_state(bool conn) const
 {
        if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
@@ -165,7 +169,16 @@ void Socket::check_state(bool conn) const
                throw Exception("Socket is not connected");
 }
 
-int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen)
+int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
+{
+#ifdef WIN32
+       return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
+#else
+       return setsockopt(handle, level, optname, optval, optlen);
+#endif
+}
+
+int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
 {
 #ifdef WIN32
        return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
@@ -181,13 +194,13 @@ unsigned Socket::do_write(const char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       int ret=::send(handle, buf, size, 0);
+       int ret = ::send(handle, buf, size, 0);
        if(ret<0)
        {
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw Exception(format("Writing to socket failed: %s", strerror(errno)));
+                       throw SystemError("Writing to socket failed", errno);
        }
 
        return ret;
@@ -200,17 +213,17 @@ unsigned Socket::do_read(char *buf, unsigned size)
        if(size==0)
                return 0;
 
-       int ret=::recv(handle, buf, size, 0);
+       int ret = ::recv(handle, buf, size, 0);
        if(ret<0)
        {
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw Exception(format("Reading from socket failed: %s", strerror(errno)));
+                       throw SystemError("Reading from socket failed", errno);
        }
        else if(ret==0 && !eof_flag)
        {
-               eof_flag=true;
+               eof_flag = true;
                signal_end_of_file.emit();
                set_events(IO::P_NONE);
        }