simple-socket library 1.9.0
Loading...
Searching...
No Matches
SimpleSocket.h
Go to the documentation of this file.
1#ifndef NET_SimpleSocket_h__
2#define NET_SimpleSocket_h__
3
4#include <string>
5#include <string_view>
6#include <exception>
7
8namespace NET
9{
11 class SocketException : public std::exception
12 {
13 public:
19 SocketException( std::string_view message, bool inclErrno = true);
20
26 SocketException( std::string_view message, int error);
27
28 SocketException( const SocketException&) = default;
29
31 ~SocketException() noexcept {};
32
34 const char* what() const noexcept { return m_message.c_str(); }
35
37 int errorCode() const noexcept { return m_errorcode; }
38
39 private:
40 std::string m_message;
41 int m_errorcode;
42 };
43
46 {
49 };
50
51 // forward declare internal type
52 class SockAddress;
53
63
83 struct [[nodiscard]] IOResult
84 {
86 size_t bytes;
87
89 explicit operator bool() const noexcept { return status == IOStatus::Success; }
90 };
91
102
110
113 typedef void raw_type;
115
117
122 {
123 public:
124 SimpleSocket( const SimpleSocket&) = delete;
126
128 int nativeHandle() noexcept { return m_socket; }
129
131 [[nodiscard]] int duplicateNativeHandle() const;
132
134 bool valid() const noexcept;
135
137
157 IOResult send( const void* buffer, size_t len);
158
160
183 IOResult receive( void* buffer, size_t len);
184
186
210 IOResult timedReceive( void* buffer, size_t len, int timeout);
211
213
225
227
238 [[nodiscard]] bool isConnected() const;
239
241
258
260
266 [[nodiscard]] bool peerDisconnected() const;
267
269 [[nodiscard]] bool isBlocking() const;
270
272
281 void setBlocking(bool blocking);
282
284 void setSendBufferSize( int bytes);
285
287 void setReceiveBufferSize( int bytes);
288
289 protected:
291
303 void listen( int backlog = 128);
304
306
312 ConnectResult finishConnect( int timeout = 0);
313
315
332 IOResult sendAll( const void* buffer, size_t len);
333
335
348 IOResult receiveAll( void* buffer, size_t len);
349
351 SimpleSocket( int domain, int type, int protocol);
352
354 struct AdoptSocketTag {};
356 SimpleSocket( AdoptSocketTag, int sockfd, int type);
357
359 ~SimpleSocket() noexcept;
360
361 // allow moving concrete classes
362 SimpleSocket( SimpleSocket&& other) noexcept;
363 SimpleSocket& operator=( SimpleSocket&& other) noexcept;
364
366
368 IOResult sendTo( const void* buffer, size_t len, const SockAddress& addr);
369
371 IOResult receiveFrom( void* buffer, size_t len, SockAddress& addr);
372
374 IOStatus waitReadable( int timeout);
375
377 int acceptNative() const;
378
380 int accept() const;
381
383 int timedAccept( int timeout) const;
384
386 ConnectResult connect( const SockAddress& addr);
387
389 ConnectResult timedConnect( const SockAddress& addr, int timeout);
390
392
393 // socket descriptor
396 };
397
398} // namespace NET
399
400#endif // NET_SimpleSocket_h__
IOResult send(const void *buffer, size_t len)
send data through a connected socket
void shutdown(ShutdownDirection type)
shutdown the connection in the specified direction
IOResult receiveAll(void *buffer, size_t len)
receive exactly the requested amount unless the peer disconnects
bool isBlocking() const
returns true if socket is in blocking mode
SimpleSocket(AdoptSocketTag, int sockfd, int type)
allows a subclass to take ownership of an existing socket
SimpleSocket & operator=(const SimpleSocket &)=delete
void listen(int backlog=128)
listen for incoming connections
IOResult receive(void *buffer, size_t len)
receive data from a bound socket
SimpleSocket(const SimpleSocket &)=delete
IOResult sendAll(const void *buffer, size_t len)
send data through a connected socket
bool valid() const noexcept
Returns true if socket is in a usable (non-moved) state and open.
bool peerDisconnected() const
returns whether a currently associated peer disconnected
ConnectResult finishConnect(int timeout=0)
check if a previously started connected attempt is finished
int nativeHandle() noexcept
Return the native descriptor. Ownership remains with the object.
void setReceiveBufferSize(int bytes)
override default receive buffer size for this socket
IOResult timedReceive(void *buffer, size_t len, int timeout)
receive data from a bound socket, return after the given timespan
void setBlocking(bool blocking)
set socket to blocking or non-blocking I/O operations
int duplicateNativeHandle() const
Duplicates the underlaying native socket descriptor.
bool isConnected() const
check if the kernel currently has an associated peer address
void setSendBufferSize(int bytes)
override default send buffer size for this socket
~SimpleSocket() noexcept
protected destructor to avoid delete via base class
void disconnect()
Disconnect and unset any foreign addresses.
const char * what() const noexcept
Returns a C-string describing the cause of the current error.
SocketException(const SocketException &)=default
SocketException(std::string_view message, int 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.
SocketException(std::string_view message, bool inclErrno=true)
IOStatus
status of the I/O operation
AddressFamily
used by internet socket types
ShutdownDirection
direction to close for a stream socket
@ 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
@ STOP_RECEIVE
disable all variants of receive() on the socket, send calls still work
ConnectResult
result of the connection attempt
@ InProgress
the attempt is still pending, check again with finishConnect()
@ Connected
the connection is established
@ Unreachable
no route to the host or network
@ Aborted
the connection attempt was aborted or reset
@ Refused
the peer refused the connection
IOStatus status
used to safely adopt internal socket descriptor