-#ifndef WIN32
-#include <cerrno>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#endif
-#include <iostream>
+#include "platform_api.h"
#include <msp/core/systemerror.h>
#include <msp/io/handle_private.h>
-#include <msp/strings/format.h>
-#include <msp/time/rawtime_private.h>
-#include <msp/time/units.h>
#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: "<<err<<'\n';
- }
-
- ~WinSockHelper()
- {
- WSACleanup();
- }
-};
-
-WinSockHelper wsh;
-#endif
-
-}
+using namespace std;
namespace Msp {
namespace Net {
getsockname(priv->handle, reinterpret_cast<sockaddr *>(&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):
{
mode = IO::M_RDWR;
+ // TODO use SOCK_CLOEXEC on Linux
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;
void Socket::set_block(bool b)
{
- mode = (mode&~IO::M_NONBLOCK);
- 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
+ IO::adjust_mode(mode, IO::M_NONBLOCK, !b);
+ priv->set_block(b);
+}
+
+void Socket::set_inherit(bool i)
+{
+ IO::adjust_mode(mode, IO::M_INHERIT, i);
+ priv->set_inherit(i);
+}
+
+const IO::Handle &Socket::get_handle(IO::Mode)
+{
+ // TODO could this be implemented somehow?
+ throw unsupported("Socket::get_handle");
}
const IO::Handle &Socket::get_event_handle()
return priv->event;
}
-
void Socket::bind(const SockAddr &addr)
{
SockAddr::SysAddr sa = addr.to_sys();
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<DWORD>(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<const char *>(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<char *>(optval), optlen);
-#else
- return getsockopt(handle, level, optname, optval, optlen);
-#endif
+ IO::PollEvent pe = static_cast<IO::PollEvent>(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