sf::Socket::Status NETSERVER::acceptTime(sf::TcpListener &p_listener, sf::TcpSocket &p_client) { sf::SocketSelector selector; selector.add(p_listener); if(selector.wait(sf::seconds(0.5))) { return p_listener.accept(p_client); } else { return sf::Socket::NotReady; } } // acceptTime()
inline void start(unsigned short mPort) { if(listener.listen(mPort) != sf::Socket::Done) { ssvu::lo("Server") << "Error initalizing listener\n"; return; } else ssvu::lo("Server") << "Listener initialized\n"; running = true; updateFuture = std::async(std::launch::async, [this] { updateImpl(); }); }
inline bool tryAccept(sf::TcpListener& mListener) { if(busy) { ssvu::lo("ManagedSocket") << "Error: already connected" << std::endl; return false; } for(int i{0}; i < 5; ++i) { if(!busy && mListener.accept(socket) == sf::Socket::Done) goto succeed; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } return false; succeed: if(!busy) { ssvu::lo("ManagedSocket") << "Accepting..." << std::endl; busy = true; handlerFuture = std::async(std::launch::async, [this]{ while(busy) update(); }); } ssvu::lo("ManagedSocket") << "Accepted" << std::endl; return true; }
inline void stop() { for(auto& ch : clientHandlers) ch->stop(); running = false; listener.close(); }
inline Server(PacketHandler<ClientHandler>& mPacketHandler) : packetHandler(mPacketHandler) { listener.setBlocking(false); }