#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 "socket.h"
#ifdef WIN32
event = CreateEvent(0, false, false, 0);
+#else
+ *event = handle;
#endif
}
#ifdef WIN32
event = CreateEvent(0, false, false, 0);
+#else
+ *event = handle;
#endif
}
#endif
}
-IO::Handle Socket::get_event_handle()
+const IO::Handle &Socket::get_event_handle()
{
-#ifdef WIN32
return event;
-#else
- return handle;
-#endif
}
int err = ::bind(handle, reinterpret_cast<sockaddr *>(&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()
{
if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
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
const SockAddr &Socket::get_local_address() const
{
if(local_addr==0)
- throw InvalidState("Local address not set");
+ throw bad_socket_state("not bound");
return *local_addr;
}
const SockAddr &Socket::get_peer_address() const
{
if(peer_addr==0)
- throw InvalidState("Peer address not set");
+ throw bad_socket_state("not connected");
return *peer_addr;
}
void Socket::check_state(bool conn) const
{
if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
- throw Exception("Socket is closed");
+ throw bad_socket_state("socket is closed");
if(conn && !connected)
- throw Exception("Socket is not connected");
+ throw bad_socket_state("not connected");
}
int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Writing to socket failed", errno);
+ throw system_error("send");
}
return ret;
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Reading from socket failed", errno);
+ throw system_error("recv");
}
else if(ret==0 && !eof_flag)
{