Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
UdpSocket.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_UDPSOCKET_HPP
8 #define NAZARA_UDPSOCKET_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Network/AbstractSocket.hpp>
12 #include <Nazara/Network/IpAddress.hpp>
13 
14 namespace Nz
15 {
16  struct NetBuffer;
17  class NetPacket;
18 
19  class NAZARA_NETWORK_API UdpSocket : public AbstractSocket
20  {
21  public:
22  inline UdpSocket();
23  inline UdpSocket(NetProtocol protocol);
24  inline UdpSocket(UdpSocket&& udpSocket);
25  ~UdpSocket() = default;
26 
27  inline SocketState Bind(UInt16 port);
28  SocketState Bind(const IpAddress& address);
29 
30  inline bool Create(NetProtocol protocol);
31 
32  void EnableBroadcasting(bool broadcasting);
33 
34  inline IpAddress GetBoundAddress() const;
35  inline UInt16 GetBoundPort() const;
36 
37  inline bool IsBroadcastingEnabled() const;
38 
39  std::size_t QueryMaxDatagramSize();
40 
41  bool Receive(void* buffer, std::size_t size, IpAddress* from, std::size_t* received);
42  bool ReceiveMultiple(NetBuffer* buffers, std::size_t bufferCount, IpAddress* from, std::size_t* received);
43  bool ReceivePacket(NetPacket* packet, IpAddress* from);
44 
45  bool Send(const IpAddress& to, const void* buffer, std::size_t size, std::size_t* sent);
46  bool SendMultiple(const IpAddress& to, const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent);
47  bool SendPacket(const IpAddress& to, const NetPacket& packet);
48 
49  private:
50  void OnClose() override;
51  void OnOpened() override;
52 
53  IpAddress m_boundAddress;
54  bool m_isBroadCastingEnabled;
55  };
56 }
57 
58 #include <Nazara/Network/UdpSocket.inl>
59 
60 #endif // NAZARA_UDPSOCKET_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Network class that represents an IP address.
Definition: IpAddress.hpp:21
Network class that represents a packet.
Definition: NetPacket.hpp:18
Network class that represents a UDP socket, allowing for sending/receiving datagrams.
Definition: UdpSocket.hpp:19
Network class that represents the base of socket.
Definition: AbstractSocket.hpp:18