Gamedev Framework (gf)  0.15.0
A C++14 framework for 2D games
TcpSocket.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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_TCP_SOCKET_H
22 #define GF_TCP_SOCKET_H
23 
24 #include <cstdint>
25 #include <string>
26 
27 #include "ArrayRef.h"
28 #include "BufferRef.h"
29 #include "Packet.h"
30 #include "Portability.h"
31 #include "Serialization.h"
32 #include "Streams.h"
33 #include "Socket.h"
34 
35 namespace gf {
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 inline namespace v1 {
38 #endif
39 
53  class GF_API TcpSocket : public Socket {
54  public:
60  TcpSocket() = default;
61 
69  TcpSocket(const std::string& hostname, const std::string& service, SocketFamily family = SocketFamily::Unspec);
70 
74  TcpSocket(const TcpSocket&) = delete;
75 
79  TcpSocket& operator=(const TcpSocket&) = delete;
80 
84  TcpSocket(TcpSocket&&) = default;
85 
89  TcpSocket& operator=(TcpSocket&&) = default;
90 
96  ~TcpSocket();
97 
101  SocketAddress getRemoteAddress() const;
102 
110 
117  SocketDataResult recvRawBytes(BufferRef<uint8_t> buffer);
118 
127  bool sendBytes(ArrayRef<uint8_t> buffer);
128 
138  bool recvBytes(BufferRef<uint8_t> buffer);
139 
146  bool sendPacket(const Packet& packet);
147 
154  bool recvPacket(Packet& packet);
155 
165  template<typename T>
166  bool sendData(const T& data) {
167  Packet packet;
168  BufferOutputStream stream(&packet.bytes);
169  gf::Serializer serializer(stream);
170  serializer | const_cast<T&>(data);
171  return sendPacket(packet);
172  }
173 
183  template<typename T>
184  bool recvData(T& data) {
185  Packet packet;
186  bool res = recvPacket(packet);
187 
188  if (res) {
189  BufferInputStream stream(&packet.bytes);
190  gf::Deserializer deserializer(stream);
191  deserializer | data;
192  }
193 
194  return res;
195  }
196 
197  private:
198  TcpSocket(SocketHandle handle);
199 
200  friend class TcpListener;
201 
202  private:
203  static SocketHandle nativeConnect(const std::string& host, const std::string& service, SocketFamily family);
204  };
205 
206 #ifndef DOXYGEN_SHOULD_SKIP_THIS
207 }
208 #endif
209 }
210 
211 #endif // GF_TCP_SOCKET_H
A deserializer from a binary file.
Definition: Serialization.h:153
SocketFamily
A socket family.
Definition: SocketAddress.h:50
The result of a socket operation.
Definition: Socket.h:97
bool recvData(T &data)
Receive arbitrary data from the socket.
Definition: TcpSocket.h:184
A TCP listener.
Definition: TcpListener.h:58
Unspecified (either IPv4 or IPv6)
Buffer input stream.
Definition: Streams.h:140
constexpr BufferRef< T > buffer(T *data, std::size_t size)
Create a reference to a buffer.
Definition: BufferRef.h:211
A packet of bytes.
Definition: Packet.h:38
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
A TCP socket.
Definition: TcpSocket.h:53
Buffer output stream.
Definition: Streams.h:266
bool sendData(const T &data)
Send arbitrary data to the socket.
Definition: TcpSocket.h:166
std::vector< uint8_t > bytes
Definition: Packet.h:39
implementation-defined SocketHandle
A native socket handle.
Definition: Socket.h:63
A network socket.
Definition: Socket.h:109
A socket address.
Definition: SocketAddress.h:78