int main() { TimeStamp start; TimeStamp newTime; TimeStamp dt; start.now(); while (1) { newTime.now(); dt=newTime-start; std::cout << dt.getSec() << "sec " << dt.getUSec() << "usec\n"; if (dt.getSec()>3) return 0; } return 1; }
int Server::main() { #ifndef WINDOOF signal(SIGPIPE,sigPipeHandler); #endif signal(SIGTERM,sigTermHandler); signal(SIGINT,sigTermHandler); NetStreamBufServer listener(m_config.m_port); listener.init(); listener.newConnection.connect(SigC::slot(*this,&Server::handleNewConnection)); listener.dataAvailable.connect(SigC::slot(*this,&Server::handleDataAvailable)); listener.connectionClosed.connect(SigC::slot(*this,&Server::handleConnectionClosed)); if (!m_config.m_useMetaServer) m_config.m_metaServer.clear(); else{ if (m_config.m_myAddress.empty()) /* myAddress not set => we only report our port and the metaserver will use the ip from which it was connected (if the server is behind a nat-firewall this means that the nat firewall must redirect this port to the server) */ m_maddr.port=m_config.m_port; else{ /* myAddress is set => we report it to the metaserver (if the address contains a : the port is assumed to follow the : */ m_maddr.adr=m_config.m_myAddress; std::string::size_type pos(m_maddr.adr.find_first_of(':')); if (pos!=std::string::npos) { if (pos+1<m_maddr.adr.size()) stringToAny(std::string(m_maddr.adr,pos+1),m_maddr.port); else m_maddr.port=m_config.m_port; m_maddr.adr=std::string(m_maddr.adr,0,pos); } } } const std::string &msURI(m_config.m_metaServer); if (!msURI.empty()) { try { MetaServer metaServer(msURI.c_str()); RegisterServer reg; reg.host=m_maddr; ServerRegistered answer; metaServer.rpc(reg,answer); std::cerr << "Metaserver answered !:\nRegistered: " << answer.registered << " , secret:"<<answer.secret<<std::endl; m_msecret=answer.secret; updateMetaserver(); }catch(...){ DOPE_WARN("Could not connect to Metaserver\n"); } } TimeStamp start; start.now(); TimeStamp oldTime; TimeStamp newTime; TimeStamp stepSize(0,11111); // ~90Hz TimeStamp frameSize(stepSize); TimeStamp dt; TimeStamp null; TimeStamp timeOut; oldTime.now(); unsigned frames=0; while (!quit) { listener.select(&null); // test for input and emit corresponding signals newTime.now(); dt=newTime-oldTime; // consume remaining time (this is the end of one frame) while(dt<frameSize) { timeOut=(frameSize-dt); listener.select(&timeOut); newTime.now(); dt=newTime-oldTime; } #ifdef ADIC_DEBUG_TIMING // todo remove again // last frame size was dt // and it should have been frameSize std::cerr << "\nLast frame took: " << (R(dt.getSec())+(R(dt.getUSec())/1000000)) << " and should have taken: " << (R(frameSize.getSec())+(R(frameSize.getUSec())/1000000)); #endif frameSize=(dt-frameSize); int eframes=1; while (frameSize>stepSize) { ++eframes; frameSize-=stepSize; } frameSize=stepSize-frameSize; #ifdef ADIC_DEBUG_TIMING // todo remove again std::cerr << "\nFramesize: "<< (R(frameSize.getSec())+(R(frameSize.getUSec())/1000000)); if (eframes>1) { DOPE_WARN("\nmachine too slow: calculate "<<eframes<<" frames at once"); } #endif // start of one frame R rdt(R(stepSize.getSec())+R(stepSize.getUSec())/1000000); for (int f=0;f<eframes;++f) m_game.step(rdt); // todo perhaps choose different frames for different clients if (!(frames%m_config.m_broadcastFreq)) broadcastGame(); // check for win condition TeamID wt; int winner(m_game.getWinner(wt)); if (winner) { if (winner==2) std::cout << "\nThe game ended in a draw\n"; else // the winner is std::cout << "\nTeam \""<<m_game.getTeams()[wt].name << "\" wins\n"; EndGame msg; msg.reason=winner; msg.winner=wt; broadcast(msg); restart(); } oldTime=newTime; ++frames; dt=newTime-start; R uptime=R(dt.getSec())+(R(dt.getUSec())/1000000); std::cout << "\rUp: " << std::fixed << std::setprecision(2) << std::setw(8) << uptime << " FPS: " << std::setw(6) << R(frames)/uptime << " Frame: " << std::setw(8) << frames; } connections.clear(); if (!m_msecret.empty()) { try { MetaServer metaServer(msURI.c_str()); ServerExit exitmsg; exitmsg.host=m_maddr; exitmsg.secret=m_msecret; Result answer; metaServer.rpc(exitmsg,answer); answer.print(); }catch(...){ DOPE_WARN("Could not connect to Metaserver\n"); } } return 0; }