#ifndef WIN32
-#include <errno.h>
+#include <cerrno>
#include <fcntl.h>
#include <sys/socket.h>
#endif
#include <iostream>
-#include <msp/strings/formatter.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 {
namespace Msp {
namespace Net {
-Socket::Socket(SocketHandle h, const SockAddr &paddr):
- handle(h),
- connected(true),
- local_addr(0),
- peer_addr(paddr.copy())
+Socket::Socket(const Private &p):
+ priv(new Private),
+ local_addr(0)
{
- sockaddr_storage sa;
- socklen_t size = sizeof(sockaddr_storage);
- getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
- local_addr = SockAddr::create(sa);
+ priv->handle = p.handle;
+
+ SockAddr::SysAddr sa;
+ getsockname(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), &sa.size);
+ local_addr = SockAddr::from_sys(sa);
#ifdef WIN32
- event = CreateEvent(0, false, false, 0);
+ *priv->event = CreateEvent(0, false, false, 0);
+#else
+ *priv->event = priv->handle;
#endif
}
Socket::Socket(Family af, int type, int proto):
- connected(false),
- local_addr(0),
- peer_addr(0)
+ priv(new Private),
+ local_addr(0)
{
- handle = socket(af, type, proto);
+ priv->handle = socket(family_to_sys(af), type, proto);
#ifdef WIN32
- event = CreateEvent(0, false, false, 0);
+ *priv->event = CreateEvent(0, false, false, 0);
+#else
+ *priv->event = priv->handle;
#endif
}
Socket::~Socket()
{
- close();
+#ifdef WIN32
+ closesocket(priv->handle);
+ CloseHandle(*priv->event);
+#else
+ ::close(priv->handle);
+#endif
+
+ delete local_addr;
+ delete priv;
}
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
}
-IO::Handle Socket::get_event_handle()
+const IO::Handle &Socket::get_event_handle()
{
-#ifdef WIN32
- return event;
-#else
- return handle;
-#endif
+ return priv->event;
}
void Socket::bind(const SockAddr &addr)
{
- check_state(false);
-
- sockaddr_storage sa;
- unsigned size = addr.fill_sockaddr(sa);
+ SockAddr::SysAddr sa = addr.to_sys();
- int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
+ int err = ::bind(priv->handle, reinterpret_cast<sockaddr *>(&sa.addr), sa.size);
if(err==-1)
- throw SystemError("Unable to bind", errno);
+ throw system_error("bind");
delete local_addr;
local_addr = addr.copy();
}
-/**
-Closes the socket. Most operations on the socket will throw an exception after
-this.
-*/
-void Socket::close()
+const SockAddr &Socket::get_local_address() const
{
- if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
- return;
-
- set_events(IO::P_NONE);
-
- signal_flush_required.emit();
-#ifdef WIN32
- closesocket(handle);
- CloseHandle(event);
-#else
- ::close(handle);
-#endif
- handle = MSP_NET_INVALID_SOCKET_HANDLE;
- connected = false;
- signal_closed.emit();
-
- delete local_addr;
- local_addr = 0;
- delete peer_addr;
- peer_addr = 0;
+ if(local_addr==0)
+ throw bad_socket_state("not bound");
+ return *local_addr;
}
void Socket::set_timeout(const Time::TimeDelta &timeout)
{
#ifndef WIN32
- timeval tv;
- timeout.fill_timeval(tv);
+ timeval tv = Time::rawtime_to_timeval(timeout.raw());
set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
#else
#endif
}
-const SockAddr &Socket::get_local_address() const
-{
- if(local_addr==0)
- throw InvalidState("Local address not set");
- return *local_addr;
-}
-
-const SockAddr &Socket::get_peer_address() const
-{
- if(peer_addr==0)
- throw InvalidState("Peer address not set");
- return *peer_addr;
-}
-
-void Socket::check_state(bool conn) const
-{
- if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
- throw Exception("Socket is closed");
- if(conn && !connected)
- throw Exception("Socket is not connected");
-}
-
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);
+ return setsockopt(priv->handle, level, optname, reinterpret_cast<const char *>(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<char *>(optval), optlen);
+ return getsockopt(priv->handle, level, optname, reinterpret_cast<char *>(optval), optlen);
#else
- return getsockopt(handle, level, optname, optval, optlen);
+ return getsockopt(priv->handle, level, optname, optval, optlen);
#endif
}
-unsigned Socket::do_write(const char *buf, unsigned size)
-{
- check_state(true);
-
- if(size==0)
- return 0;
-
- int ret = ::send(handle, buf, size, 0);
- if(ret<0)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw SystemError("Writing to socket failed", errno);
- }
-
- return ret;
-}
-
-unsigned Socket::do_read(char *buf, unsigned size)
-{
- check_state(true);
-
- if(size==0)
- return 0;
-
- int ret = ::recv(handle, buf, size, 0);
- if(ret<0)
- {
- if(errno==EAGAIN)
- return 0;
- else
- throw SystemError("Reading from socket failed", errno);
- }
- else if(ret==0 && !eof_flag)
- {
- eof_flag = true;
- signal_end_of_file.emit();
- set_events(IO::P_NONE);
- }
-
- return ret;
-}
-
} // namespace Net
} // namespace Msp