Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Socket.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_SOCKET_H
22 #define GF_SOCKET_H
23 
24 #include "NetApi.h"
25 #include "SocketAddress.h"
26 #include "SocketGuard.h"
27 #include "SocketTypes.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
41  class GF_NET_API Socket : private SocketGuard {
42  public:
48  ~Socket();
49 
53  Socket(const Socket&) = delete;
54 
58  Socket& operator=(const Socket&) = delete;
59 
65  Socket(Socket&& other) noexcept;
66 
73  Socket& operator=(Socket&& other) noexcept;
74 
80  operator bool () const noexcept {
81  return m_handle != InvalidSocketHandle;
82  }
83 
89  SocketAddress getLocalAddress() const;
90 
96  void setBlocking();
97 
103  void setNonBlocking();
104 
105  protected:
106 #ifndef DOXYGEN_SHOULD_SKIP_THIS
107  Socket()
108  : m_handle(InvalidSocketHandle)
109  {
110 
111  }
112 
113  void setHandle(SocketHandle handle) {
114  m_handle = handle;
115  }
116 
117  SocketHandle getHandle() const {
118  return m_handle;
119  }
120 #endif
121 
122  private:
123  friend class SocketSelector;
124  SocketHandle m_handle;
125  };
126 
127 #ifndef DOXYGEN_SHOULD_SKIP_THIS
128 }
129 #endif
130 }
131 
132 #endif // GF_SOCKET_H
The namespace for gf classes.
Definition: Action.h:35
constexpr SocketHandle InvalidSocketHandle
An invalid socket handle.
Definition: SocketTypes.h:67
A socket selector.
Definition: SocketSelector.h:50
A guard to handle library initialization.
Definition: SocketGuard.h:42
implementation-defined SocketHandle
A native socket handle.
Definition: SocketTypes.h:55
A network socket.
Definition: Socket.h:41
A socket address.
Definition: SocketAddress.h:78