simple-socket library 1.5.0
Loading...
Searching...
No Matches
SimpleSocket.h
1#ifndef NET_SimpleSocket_h__
2#define NET_SimpleSocket_h__
3
4#include <sys/socket.h>
5#include <string>
6#include <exception>
7
8namespace NET
9{
11 class SocketException : public std::exception
12 {
13 public:
20 SocketException( std::string_view message, bool inclSysMsg = true);
21
22 SocketException( const SocketException&) = default;
23
25 ~SocketException() noexcept {};
26
28 const char* what() const noexcept { return m_message.c_str(); }
29
31 int errorCode() const noexcept { return m_errorcode; }
32
33 private:
34 std::string m_message;
35 int m_errorcode;
36 };
37
38 enum AddressFamily
39 {
40 IPv4,
41 IPv6
42 };
43
46 typedef void raw_type;
48
50
54 class SimpleSocket
55 {
56 public:
58 {
59 STOP_SEND = SHUT_WR,
60 STOP_RECEIVE = SHUT_RD,
61 STOP_BOTH = SHUT_RDWR
62 };
63
64 SimpleSocket( const SimpleSocket&) = delete;
65 SimpleSocket& operator=( const SimpleSocket&) = delete;
66
68 int nativeHandle() noexcept { return m_socket; }
69
71
91 int send( const void* buffer, size_t len);
92
94
117 int receive( void* buffer, size_t len);
118
120
144 int timedReceive( void* buffer, size_t len, int timeout);
145
147
158 void disconnect();
159
161
177 void shutdown( ShutdownDirection type);
178
180
191 bool peerDisconnected() const noexcept;
192
193 protected:
195
211 void listen( int backlog = 5);
212
214
231 int sendAll( const void* buffer, size_t len);
232
234
248 int receiveAll( void* buffer, size_t len);
249
251 explicit SimpleSocket( int sockfd);
252
254 SimpleSocket( int domain, int type, int protocol);
255
256 // protected to avoid delete via base class
257 ~SimpleSocket();
258
260 int accept() const;
261
263 int timedAccept( int timeout) const;
264
265 // socket descriptor
266 int m_socket;
267 bool m_peerDisconnected;
268 bool m_connectionOriented;
269 };
270
271} // namespace NET
272
273#endif // NET_SimpleSocket_h__
A socket representing a basic communication endpoint.
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 nativeHandle() noexcept
return the native handle of the open socket
@ STOP_RECEIVE
disable all variants of receive() on the socket, send calls still work
@ STOP_SEND
disable all variants of send() on the socket, receive calls still work
@ STOP_BOTH
disable all send() and receive() calls on the 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
SocketException(std::string_view message, bool inclSysMsg=true)
const char * what() const noexcept
Returns a C-string describing the cause of the current error.
~SocketException() noexcept
Provided just to guarantee that no exceptions are thrown.
int errorCode() const noexcept
Returns the glibc errno code of the current error.