Commit c34d5253 by Anselme

fixed quitting when port not available, fixed endless waiting

parent efc3debf
......@@ -13,24 +13,29 @@ Server::Server(unsigned short port)
{
m_map = new MapInstance(this);
m_socket_handler = new SocketHandler(this,port);
m_socketUpdaterRunning = true;
m_socketUpdaterRunning = new bool(true);
new std::thread(Server::updateSockets, m_socket_handler, m_socketUpdaterRunning);
}
Server::~Server()
{
m_socketUpdaterRunning = false;
delete m_map;
delete m_socket_handler;
}
void Server::destroy()
{
*m_socketUpdaterRunning = false;
}
void Server::initMap(){
m_map->setMapData(MapKoban::getTestMap());
}
void Server::updateSockets(SocketHandler* socket_handler, const bool& running){
while(running)
void Server::updateSockets(SocketHandler* socket_handler, bool* running){
while(*running)
socket_handler->update();
delete running;
delete socket_handler;
}
void Server::update()
......
......@@ -23,16 +23,18 @@ class Server
MapDiff m_diff; //m
//std::mutex m_mutex; // m?
bool m_socketUpdaterRunning;
bool* m_socketUpdaterRunning;
public:
Server(unsigned short port);
~Server();
void destroy();
void update();
MapInstance* getMap(){return m_map;}
SocketHandler* getSocketHandler(){return m_socket_handler;}
static void updateSockets(SocketHandler* socket_handler, const bool &running);
static void updateSockets(SocketHandler* socket_handler, bool* running);
// PlayerAction resolveMove(sf::Vector2i pos, Direction dir);
// void stackAction(PlayerDiff p);
......
......@@ -16,22 +16,29 @@ SocketHandler::SocketHandler(Server* s,unsigned short port):
m_server(s)
{
m_listener.setBlocking(false);
if(m_listener.listen(port) == sf::TcpListener::Status::Done){
std::cout << "Server listening on port " << port << std::endl;
while(m_listener.listen(port) != sf::TcpListener::Status::Done)
{
std::cout << "Port " << port << " already in use... reconnecting in 5 seconds..." << std::endl;
sf::sleep(sf::seconds(5));
}
else
exit(0);
std::cout << "Server listening on port " << port << std::endl;
m_selector.add(m_listener);
}
SocketHandler::~SocketHandler()
{
m_listener.close();
for(auto it : m_clients)
delete it.first;
{
sf::TcpSocket* socket = it.first;
socket->disconnect();
delete socket;
}
delete m_server;
}
void SocketHandler::update(){
if(m_selector.wait())
if(m_selector.wait(sf::seconds(1)))
{
if(m_selector.isReady(m_listener))
handleListenerMessage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment