Changeset 3119 for code/branches
- Timestamp:
- Jun 7, 2009, 1:54:02 PM (15 years ago)
- Location:
- code/branches/netp4/src/orxonox/gamestates
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/netp4/src/orxonox/gamestates/GSDedicated.cc
r3110 r3119 31 31 #include "core/Clock.h" 32 32 #include "core/CommandLine.h" 33 #include "core/CommandExecutor.h" 33 34 #include "core/Game.h" 34 35 #include "core/GameMode.h" … … 38 39 #include "util/Sleep.h" 39 40 41 #include <iostream> 42 #include <iomanip> 43 #include <boost/bind.hpp> 44 45 40 46 namespace orxonox 41 47 { 48 const unsigned int MAX_COMMAND_LENGTH = 255; 49 42 50 AddGameState(GSDedicated, "dedicated"); 43 51 … … 46 54 , server_(0) 47 55 , timeSinceLastUpdate_(0) 56 , closeThread_(false) 57 , inputIterator_(0) 58 , cleanLine_(true) 48 59 { 60 this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this)); 61 this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH]; 62 // memset( this->commandLine_, 0, MAX_COMMAND_LENGTH ); 49 63 } 50 64 51 65 GSDedicated::~GSDedicated() 52 66 { 67 closeThread_ = true; 68 #ifndef ORXONOX_PLATFORM_WINDOWS 69 std::cout << "\033[0G\033[K"; 70 std::cout.flush(); 71 #endif 72 //inputThread_->join(); 53 73 } 54 74 … … 73 93 void GSDedicated::update(const Clock& time) 74 94 { 75 // static float startTime = time.getSecondsPrecise();76 // static int nrOfTicks = 0;77 95 timeSinceLastUpdate_ += time.getDeltaTime(); 78 96 if (timeSinceLastUpdate_ >= NETWORK_PERIOD) 79 97 { 80 // ++nrOfTicks;81 // COUT(0) << "estimated ticks/sec: " << nrOfTicks/(time.getSecondsPrecise()-startTime) << endl;82 98 timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; 83 99 server_->update(time); … … 85 101 else 86 102 { 87 usleep((int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000)); 103 usleep((unsigned int)((NETWORK_PERIOD - timeSinceLastUpdate_)*1000*1000 )); 104 usleep(NETWORK_PERIOD*1000*1000); // NOTE: this is to throttle the non-network framerate 88 105 // COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl; 106 } 107 processQueue(); 108 printLine(); 109 } 110 111 void GSDedicated::inputThread() 112 { 113 unsigned char c; 114 while(!closeThread_) 115 { 116 c = getchar(); 117 { 118 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); 119 if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' ) 120 continue; 121 this->commandLine_[this->inputIterator_++] = c; 122 if( c == '\n' ) 123 { 124 this->cleanLine_ = true; 125 boost::recursive_mutex::scoped_lock(this->inputQueueMutex_); 126 this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) ); 127 inputIterator_ = 0; 128 } 129 } 130 } 131 } 132 133 void GSDedicated::printLine() 134 { 135 #ifndef ORXONOX_PLATFORM_WINDOWS 136 std::cout << "\033[s\033[0G"; 137 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_); 138 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 # "; 139 if ( this->cleanLine_ ) 140 this->cleanLine_ = false; 141 else 142 std::cout <<"\033[u"; 143 std::cout.flush(); 144 #endif 145 } 146 147 void GSDedicated::processQueue() 148 { 149 std::string tempstr; 150 { 151 boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_); 152 while(true) 153 { 154 if ( !this->commandQueue_.empty() ) 155 { 156 tempstr = this->commandQueue_.front(); 157 this->commandQueue_.pop(); 158 lock1.unlock(); 159 } 160 else 161 break; 162 CommandExecutor::execute(tempstr, true); 163 } 89 164 } 90 165 } -
code/branches/netp4/src/orxonox/gamestates/GSDedicated.h
r2896 r3119 33 33 #include "core/GameState.h" 34 34 #include "network/NetworkPrereqs.h" 35 #include <queue> 36 #include <cstring> 37 #include <boost/thread/thread.hpp> 38 #include <boost/thread/mutex.hpp> 39 #include <boost/thread/recursive_mutex.hpp> 35 40 36 41 namespace orxonox … … 47 52 48 53 private: 49 Server* server_; 50 float timeSinceLastUpdate_; 54 void inputThread(); 55 void printLine(); 56 void processQueue(); 57 58 Server* server_; 59 float timeSinceLastUpdate_; 60 61 boost::thread *inputThread_; 62 // boost::recursive_mutex inputLineMutex_; 63 boost::recursive_mutex inputQueueMutex_; 64 bool closeThread_; 65 bool cleanLine_; 66 unsigned char* commandLine_; 67 unsigned int inputIterator_; 68 std::queue<std::string> commandQueue_; 69 51 70 }; 52 71 }
Note: See TracChangeset
for help on using the changeset viewer.