simple-socket library 1.2.0
SimpleSocket.cpp
Go to the documentation of this file.
1#include "SimpleSocket.h"
2#include "TempFailure.h"
3
4#include <poll.h>
5#include <unistd.h>
6#include <cstring>
7#include <cerrno>
8
9using namespace NET;
10
11SocketException::SocketException( std::string_view message, bool inclSysMsg /* = true */)
12: m_message(message)
13, m_errorcode(0)
14{
15 if(inclSysMsg)
16 {
17 m_errorcode = errno;
18 m_message += ": ";
19 m_message += strerror(m_errorcode);
20 }
21}
22
23SimpleSocket::SimpleSocket( int domain, int type, int protocol)
24: m_peerDisconnected(false)
25{
26 if( (m_socket = ::socket( domain, type, protocol)) < 0)
27 throw SocketException("Socket creation failed (socket)");
28}
29
31: m_socket(sockfd)
32, m_peerDisconnected(false)
33{
34 if(sockfd < 0)
35 throw SocketException("Tried to initialize Socket with invalid Handle", false);
36}
37
39{
41 // on Windows do cleanup here
42}
43
44int SimpleSocket::send( const void* buffer, size_t len)
45{
46 int sent = TEMP_FAILURE_RETRY (::send( m_socket, (const raw_type*) buffer, len, 0));
47 if( sent < 0)
48 {
49 switch(errno)
50 {
51 case ECONNRESET:
52 case ECONNREFUSED:
53 m_peerDisconnected = true;
54 break;
55 default:
56 throw SocketException("Send failed (send)");
57 }
58 }
59 return sent;
60}
61
62int SimpleSocket::receive( void* buffer, size_t len)
63{
64 int ret = TEMP_FAILURE_RETRY (::recv( m_socket, (raw_type*) buffer, len, 0));
65 if( ret < 0) throw SocketException("Received failed (receive)");
66 return ret;
67}
68
69int SimpleSocket::timedReceive( void* buffer, size_t len, int timeout)
70{
71 struct pollfd poll;
72 poll.fd = m_socket;
73 poll.events = POLLIN | POLLPRI | POLLRDHUP;
74
75 int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
76
77 if( ret == 0) return 0;
78 if( ret < 0) throw SocketException("timedReceive failed (poll)");
79
80 if( poll.revents & POLLRDHUP)
81 m_peerDisconnected = true;
82
83 if( poll.revents & POLLIN || poll.revents & POLLPRI)
84 {
85 ret = TEMP_FAILURE_RETRY (::recv( m_socket, (raw_type*) buffer, len, 0));
86 if( ret < 0)
87 throw SocketException("timedReceive failed (recv)");
88 }
89
90 return ret;
91}
92
94{
95 sockaddr addr;
96 std::memset( &addr, 0, sizeof(addr));
97 addr.sa_family = AF_UNSPEC;
98
99 if( ::connect( m_socket, &addr, sizeof(addr)) < 0)
100 {
101 if( errno != ECONNRESET)
102 throw SocketException("Disconnect failed (connect)");
103
104 m_peerDisconnected = false;
105 }
106}
107
109{
110 if( ::shutdown( m_socket, type) < 0)
111 throw SocketException("Shutdown failed (shutdown)");
112}
113
115{
116 return m_peerDisconnected;
117}
static const int len
#define TEMP_FAILURE_RETRY(expression)
Definition TempFailure.h:8
Definition CANFrame.h:7
Signals a problem with the execution of a socket call.
SocketException(std::string_view message, bool inclSysMsg=true)
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.
void shutdown(ShutdownDirection type)
shutdown the connection in the specified direction
int send(const void *buffer, size_t len)
send data through a connected socket
bool peerDisconnected() const
returns whether a peer disconnected
SimpleSocket(int sockfd)
enables return of an accepted socket
int receive(void *buffer, size_t len)
receive data from a bound socket