sockethandler.h 986 Bytes
Newer Older
1 2 3 4 5
#ifndef SOCKETHANDLER_H
#define SOCKETHANDLER_H


#include <SFML/Network.hpp>
6
#include "data.h"
7
#include <unordered_map>
8
#include <set>
9 10 11 12 13 14 15 16 17 18 19 20

class Server;

class SocketHandler
{
    Server* m_server;
    sf::TcpListener m_listener;
    sf::SocketSelector m_selector;
    std::unordered_map<sf::TcpSocket*,ClientData> m_clients;
    std::vector<sf::TcpSocket*> m_to_erase;
    std::set<sf::IpAddress> m_ip_timeout_table;
public:
21
    SocketHandler(Server* s, unsigned short port);
22 23 24 25 26 27 28 29 30 31 32

    void update();

    bool isAllowed(sf::IpAddress);

    void handleListenerMessage();
    void handleSocketMessage(sf::TcpSocket& socket);
    bool safeReceive(sf::TcpSocket&,sf::Packet&,bool blocking = false);
    bool safeSend(sf::TcpSocket &socket, sf::Packet& packet, bool blocking = false);

    void disconnectClient(sf::TcpSocket &socket);
33
    void removeDisconnectedPlayer();
34 35 36 37 38
    //broadcast to all authenticated client;
    void broadcastMessage(sf::Packet& packet);
};

#endif // SOCKETHANDLER_H