c++ - Handling multiple client sockets with SFML -
i trying write networking part of little multiplayer game, , facing problem store tcp sockets are, in sfml, non-copyable (i beginner in c++).
i have 3 classes : server
, client
(a server-side class used store informations connecting client) , clientmanager
, in charge of storing clients , giving them ids, etc.
clientmanager.h
class clientmanager { public: clientmanager(); std::map<int, net::client*> getclients(); int attribid(); void addclient(net::client *client); sf::tcpsocket getclientsocket(int id) throw(std::string); void setclientsocket(int id, sf::tcpsocket); private: std::map<int, net::client*> m_clients; std::map<int, sf::tcpsocket> m_clientsockets; std::vector<int> m_ids; int m_lastid; };
what planned originally, when client connects, :
void net::server::waitforclient() { while(true) { if(m_listener.accept(m_tcpsocket) != socket::done) { cout << "error happened during client connection. skipping. " << endl; return; } int newid = m_clientmanager.attribid(); this->m_clientmanager.addclient(new net::client(newid, m_tcpsocket, m_tcpsocket.getremoteaddress())); } }
so, adding new client
clientmanager's list, id, tcpsocket send info , address.
but, problem sfml's tcpsocket class not copyable, means can't copy client this.
i pass pointer original tcpsocket, if client connects ? data pointer points have change , program bug. not know if behavior same smart pointers, think (but don't master them @ all).
storing them in std::map
or std::vector
causes same problem, both copy object. storing them pointers (in map) original tcpsocket cause same problem before, because socket change too, , pointers point same object.
how can store sockets without having use references, pointers or copy object ?
any appreciated :)
it's going real pain without pointers. use smart pointers manage sockets (std::vector<std::unique_ptr<sf::tcpsocket>>
or similiar), along `sf::socketselector' manage actual communication
Comments
Post a Comment