From: Mikko Rasa Date: Sat, 6 Aug 2011 09:14:36 +0000 (+0300) Subject: Hide platform details of Socket behind pimpl X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4b408e693bcb004ccaa6958610e946cfbeff9465;p=libs%2Fnet.git Hide platform details of Socket behind pimpl --- diff --git a/source/datagramsocket.cpp b/source/datagramsocket.cpp index 1368d15..7aee3f8 100644 --- a/source/datagramsocket.cpp +++ b/source/datagramsocket.cpp @@ -1,7 +1,9 @@ #include #include +#include #include #include "datagramsocket.h" +#include "socket_private.h" namespace Msp { namespace Net { @@ -10,7 +12,7 @@ DatagramSocket::DatagramSocket(Family f, int p): Socket(f, SOCK_DGRAM, p) { #ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); + WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE); #endif set_events(IO::P_INPUT); } @@ -20,7 +22,7 @@ int DatagramSocket::connect(const SockAddr &addr) sockaddr_storage sa; socklen_t size = addr.fill_sockaddr(sa); - int err = ::connect(handle, reinterpret_cast(&sa), size); + int err = ::connect(priv->handle, reinterpret_cast(&sa), size); if(err==-1) { #ifdef WIN32 @@ -35,7 +37,7 @@ int DatagramSocket::connect(const SockAddr &addr) delete local_addr; size = sizeof(sockaddr_storage); - getsockname(handle, reinterpret_cast(&sa), &size); + getsockname(priv->handle, reinterpret_cast(&sa), &size); local_addr = SockAddr::create(sa); connected = true; @@ -51,7 +53,7 @@ unsigned DatagramSocket::sendto(const char *buf, unsigned size, const SockAddr & sockaddr_storage addr; socklen_t addr_len = addr_.fill_sockaddr(addr); - int ret = ::sendto(handle, buf, size, 0, reinterpret_cast(&addr), addr_len); + int ret = ::sendto(priv->handle, buf, size, 0, reinterpret_cast(&addr), addr_len); if(ret<0) { if(errno==EAGAIN) @@ -77,7 +79,7 @@ unsigned DatagramSocket::recvfrom(char *buf, unsigned size, SockAddr *&addr_) sockaddr_storage addr; socklen_t addr_len = sizeof(sockaddr_storage); - int ret = ::recvfrom(handle, buf, size, 0, reinterpret_cast(&addr), &addr_len); + int ret = ::recvfrom(priv->handle, buf, size, 0, reinterpret_cast(&addr), &addr_len); if(ret<0) { if(errno==EAGAIN) diff --git a/source/socket.cpp b/source/socket.cpp index 7a82463..e91b684 100644 --- a/source/socket.cpp +++ b/source/socket.cpp @@ -10,6 +10,7 @@ #include #include #include "socket.h" +#include "socket_private.h" namespace { @@ -39,35 +40,38 @@ WinSockHelper wsh; namespace Msp { namespace Net { -Socket::Socket(SocketHandle h, const SockAddr &paddr): - handle(h), +Socket::Socket(const Private &p, const SockAddr &paddr): + priv(new Private), connected(true), local_addr(0), peer_addr(paddr.copy()) { + priv->handle = p.handle; + sockaddr_storage sa; socklen_t size = sizeof(sockaddr_storage); - getsockname(handle, reinterpret_cast(&sa), &size); + getsockname(priv->handle, reinterpret_cast(&sa), &size); local_addr = SockAddr::create(sa); #ifdef WIN32 - event = CreateEvent(0, false, false, 0); + *priv->event = CreateEvent(0, false, false, 0); #else - *event = handle; + *priv->event = priv->handle; #endif } Socket::Socket(Family af, int type, int proto): + priv(new Private), connected(false), local_addr(0), peer_addr(0) { - handle = socket(af, type, proto); + priv->handle = socket(af, type, proto); #ifdef WIN32 - event = CreateEvent(0, false, false, 0); + *priv->event = CreateEvent(0, false, false, 0); #else - *event = handle; + *priv->event = priv->handle; #endif } @@ -75,14 +79,15 @@ Socket::~Socket() { signal_flush_required.emit(); #ifdef WIN32 - closesocket(handle); - CloseHandle(event); + closesocket(priv->handle); + CloseHandle(*priv->event); #else - ::close(handle); + ::close(priv->handle); #endif delete local_addr; delete peer_addr; + delete priv; } void Socket::set_block(bool b) @@ -93,16 +98,16 @@ void Socket::set_block(bool b) #ifdef WIN32 u_long flag = !b; - ioctlsocket(handle, FIONBIO, &flag); + ioctlsocket(priv->handle, FIONBIO, &flag); #else - int flags = fcntl(handle, F_GETFL); - fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); + int flags = fcntl(priv->handle, F_GETFL); + fcntl(priv->handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } const IO::Handle &Socket::get_event_handle() { - return event; + return priv->event; } @@ -111,7 +116,7 @@ void Socket::bind(const SockAddr &addr) sockaddr_storage sa; unsigned size = addr.fill_sockaddr(sa); - int err = ::bind(handle, reinterpret_cast(&sa), size); + int err = ::bind(priv->handle, reinterpret_cast(&sa), size); if(err==-1) throw system_error("bind"); @@ -149,18 +154,18 @@ const SockAddr &Socket::get_peer_address() const int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { #ifdef WIN32 - return setsockopt(handle, level, optname, reinterpret_cast(optval), optlen); + return setsockopt(priv->handle, level, optname, reinterpret_cast(optval), optlen); #else - return setsockopt(handle, level, optname, optval, optlen); + return setsockopt(priv->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(optval), optlen); + return getsockopt(priv->handle, level, optname, reinterpret_cast(optval), optlen); #else - return getsockopt(handle, level, optname, optval, optlen); + return getsockopt(priv->handle, level, optname, optval, optlen); #endif } @@ -172,7 +177,7 @@ unsigned Socket::do_write(const char *buf, unsigned size) if(size==0) return 0; - int ret = ::send(handle, buf, size, 0); + int ret = ::send(priv->handle, buf, size, 0); if(ret<0) { if(errno==EAGAIN) @@ -193,7 +198,7 @@ unsigned Socket::do_read(char *buf, unsigned size) if(size==0) return 0; - int ret = ::recv(handle, buf, size, 0); + int ret = ::recv(priv->handle, buf, size, 0); if(ret<0) { if(errno==EAGAIN) diff --git a/source/socket.h b/source/socket.h index 6eb2906..861012b 100644 --- a/source/socket.h +++ b/source/socket.h @@ -5,11 +5,15 @@ #include #include "constants.h" #include "sockaddr.h" -#include "types.h" namespace Msp { namespace Net { +#ifdef WIN32 +typedef int socklen_t; +#endif + + class bad_socket_state: public std::logic_error { public: @@ -21,13 +25,14 @@ public: class Socket: public IO::EventObject { protected: - SocketHandle handle; - IO::Handle event; + struct Private; + + Private *priv; bool connected; SockAddr *local_addr; SockAddr *peer_addr; - Socket(SocketHandle, const SockAddr &); + Socket(const Private &, const SockAddr &); Socket(Family, int, int); public: ~Socket(); diff --git a/source/socket_private.h b/source/socket_private.h new file mode 100644 index 0000000..83cc369 --- /dev/null +++ b/source/socket_private.h @@ -0,0 +1,26 @@ +#ifndef MSP_NET_SOCKET_PRIVATE_H_ +#define MSP_NET_SOCKET_PRIVATE_H_ + +#include +#include "socket.h" + +namespace Msp { +namespace Net { + +struct Socket::Private +{ +#ifdef WIN32 + SOCKET handle; +#else + int handle; +#endif + + /* On POSIX platforms this is the same as the handle. This might seem + strange but it allows the same syntax on both POSIX and Windows. */ + IO::Handle event; +}; + +} // namespace Net +} // namespace Msp + +#endif diff --git a/source/streamlistensocket.cpp b/source/streamlistensocket.cpp index 693f304..3b375bc 100644 --- a/source/streamlistensocket.cpp +++ b/source/streamlistensocket.cpp @@ -1,7 +1,9 @@ #include #include #include +#include #include +#include "socket_private.h" #include "streamlistensocket.h" #include "streamsocket.h" @@ -25,12 +27,12 @@ void StreamListenSocket::listen(const SockAddr &addr, unsigned backlog) { bind(addr); - int err = ::listen(handle, backlog); + int err = ::listen(priv->handle, backlog); if(err==-1) throw system_error("listen"); #ifdef WIN32 - WSAEventSelect(handle, event, FD_ACCEPT); + WSAEventSelect(priv->handle, *priv->event, FD_ACCEPT); #endif set_events(IO::P_INPUT); @@ -44,10 +46,11 @@ StreamSocket *StreamListenSocket::accept() sockaddr_storage sa; socklen_t size = sizeof(sockaddr_storage); - SocketHandle new_h = ::accept(handle, reinterpret_cast(&sa), &size); + Private new_p; + new_p.handle = ::accept(priv->handle, reinterpret_cast(&sa), &size); RefPtr paddr = SockAddr::create(sa); - return new StreamSocket(new_h, *paddr); + return new StreamSocket(new_p, *paddr); } } // namespace Net diff --git a/source/streamsocket.cpp b/source/streamsocket.cpp index 5959084..c6cd214 100644 --- a/source/streamsocket.cpp +++ b/source/streamsocket.cpp @@ -3,19 +3,21 @@ #endif #include #include +#include #include #include +#include "socket_private.h" #include "streamsocket.h" namespace Msp { namespace Net { -StreamSocket::StreamSocket(SocketHandle h, const SockAddr &paddr): - Socket(h, paddr), +StreamSocket::StreamSocket(const Private &p, const SockAddr &paddr): + Socket(p, paddr), connecting(false) { #ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); + WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE); #endif set_events(IO::P_INPUT); } @@ -56,7 +58,7 @@ int StreamSocket::poll_connect(const Time::TimeDelta &timeout) } #ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); + WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE); #endif set_events(IO::P_INPUT); @@ -77,21 +79,21 @@ int StreamSocket::connect(const SockAddr &addr) socklen_t size = addr.fill_sockaddr(sa); #ifdef WIN32 - int err = WSAConnect(handle, reinterpret_cast(&sa), size, 0, 0, 0, 0); + int err = WSAConnect(priv->handle, reinterpret_cast(&sa), size, 0, 0, 0, 0); if(err==SOCKET_ERROR) { int err_code = WSAGetLastError(); if(err_code==WSAEWOULDBLOCK) { connecting = true; - WSAEventSelect(handle, event, FD_CONNECT); + WSAEventSelect(priv->handle, *priv->event, FD_CONNECT); set_events(IO::P_OUTPUT); } else throw system_error("connect", err_code); } #else - int err = ::connect(handle, reinterpret_cast(&sa), size); + int err = ::connect(priv->handle, reinterpret_cast(&sa), size); if(err==-1) { if(errno==EINPROGRESS) @@ -109,7 +111,7 @@ int StreamSocket::connect(const SockAddr &addr) delete local_addr; size = sizeof(sockaddr_storage); - getsockname(handle, reinterpret_cast(&sa), &size); + getsockname(priv->handle, reinterpret_cast(&sa), &size); local_addr = SockAddr::create(sa); if(err==0) @@ -141,7 +143,7 @@ void StreamSocket::on_event(IO::PollEvent ev) } #ifdef WIN32 - WSAEventSelect(handle, event, FD_READ|FD_CLOSE); + WSAEventSelect(priv->handle, *priv->event, FD_READ|FD_CLOSE); #endif set_events((err==0) ? IO::P_INPUT : IO::P_NONE); } diff --git a/source/streamsocket.h b/source/streamsocket.h index dcc5dbd..3ffe72a 100644 --- a/source/streamsocket.h +++ b/source/streamsocket.h @@ -19,7 +19,7 @@ public: private: /// Used by StreamListenSocket to construct a new socket from accept. - StreamSocket(SocketHandle, const SockAddr &); + StreamSocket(const Private &, const SockAddr &); public: StreamSocket(Family, int = 0); diff --git a/source/types.h b/source/types.h deleted file mode 100644 index 3af68ab..0000000 --- a/source/types.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef MSP_NET_TYPES_H_ -#define MSP_NET_TYPES_H_ - -namespace Msp { -namespace Net { - -#ifdef WIN32 -typedef SOCKET SocketHandle; -typedef int socklen_t; - -#define MSP_NET_INVALID_SOCKET_HANDLE static_cast(-1) -#else -typedef int SocketHandle; - -#define MSP_NET_INVALID_SOCKET_HANDLE -1 -#endif - -} // namespace Net -} // namespace Msp - -#endif