1#include "SimpleSocket.h"
2#include "SockAddress.h"
3#include "TempFailure.h"
20 m_message += strerror(m_errorcode);
24SimpleSocket::SimpleSocket(
int domain,
int type,
int protocol)
25: m_peerDisconnected(false)
26, m_connectionOriented(type == SOCK_STREAM)
32 if( (m_socket = ::socket( domain, type, protocol)) < 0)
36SimpleSocket::SimpleSocket(
int sockfd)
38, m_peerDisconnected(false)
39, m_connectionOriented(true)
42 throw SocketException(
"Tried to initialize Socket with invalid Handle",
false);
45SimpleSocket::~SimpleSocket()
47 if( m_socket >= 0) ::close(m_socket);
55 flags |= MSG_NOSIGNAL;
57 int sent = TEMP_FAILURE_RETRY (
::send( m_socket, (
const raw_type*) buffer, len, flags));
60 if( errno == ECONNRESET || errno == ECONNREFUSED || errno == EPIPE)
62 m_peerDisconnected =
true;
72 int ret = TEMP_FAILURE_RETRY (::recv( m_socket, (raw_type*) buffer, len, 0));
75 if( errno == ECONNRESET && m_connectionOriented)
77 m_peerDisconnected =
true;
83 if( ret == 0 && len != 0 && m_connectionOriented)
84 m_peerDisconnected =
true;
93 poll.events = POLLIN | POLLPRI | POLLRDHUP;
95 int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
97 if( ret == 0)
return 0;
100 if( poll.revents & POLLRDHUP || poll.revents & POLLHUP)
101 m_peerDisconnected =
true;
103 if( poll.revents & POLLIN || poll.revents & POLLPRI)
106 if( poll.revents & (POLLERR | POLLNVAL))
115 std::memset( &addr, 0,
sizeof(addr));
116 addr.sa_family = AF_UNSPEC;
118 if( ::connect( m_socket, &addr,
sizeof(addr)) < 0)
120 if( errno != ECONNRESET)
123 m_peerDisconnected =
false;
134 return m_peerDisconnected;
139 int ret =
::listen( m_socket, backlog);
148 int ret = TEMP_FAILURE_RETRY (::accept4( m_socket,
nullptr,
nullptr, SOCK_CLOEXEC));
150 int ret = TEMP_FAILURE_RETRY (
::accept( m_socket,
nullptr,
nullptr));
162 poll.events = POLLIN;
164 int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
165 if( ret == 0)
return -1;
169 if( poll.revents & (POLLERR | POLLNVAL))
180 const char* buf =
static_cast<const char*
>(buffer) + sent;
181 auto ret =
send( buf, len - sent);
182 sent +=
static_cast<size_t>(ret);
183 if( m_peerDisconnected)
break;
191 while( received != len)
193 char* buf =
static_cast<char*
>(buffer) + received;
194 auto ret =
receive( buf, len - received);
196 received +=
static_cast<size_t>(ret);
int timedReceive(void *buffer, size_t len, int timeout)
receive data from a bound socket, return after the given timespan
void disconnect()
Disconnect and unset any foreign addresses.
int accept() const
allows stream sockets to accept connections into Handles
int receiveAll(void *buffer, size_t len)
receive exactly the requested amount unless the peer disconnects
void listen(int backlog=5)
listen for incoming connections
void shutdown(ShutdownDirection type)
shutdown the connection in the specified direction
int sendAll(const void *buffer, size_t len)
send data through a connected socket
int send(const void *buffer, size_t len)
send data through a connected socket
int receive(void *buffer, size_t len)
receive data from a bound socket
bool peerDisconnected() const noexcept
returns whether a currently associated peer disconnected
int timedAccept(int timeout) const
allows stream sockets timed accept
Signals a problem with the execution of a socket call.
SocketException(std::string_view message, bool inclSysMsg=true)