[Orxonox-commit 587] r3119 - branches/netp4/src/orxonox/gamestates
scheusso at orxonox.net
scheusso at orxonox.net
Sun Jun 7 13:54:03 CEST 2009
Author: scheusso
Date: 2009-06-07 13:54:02 +0200 (Sun, 07 Jun 2009)
New Revision: 3119
Modified:
branches/netp4/src/orxonox/gamestates/GSDedicated.cc
branches/netp4/src/orxonox/gamestates/GSDedicated.h
Log:
first version of the i/o-console for the dedicated server
Modified: branches/netp4/src/orxonox/gamestates/GSDedicated.cc
===================================================================
--- branches/netp4/src/orxonox/gamestates/GSDedicated.cc 2009-06-03 22:01:16 UTC (rev 3118)
+++ branches/netp4/src/orxonox/gamestates/GSDedicated.cc 2009-06-07 11:54:02 UTC (rev 3119)
@@ -30,6 +30,7 @@
#include "core/Clock.h"
#include "core/CommandLine.h"
+#include "core/CommandExecutor.h"
#include "core/Game.h"
#include "core/GameMode.h"
#include "core/Iterator.h"
@@ -37,19 +38,38 @@
#include "objects/Tickable.h"
#include "util/Sleep.h"
+#include <iostream>
+#include <iomanip>
+#include <boost/bind.hpp>
+
+
namespace orxonox
{
+ const unsigned int MAX_COMMAND_LENGTH = 255;
+
AddGameState(GSDedicated, "dedicated");
GSDedicated::GSDedicated(const std::string& name)
: GameState(name)
, server_(0)
, timeSinceLastUpdate_(0)
+ , closeThread_(false)
+ , inputIterator_(0)
+ , cleanLine_(true)
{
+ this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this));
+ this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];
+// memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );
}
GSDedicated::~GSDedicated()
{
+ closeThread_ = true;
+#ifndef ORXONOX_PLATFORM_WINDOWS
+ std::cout << "\033[0G\033[K";
+ std::cout.flush();
+#endif
+ //inputThread_->join();
}
void GSDedicated::activate()
@@ -72,20 +92,75 @@
void GSDedicated::update(const Clock& time)
{
-// static float startTime = time.getSecondsPrecise();
-// static int nrOfTicks = 0;
timeSinceLastUpdate_ += time.getDeltaTime();
if (timeSinceLastUpdate_ >= NETWORK_PERIOD)
{
-// ++nrOfTicks;
-// COUT(0) << "estimated ticks/sec: " << nrOfTicks/(time.getSecondsPrecise()-startTime) << endl;
timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
server_->update(time);
}
else
{
- usleep((int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000));
+ usleep((unsigned int)((NETWORK_PERIOD - timeSinceLastUpdate_)*1000*1000 ));
+ usleep(NETWORK_PERIOD*1000*1000); // NOTE: this is to throttle the non-network framerate
// COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl;
}
+ processQueue();
+ printLine();
}
+
+ void GSDedicated::inputThread()
+ {
+ unsigned char c;
+ while(!closeThread_)
+ {
+ c = getchar();
+ {
+// boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
+ if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )
+ continue;
+ this->commandLine_[this->inputIterator_++] = c;
+ if( c == '\n' )
+ {
+ this->cleanLine_ = true;
+ boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);
+ this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );
+ inputIterator_ = 0;
+ }
+ }
+ }
+ }
+
+ void GSDedicated::printLine()
+ {
+#ifndef ORXONOX_PLATFORM_WINDOWS
+ std::cout << "\033[s\033[0G";
+// boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
+ std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, " << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms avg ticktime # ";
+ if ( this->cleanLine_ )
+ this->cleanLine_ = false;
+ else
+ std::cout <<"\033[u";
+ std::cout.flush();
+#endif
+ }
+
+ void GSDedicated::processQueue()
+ {
+ std::string tempstr;
+ {
+ boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_);
+ while(true)
+ {
+ if ( !this->commandQueue_.empty() )
+ {
+ tempstr = this->commandQueue_.front();
+ this->commandQueue_.pop();
+ lock1.unlock();
+ }
+ else
+ break;
+ CommandExecutor::execute(tempstr, true);
+ }
+ }
+ }
}
Modified: branches/netp4/src/orxonox/gamestates/GSDedicated.h
===================================================================
--- branches/netp4/src/orxonox/gamestates/GSDedicated.h 2009-06-03 22:01:16 UTC (rev 3118)
+++ branches/netp4/src/orxonox/gamestates/GSDedicated.h 2009-06-07 11:54:02 UTC (rev 3119)
@@ -32,6 +32,11 @@
#include "OrxonoxPrereqs.h"
#include "core/GameState.h"
#include "network/NetworkPrereqs.h"
+#include <queue>
+#include <cstring>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/recursive_mutex.hpp>
namespace orxonox
{
@@ -46,8 +51,22 @@
void update(const Clock& time);
private:
- Server* server_;
- float timeSinceLastUpdate_;
+ void inputThread();
+ void printLine();
+ void processQueue();
+
+ Server* server_;
+ float timeSinceLastUpdate_;
+
+ boost::thread *inputThread_;
+// boost::recursive_mutex inputLineMutex_;
+ boost::recursive_mutex inputQueueMutex_;
+ bool closeThread_;
+ bool cleanLine_;
+ unsigned char* commandLine_;
+ unsigned int inputIterator_;
+ std::queue<std::string> commandQueue_;
+
};
}
More information about the Orxonox-commit
mailing list