simple-socket library 1.2.0
SimpleSocket.h
Go to the documentation of this file.
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
40 typedef void raw_type;
42
44
49 {
50 public:
52 {
53 STOP_SEND = SHUT_WR,
54 STOP_RECEIVE = SHUT_RD,
55 STOP_BOTH = SHUT_RDWR
56 };
57
59
61 inline int nativeHandle() { return m_socket; }
62
64
84 int send( const void* buffer, size_t len);
85
87
110 int receive( void* buffer, size_t len);
111
113
137 int timedReceive( void* buffer, size_t len, int timeout);
138
140
151 void disconnect();
152
154
170 void shutdown( ShutdownDirection type);
171
173
180 bool peerDisconnected() const;
181
182 protected:
184 {
185 INTERNET = PF_INET,
186 UNIX = PF_LOCAL,
187 CAN = PF_CAN
188 };
189
191 {
192 RAW = SOCK_RAW,
193 STREAM = SOCK_STREAM,
194 DATAGRAM = SOCK_DGRAM
195 };
196
198 explicit SimpleSocket( int sockfd);
199
201 SimpleSocket( int domain, int type, int protocol);
202
203 // socket descriptor
206
207 private:
208 // dont' allow
209 SimpleSocket( const SimpleSocket&);
210 const SimpleSocket& operator=( const SimpleSocket&);
211 };
212
213} // namespace NET
214
215#endif // NET_SimpleSocket_h__
static const int len
Definition CANFrame.h:7
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(const SocketException &)=default
~SocketException() noexcept
Provided just to guarantee that no exceptions are thrown.
int errorCode() const noexcept
Returns the glibc errno code of the current error.
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 nativeHandle()
return the native handle of the open socket
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
@ 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