Skip to content

File UdpClient.hpp

File List > Engine > include > Network > UdpClient.hpp

Go to the documentation of this file

#ifndef ENGINE_NETWORK_UDP_CLIENT_HPP_
#define ENGINE_NETWORK_UDP_CLIENT_HPP_

#include "Compat/EnetWrapper.hpp"
#include "Network/ByteStream.hpp"
#include "Network/UdpPacket.hpp"
#include <functional>
#include <string>

namespace Engine::Network {

enum class ConnectionState { Disconnected, Connecting, Connected };

class UdpClient {
  public:
    UdpClient();
    virtual ~UdpClient();

    // Non-copyable
    UdpClient(const UdpClient&) = delete;
    UdpClient& operator=(const UdpClient&) = delete;

    bool Connect(const std::string& hostAddress, uint16_t port, size_t channels = 2);

    void Disconnect();

    [[nodiscard]] bool IsConnected() const noexcept {
        return state_ == ConnectionState::Connected;
    }
    [[nodiscard]] ConnectionState GetState() const noexcept {
        return state_;
    }
    [[nodiscard]] uint32_t GetPing() const noexcept;

    void PollEvents(const std::function<void(const NetworkEvent&)>& handler);

    void SendPacket(const uint8_t* data, size_t size, bool reliable = true, uint8_t channel = 0);
    void SendPacket(const ByteWriter& writer, bool reliable = true, uint8_t channel = 0);

  private:
    ENetHost* clientHost_{nullptr};
    ENetPeer* serverPeer_{nullptr};
    ConnectionState state_{ConnectionState::Disconnected};
};

} // namespace Engine::Network

#endif // ENGINE_NETWORK_UDP_CLIENT_HPP_