X-Git-Url: http://git.tdb.fi/?p=libs%2Fnet.git;a=blobdiff_plain;f=source%2Fnet%2Fsocket.cpp;fp=source%2Fnet%2Fsocket.cpp;h=e8e5eaf6275eddb27634a0d473377bed18570d8b;hp=031d077c44dcc194a095455d06dde3def3a62ccd;hb=88bbb4039aa274c7f41ebe3a18085b63427e5475;hpb=6f6845971a21c2d7e04b87925f5e657f0f20ca0d diff --git a/source/net/socket.cpp b/source/net/socket.cpp index 031d077..e8e5eaf 100644 --- a/source/net/socket.cpp +++ b/source/net/socket.cpp @@ -1,46 +1,10 @@ -#ifdef _WIN32 -#include -#else -#include -#include -#include -#include -#endif -#include +#include "platform_api.h" #include #include -#include -#include -#include #include "sockaddr_private.h" #include "socket.h" #include "socket_private.h" -namespace { - -#ifdef _WIN32 -class WinSockHelper -{ -public: - WinSockHelper() - { - WSADATA wsa_data; - int err = WSAStartup(0x0002, &wsa_data); - if(err) - std::cerr<<"Failed to initialize WinSock: "<handle, reinterpret_cast(&sa.addr), &sa.size); local_addr = SockAddr::new_from_sys(sa); -#ifdef _WIN32 - *priv->event = CreateEvent(0, false, false, 0); -#else - *priv->event = priv->handle; -#endif + platform_init(); } Socket::Socket(Family af, int type, int proto): @@ -71,21 +31,12 @@ Socket::Socket(Family af, int type, int proto): priv->handle = socket(family_to_sys(af), type, proto); -#ifdef _WIN32 - *priv->event = CreateEvent(0, false, false, 0); -#else - *priv->event = priv->handle; -#endif + platform_init(); } Socket::~Socket() { -#ifdef _WIN32 - closesocket(priv->handle); - CloseHandle(*priv->event); -#else - ::close(priv->handle); -#endif + platform_cleanup(); delete local_addr; delete priv; @@ -97,13 +48,7 @@ void Socket::set_block(bool b) if(b) mode = (mode|IO::M_NONBLOCK); -#ifdef _WIN32 - u_long flag = !b; - ioctlsocket(priv->handle, FIONBIO, &flag); -#else - int flags = fcntl(priv->handle, F_GETFL); - fcntl(priv->handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); -#endif + priv->set_block(b); } const IO::Handle &Socket::get_event_handle() @@ -130,36 +75,16 @@ const SockAddr &Socket::get_local_address() const return *local_addr; } -void Socket::set_timeout(const Time::TimeDelta &timeout) -{ -#ifndef _WIN32 - timeval tv = Time::rawtime_to_timeval(timeout.raw()); - priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval)); - priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval)); -#else - DWORD msecs = static_cast(timeout/Time::msec); - priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD)); - priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD)); -#endif -} - - -int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen) -{ -#ifdef _WIN32 - return setsockopt(handle, level, optname, reinterpret_cast(optval), optlen); -#else - return setsockopt(handle, level, optname, optval, optlen); -#endif -} - -int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen) +void Socket::set_socket_events(unsigned e) { -#ifdef _WIN32 - return getsockopt(handle, level, optname, reinterpret_cast(optval), optlen); -#else - return getsockopt(handle, level, optname, optval, optlen); -#endif + IO::PollEvent pe = static_cast(e&4095); + if(e&S_CONNECT) + pe = pe|IO::P_OUTPUT; + if(e&S_ACCEPT) + pe = pe|IO::P_INPUT; + + set_platform_events(e); + set_events(pe); } } // namespace Net