simple-socket library 1.5.0
Loading...
Searching...
No Matches
SocketHandle.h
1#ifndef NET_SocketHandle_h__
2#define NET_SocketHandle_h__
3
4#include <unistd.h>
5
6namespace NET
7{
8 class TCPSocket;
9 class SCTPSocket;
10 class UnixStreamSocket;
11
13
37 template<class Socket>
39 {
40 public:
41 friend class TCPSocket;
42 friend class SCTPSocket;
43 friend class UnixStreamSocket;
44
46 typedef Socket socket_type;
47
49 SocketHandle() noexcept : m_sockfd(-1) {}
50
51 SocketHandle( const SocketHandle&) = delete;
52 SocketHandle& operator=( const SocketHandle&) = delete;
53
55 SocketHandle( SocketHandle&& other) noexcept : m_sockfd(other.release()) {}
56
59 {
60 if( this != &other)
61 reset(other.release());
62 return *this;
63 }
64
65 ~SocketHandle() noexcept { reset(); }
66
68
75 explicit operator bool() const noexcept { return m_sockfd >= 0; }
76
77 private:
79 explicit SocketHandle( int sockfd) noexcept : m_sockfd(sockfd) {}
80
82 int release() noexcept
83 {
84 int tmp = m_sockfd;
85 m_sockfd = -1;
86 return tmp;
87 }
88
90 void reset( int fd = -1) noexcept
91 {
92 if( fd != m_sockfd)
93 {
94 if( m_sockfd >= 0) ::close(m_sockfd);
95 m_sockfd = fd;
96 }
97 }
98
99 int m_sockfd;
100 };
101
102} // namespace NET
103
104#endif // NET_SocketHandle_h__
SCTP socket class.
Definition SCTPSocket.h:15
A simple class to provide strict ownership of socket handles.
SocketHandle(SocketHandle &&other) noexcept
move constructor: transfers ownership from other
SocketHandle & operator=(SocketHandle &&other) noexcept
move assignment: closes the currently owned descriptor first
SocketHandle() noexcept
constructs an invalid socket handle
TCP socket class.
Definition TCPSocket.h:11
Unix domain stream socket class.