2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Neirokan on 30.04.2020
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ENGINE_UDPSOCKET_H
|
|
|
|
#define ENGINE_UDPSOCKET_H
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "ReliableMsg.h"
|
|
|
|
#include "UDPConnection.h"
|
|
|
|
#include "MsgType.h"
|
|
|
|
|
2021-10-12 20:18:56 +03:00
|
|
|
class UDPSocket final {
|
2021-09-13 15:53:43 +03:00
|
|
|
private:
|
|
|
|
sf::UdpSocket _socket;
|
|
|
|
sf::Uint16 _nextRelyMsgId;
|
|
|
|
sf::Uint16 _ownId;
|
|
|
|
const sf::Uint16 _serverId = 0;
|
|
|
|
|
|
|
|
std::map<sf::Uint16, UDPConnection> _connections;
|
|
|
|
std::map<sf::Uint16, ReliableMsg> _relyPackets;
|
|
|
|
std::map<sf::Uint32, double> _confirmTimes;
|
|
|
|
std::function<bool(sf::Uint16)> _timeoutCallback;
|
|
|
|
|
|
|
|
bool confirmed(sf::Uint16 msgId, sf::Uint16 senderId);
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UDPSocket();
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
bool bind(sf::Uint16 port);
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void unbind();
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void setTimeoutCallback(std::function<bool(sf::Uint16)> callback);
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void addConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port);
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void removeConnection(sf::Uint16 id);
|
|
|
|
|
|
|
|
void setId(sf::Uint16 id);
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
[[nodiscard]] sf::Uint16 ownId() const;
|
2021-10-31 11:39:08 +03:00
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
[[nodiscard]] sf::Uint16 serverId() const;
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void send(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port);
|
|
|
|
|
|
|
|
void send(const sf::Packet &packet, sf::Uint16 id);
|
|
|
|
|
|
|
|
void sendRely(const sf::Packet &packet, const sf::IpAddress &ip, sf::Uint16 port);
|
|
|
|
|
|
|
|
void sendRely(const sf::Packet &packet, sf::Uint16 id);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
void update();
|
2021-10-31 11:39:08 +03:00
|
|
|
|
|
|
|
MsgType receive(sf::Packet &packet, sf::Uint16 &senderId);
|
2021-10-16 20:22:55 +03:00
|
|
|
|
|
|
|
~UDPSocket();
|
2021-09-13 15:53:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif //INC_3DZAVR_UDPSOCKET_H
|