Changeset 6016
- Timestamp:
- Nov 2, 2009, 6:06:25 PM (15 years ago)
- Location:
- code/branches/console/src/orxonox/gamestates
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/console/src/orxonox/gamestates/GSDedicated.cc
r5929 r6016 29 29 #include "GSDedicated.h" 30 30 31 #include <iomanip>32 #include <iostream>33 #include <boost/bind.hpp>34 35 #include "util/Clock.h"36 31 #include "util/Debug.h" 37 #include "util/Sleep.h"38 32 #include "core/CommandLine.h" 39 #include "core/CommandExecutor.h"40 33 #include "core/Game.h" 41 34 #include "core/GameMode.h" 42 35 #include "network/Server.h" 43 36 44 #ifdef ORXONOX_PLATFORM_UNIX45 #include <termios.h>46 #endif47 48 49 37 namespace orxonox 50 38 { 51 const unsigned int MAX_COMMAND_LENGTH = 255;52 53 39 DeclareGameState(GSDedicated, "dedicated", false, false); 54 40 55 termios* GSDedicated::originalTerminalSettings_;56 57 41 GSDedicated::GSDedicated(const GameStateInfo& info) 58 42 : GameState(info) 59 43 , server_(0) 60 , closeThread_(false)61 , cleanLine_(true)62 , inputIterator_(0)63 , cursorX_(0)64 , cursorY_(0)65 44 { 66 45 } … … 73 52 { 74 53 GameMode::setHasServer(true); 75 76 this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this));77 78 #ifndef ORXONOX_PLATFORM_WINDOWS79 this->originalTerminalSettings_ = new termios;80 this->setTerminalMode();81 #endif82 54 83 55 this->server_ = new Server(CommandLine::getValue("port")); … … 91 63 this->server_->close(); 92 64 delete this->server_; 93 94 closeThread_ = true;95 #ifdef ORXONOX_PLATFORM_UNIX96 std::cout << "\033[0G\033[K";97 std::cout.flush();98 resetTerminalMode();99 delete this->originalTerminalSettings_;100 #else101 COUT(0) << "Press enter to end the game..." << std::endl;102 #endif103 inputThread_->join();104 delete this->inputThread_;105 65 106 66 GameMode::setHasServer(false); … … 110 70 { 111 71 server_->update(time); 112 processQueue();113 printLine();114 72 } 115 116 void GSDedicated::inputThread()117 {118 this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];119 // memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );120 unsigned char c;121 unsigned int escapeChar=0;122 while(!closeThread_)123 {124 #ifdef ORXONOX_PLATFORM_UNIX125 size_t count = read(STDIN_FILENO, &c, 1);126 if (count == 1)127 #else128 c = getchar();129 #endif130 {131 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);132 if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )133 continue;134 if( escapeChar > 0 )135 {136 if( c == '[' )137 {138 escapeChar = 2;139 continue;140 }141 else if ( escapeChar == 2 )142 {143 switch (c)144 {145 case 'A': //keyup146 147 break;148 case 'B': //keydown149 150 break;151 case 'C': //keyright152 if(cursorX_<inputIterator_)153 ++cursorX_;154 break;155 case 'D': //keyleft156 if(cursorX_>0)157 --cursorX_;158 break;159 default: //not supported...160 // std::cout << endl << c << endl;161 break;162 }163 escapeChar = 0;164 }165 }166 else // not in escape sequence mode167 {168 switch (c)169 {170 case '\n':171 this->cleanLine_ = true;172 {173 boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);174 boost::recursive_mutex::scoped_lock(this->inputLineMutex_);175 this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );176 }177 memset( this->commandLine_, 0, inputIterator_ );178 inputIterator_ = 0;179 this->cursorX_ = 0;180 this->cursorY_ = 0;181 std::cout << endl;182 break;183 case 127: // backspace184 case '\b':185 deleteCharacter( this->cursorX_ );186 break;187 case '\t':188 {189 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);190 std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;191 strncpy(reinterpret_cast<char*>(this->commandLine_), CommandExecutor::complete( std::string(reinterpret_cast<char*>(this->commandLine_),inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);192 this->inputIterator_ = strlen((const char*)this->commandLine_);193 this->cursorX_ = this->inputIterator_;194 break;195 }196 case '\033': // 1. escape character197 escapeChar = 1;198 break;199 default:200 insertCharacter( this->cursorX_, c );201 break;202 }203 }204 }205 }206 207 delete[] this->commandLine_;208 }209 210 void GSDedicated::printLine()211 {212 #ifdef ORXONOX_PLATFORM_UNIX213 // set cursor to the begining of the line and erase the line214 std::cout << "\033[0G\033[K";215 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);216 // print status line217 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 # ";218 //save cursor position219 std::cout << "\033[s";220 //print commandLine buffer221 std::cout << std::string((const char*)this->commandLine_, inputIterator_);222 //restore cursor position and move it cursorX_ to the right223 std::cout << "\033[u";224 if( this->cursorX_ > 0 )225 std::cout << "\033[" << this->cursorX_ << "C";226 std::cout.flush();227 #endif228 }229 230 void GSDedicated::processQueue()231 {232 std::string tempstr;233 {234 boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_);235 while(true)236 {237 if ( !this->commandQueue_.empty() )238 {239 tempstr = this->commandQueue_.front();240 this->commandQueue_.pop();241 lock1.unlock();242 }243 else244 break;245 CommandExecutor::execute(tempstr, true);246 }247 }248 }249 250 void GSDedicated::setTerminalMode()251 {252 #ifdef ORXONOX_PLATFORM_UNIX253 termios new_settings;254 255 tcgetattr(0,this->originalTerminalSettings_);256 new_settings = *this->originalTerminalSettings_;257 new_settings.c_lflag &= ~( ICANON | ECHO );258 // new_settings.c_lflag |= ( ISIG | IEXTEN );259 new_settings.c_cc[VTIME] = 1;260 new_settings.c_cc[VMIN] = 0;261 tcsetattr(0,TCSANOW,&new_settings);262 COUT(0) << endl;263 // atexit(&GSDedicated::resetTerminalMode);264 #endif265 }266 267 void GSDedicated::resetTerminalMode()268 {269 #ifdef ORXONOX_PLATFORM_UNIX270 tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_);271 #endif272 }273 274 void GSDedicated::insertCharacter( unsigned int position, char c )275 {276 // std::cout << endl << static_cast<unsigned int>(c) << endl;277 // check that we do not exceed MAX_COMMAND_LENGTH278 if( inputIterator_+1 < MAX_COMMAND_LENGTH )279 {280 // if cursor not at end of line then move the rest of the line281 if( position != this->inputIterator_ )282 memmove( this->commandLine_+position+1, this->commandLine_+position, this->inputIterator_-position);283 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);284 this->commandLine_[position] = c;285 ++this->cursorX_;286 ++this->inputIterator_;287 }288 }289 void GSDedicated::deleteCharacter( unsigned int position )290 {291 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);292 if ( this->inputIterator_>0 && position>0 )293 {294 if ( position != this->inputIterator_ )295 memmove( this->commandLine_+position-1, this->commandLine_+position, this->inputIterator_-position);296 --this->cursorX_;297 --this->inputIterator_;298 }299 }300 301 73 } -
code/branches/console/src/orxonox/gamestates/GSDedicated.h
r5929 r6016 32 32 #include "OrxonoxPrereqs.h" 33 33 34 #include <cstring>35 #include <queue>36 #include <boost/thread/thread.hpp>37 #include <boost/thread/mutex.hpp>38 #include <boost/thread/recursive_mutex.hpp>39 40 34 #include "core/GameState.h" 41 35 #include "network/NetworkPrereqs.h" 42 36 43 struct termios;44 45 37 namespace orxonox 46 38 { 47 48 39 class _OrxonoxExport GSDedicated : public GameState 49 40 { … … 57 48 58 49 private: 59 void inputThread();60 void printLine();61 void processQueue();62 void setTerminalMode();63 static void resetTerminalMode();64 65 void insertCharacter( unsigned int position, char c );66 void deleteCharacter( unsigned int position );67 68 50 Server* server_; 69 70 boost::thread *inputThread_;71 boost::recursive_mutex inputLineMutex_;72 boost::recursive_mutex inputQueueMutex_;73 bool closeThread_;74 bool cleanLine_;75 unsigned char* commandLine_;76 unsigned int inputIterator_;77 std::queue<std::string> commandQueue_;78 static termios* originalTerminalSettings_;79 80 unsigned int cursorX_;81 unsigned int cursorY_;82 51 }; 83 52 } -
code/branches/console/src/orxonox/gamestates/GSDedicatedClient.cc
r5929 r6016 29 29 #include "GSDedicatedClient.h" 30 30 31 #include <iomanip>32 #include <iostream>33 #include <boost/bind.hpp>34 35 #include "util/Clock.h"36 31 #include "util/Debug.h" 37 32 #include "util/Exception.h" 38 #include "util/Sleep.h"39 33 #include "core/CommandLine.h" 40 #include "core/CommandExecutor.h"41 34 #include "core/Game.h" 42 35 #include "core/GameMode.h" 43 36 #include "network/Client.h" 44 37 45 #ifdef ORXONOX_PLATFORM_UNIX46 #include <termios.h>47 #endif48 49 50 38 namespace orxonox 51 39 { 52 const unsigned int MAX_COMMAND_LENGTH = 255;53 54 40 DeclareGameState(GSDedicatedClient, "dedicatedClient", false, false); 55 41 56 termios* GSDedicatedClient::originalTerminalSettings_;57 58 42 GSDedicatedClient::GSDedicatedClient(const GameStateInfo& info) 59 43 : GameState(info) 60 44 , client_(0) 61 , closeThread_(false)62 , cleanLine_(true)63 , inputIterator_(0)64 , cursorX_(0)65 , cursorY_(0)66 45 { 67 46 } … … 73 52 void GSDedicatedClient::activate() 74 53 { 75 this->inputThread_ = new boost::thread(boost::bind(&GSDedicatedClient::inputThread, this)); 76 77 #ifndef ORXONOX_PLATFORM_WINDOWS 78 this->originalTerminalSettings_ = new termios; 79 this->setTerminalMode(); 80 #endif 54 GameMode::setIsClient(true); 81 55 82 56 this->client_ = new Client(CommandLine::getValue("ip").getString(), CommandLine::getValue("port")); … … 87 61 88 62 client_->update(Game::getInstance().getGameClock()); 89 90 91 63 } 92 64 93 65 void GSDedicatedClient::deactivate() 94 66 { 95 if ( this->client_)67 if (this->client_) 96 68 { 97 69 this->client_->closeConnection(); 98 70 delete this->client_; 99 71 } 100 101 closeThread_ = true; 102 #ifdef ORXONOX_PLATFORM_UNIX 103 std::cout << "\033[0G\033[K"; 104 std::cout.flush(); 105 resetTerminalMode(); 106 delete this->originalTerminalSettings_; 107 #else 108 COUT(0) << "Press enter to end the game..." << std::endl; 109 #endif 110 inputThread_->join(); 111 delete this->inputThread_; 72 73 GameMode::setIsClient(false); 112 74 } 113 75 … … 115 77 { 116 78 client_->update(time); 117 processQueue();118 printLine();119 79 } 120 121 void GSDedicatedClient::inputThread()122 {123 this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];124 // memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );125 unsigned char c;126 unsigned int escapeChar=0;127 while(!closeThread_)128 {129 #ifdef ORXONOX_PLATFORM_UNIX130 size_t count = read(STDIN_FILENO, &c, 1);131 if (count == 1)132 #else133 c = getchar();134 #endif135 {136 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);137 if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )138 continue;139 if( escapeChar > 0 )140 {141 if( c == '[' )142 {143 escapeChar = 2;144 continue;145 }146 else if ( escapeChar == 2 )147 {148 switch (c)149 {150 case 'A': //keyup151 152 break;153 case 'B': //keydown154 155 break;156 case 'C': //keyright157 if(cursorX_<inputIterator_)158 ++cursorX_;159 break;160 case 'D': //keyleft161 if(cursorX_>0)162 --cursorX_;163 break;164 default: //not supported...165 // std::cout << endl << c << endl;166 break;167 }168 escapeChar = 0;169 }170 }171 else // not in escape sequence mode172 {173 switch (c)174 {175 case '\n':176 this->cleanLine_ = true;177 {178 boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);179 boost::recursive_mutex::scoped_lock(this->inputLineMutex_);180 this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );181 }182 memset( this->commandLine_, 0, inputIterator_ );183 inputIterator_ = 0;184 this->cursorX_ = 0;185 this->cursorY_ = 0;186 std::cout << endl;187 break;188 case 127: // backspace189 case '\b':190 deleteCharacter( this->cursorX_ );191 break;192 case '\t':193 {194 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);195 std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;196 strncpy(reinterpret_cast<char*>(this->commandLine_), CommandExecutor::complete( std::string(reinterpret_cast<char*>(this->commandLine_),inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);197 this->inputIterator_ = strlen((const char*)this->commandLine_);198 this->cursorX_ = this->inputIterator_;199 break;200 }201 case '\033': // 1. escape character202 escapeChar = 1;203 break;204 default:205 insertCharacter( this->cursorX_, c );206 break;207 }208 }209 }210 }211 212 delete[] this->commandLine_;213 }214 215 void GSDedicatedClient::printLine()216 {217 #ifdef ORXONOX_PLATFORM_UNIX218 // set cursor to the begining of the line and erase the line219 std::cout << "\033[0G\033[K";220 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);221 // print status line222 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 # ";223 //save cursor position224 std::cout << "\033[s";225 //print commandLine buffer226 std::cout << std::string((const char*)this->commandLine_, inputIterator_);227 //restore cursor position and move it cursorX_ to the right228 std::cout << "\033[u";229 if( this->cursorX_ > 0 )230 std::cout << "\033[" << this->cursorX_ << "C";231 std::cout.flush();232 #endif233 }234 235 void GSDedicatedClient::processQueue()236 {237 std::string tempstr;238 {239 boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_);240 while(true)241 {242 if ( !this->commandQueue_.empty() )243 {244 tempstr = this->commandQueue_.front();245 this->commandQueue_.pop();246 lock1.unlock();247 }248 else249 break;250 CommandExecutor::execute(tempstr, true);251 }252 }253 }254 255 void GSDedicatedClient::setTerminalMode()256 {257 #ifdef ORXONOX_PLATFORM_UNIX258 termios new_settings;259 260 tcgetattr(0,this->originalTerminalSettings_);261 new_settings = *this->originalTerminalSettings_;262 new_settings.c_lflag &= ~( ICANON | ECHO );263 // new_settings.c_lflag |= ( ISIG | IEXTEN );264 new_settings.c_cc[VTIME] = 1;265 new_settings.c_cc[VMIN] = 0;266 tcsetattr(0,TCSANOW,&new_settings);267 COUT(0) << endl;268 // atexit(&GSDedicatedClient::resetTerminalMode);269 #endif270 }271 272 void GSDedicatedClient::resetTerminalMode()273 {274 #ifdef ORXONOX_PLATFORM_UNIX275 tcsetattr(0, TCSANOW, GSDedicatedClient::originalTerminalSettings_);276 #endif277 }278 279 void GSDedicatedClient::insertCharacter( unsigned int position, char c )280 {281 // std::cout << endl << static_cast<unsigned int>(c) << endl;282 // check that we do not exceed MAX_COMMAND_LENGTH283 if( inputIterator_+1 < MAX_COMMAND_LENGTH )284 {285 // if cursor not at end of line then move the rest of the line286 if( position != this->inputIterator_ )287 memmove( this->commandLine_+position+1, this->commandLine_+position, this->inputIterator_-position);288 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);289 this->commandLine_[position] = c;290 ++this->cursorX_;291 ++this->inputIterator_;292 }293 }294 void GSDedicatedClient::deleteCharacter( unsigned int position )295 {296 // boost::recursive_mutex::scoped_lock(this->inputLineMutex_);297 if ( this->inputIterator_>0 && position>0 )298 {299 if ( position != this->inputIterator_ )300 memmove( this->commandLine_+position-1, this->commandLine_+position, this->inputIterator_-position);301 --this->cursorX_;302 --this->inputIterator_;303 }304 }305 306 80 } -
code/branches/console/src/orxonox/gamestates/GSDedicatedClient.h
r5929 r6016 34 34 #include "core/GameState.h" 35 35 #include "network/NetworkPrereqs.h" 36 #include <queue>37 #include <cstring>38 #include <boost/thread/thread.hpp>39 #include <boost/thread/mutex.hpp>40 #include <boost/thread/recursive_mutex.hpp>41 42 struct termios;43 36 44 37 namespace orxonox … … 56 49 57 50 private: 58 void inputThread();59 void printLine();60 void processQueue();61 void setTerminalMode();62 static void resetTerminalMode();63 64 void insertCharacter( unsigned int position, char c );65 void deleteCharacter( unsigned int position );66 67 51 Client* client_; 68 69 boost::thread *inputThread_;70 boost::recursive_mutex inputLineMutex_;71 boost::recursive_mutex inputQueueMutex_;72 bool closeThread_;73 bool cleanLine_;74 unsigned char* commandLine_;75 unsigned int inputIterator_;76 std::queue<std::string> commandQueue_;77 static termios* originalTerminalSettings_;78 79 unsigned int cursorX_;80 unsigned int cursorY_;81 52 }; 82 53 }
Note: See TracChangeset
for help on using the changeset viewer.