Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
TcpClient.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Network module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_TCPCLIENT_HPP
8 #define NAZARA_TCPCLIENT_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/ByteArray.hpp>
12 #include <Nazara/Core/Stream.hpp>
13 #include <Nazara/Network/AbstractSocket.hpp>
14 #include <Nazara/Network/IpAddress.hpp>
15 
16 namespace Nz
17 {
18  struct NetBuffer;
19  class NetPacket;
20 
21  class NAZARA_NETWORK_API TcpClient : public AbstractSocket, public Stream
22  {
23  friend class TcpServer;
24 
25  public:
26  inline TcpClient();
27  TcpClient(TcpClient&& tcpClient) = default;
28  ~TcpClient() = default;
29 
30  SocketState Connect(const IpAddress& remoteAddress);
31  SocketState Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr);
32  inline void Disconnect();
33 
34  void EnableLowDelay(bool lowDelay);
35  void EnableKeepAlive(bool keepAlive, UInt64 msTime = 10000, UInt64 msInterval = 1000);
36 
37  bool EndOfStream() const override;
38 
39  UInt64 GetCursorPos() const override;
40  inline UInt64 GetKeepAliveInterval() const;
41  inline UInt64 GetKeepAliveTime() const;
42  inline IpAddress GetRemoteAddress() const;
43  UInt64 GetSize() const override;
44 
45  inline bool IsLowDelayEnabled() const;
46  inline bool IsKeepAliveEnabled() const;
47 
48  bool Receive(void* buffer, std::size_t size, std::size_t* received);
49  bool ReceivePacket(NetPacket* packet);
50 
51  bool Send(const void* buffer, std::size_t size, std::size_t* sent);
52  bool SendMultiple(const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent);
53  bool SendPacket(const NetPacket& packet);
54 
55  bool SetCursorPos(UInt64 offset) override;
56 
57  bool WaitForConnected(UInt64 msTimeout = 3000);
58 
59  inline TcpClient& operator=(TcpClient&& tcpClient) = default;
60 
61  private:
62  void FlushStream() override;
63 
64  void OnClose() override;
65  void OnOpened() override;
66 
67  std::size_t ReadBlock(void* buffer, std::size_t size) override;
68  void Reset(SocketHandle handle, const IpAddress& peerAddress);
69  std::size_t WriteBlock(const void* buffer, std::size_t size) override;
70 
71  struct PendingPacket
72  {
73  std::size_t received = 0;
74  ByteArray data;
75  UInt16 netcode;
76  bool headerReceived = false;
77  };
78 
79  IpAddress m_peerAddress;
80  PendingPacket m_pendingPacket;
81  UInt64 m_keepAliveInterval;
82  UInt64 m_keepAliveTime;
83  bool m_isKeepAliveEnabled;
84  bool m_isLowDelayEnabled;
85  };
86 }
87 
88 #include <Nazara/Network/TcpClient.inl>
89 
90 #endif // NAZARA_TCPCLIENT_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Network class that represents an IP address.
Definition: IpAddress.hpp:21
Core class that represents a string.
Definition: String.hpp:22
Network class that represents a client in a TCP connection.
Definition: TcpClient.hpp:21
Network class that represents a packet.
Definition: NetPacket.hpp:18
Core class that represents an array of bytes.
Definition: ByteArray.hpp:18
Core class that represents a stream.
Definition: Stream.hpp:19
Network class that represents a server in a TCP connection.
Definition: TcpServer.hpp:18
Network class that represents the base of socket.
Definition: AbstractSocket.hpp:18