Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/gamestates/GSDedicated.cc @ 3263

Last change on this file since 3263 was 3263, checked in by scheusso, 15 years ago

fix for tab completion in dedicated server console

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "GSDedicated.h"
30
31#include "util/Debug.h"
32#include "util/Sleep.h"
33#include "core/Clock.h"
34#include "core/CommandLine.h"
35#include "core/CommandExecutor.h"
36#include "core/Game.h"
37#include "core/GameMode.h"
38#include "network/Server.h"
39
40#include <iostream>
41#include <iomanip>
42#include <boost/bind.hpp>
43
44#ifndef ORXONOX_PLATFORM_WINDOWS
45#include <termios.h>
46#endif
47
48
49namespace orxonox
50{
51    const unsigned int MAX_COMMAND_LENGTH = 255;
52   
53    AddGameState(GSDedicated, "dedicated");
54   
55    termios* GSDedicated::originalTerminalSettings_;
56
57    GSDedicated::GSDedicated(const std::string& name)
58        : GameState(name)
59        , server_(0)
60        , timeSinceLastUpdate_(0)
61        , closeThread_(false)
62        , cleanLine_(true)
63        , inputIterator_(0)
64        , cursorX_(0)
65        , cursorY_(0)
66    {
67        this->commandLine_ = new unsigned char[MAX_COMMAND_LENGTH];
68//         memset( this->commandLine_, 0, MAX_COMMAND_LENGTH );
69    }
70
71    GSDedicated::~GSDedicated()
72    {
73    }
74
75    void GSDedicated::activate()
76    {
77        GameMode::setHasServer(true);
78       
79        this->inputThread_ = new boost::thread(boost::bind(&GSDedicated::inputThread, this));
80       
81#ifndef ORXONOX_PLATFORM_WINDOWS
82        this->originalTerminalSettings_ = new termios;
83        this->setTerminalMode();
84#endif
85
86        this->server_ = new Server(CommandLine::getValue("port"));
87        if( this->commandLine_ )
88          delete[] this->commandLine_;
89        COUT(0) << "Loading scene in server mode" << std::endl;
90
91        server_->open();
92    }
93
94    void GSDedicated::deactivate()
95    {
96        this->server_->close();
97        delete this->server_;
98       
99        closeThread_ = true;
100#ifndef ORXONOX_PLATFORM_WINDOWS
101        std::cout << "\033[0G\033[K";
102        std::cout.flush();
103        resetTerminalMode();
104        delete this->originalTerminalSettings_;
105#endif
106        //inputThread_->join();
107        delete this->inputThread_;
108
109        GameMode::setHasServer(false);
110    }
111
112    void GSDedicated::update(const Clock& time)
113    {
114        timeSinceLastUpdate_ += time.getDeltaTime();
115        //if (timeSinceLastUpdate_ >= NETWORK_PERIOD)
116        {
117            timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
118            server_->update(time);
119        }
120        /*else
121        {
122            msleep(static_cast<unsigned int>((NETWORK_PERIOD - timeSinceLastUpdate_)*1000));
123            msleep(static_cast<unsigned int>(NETWORK_PERIOD*1000)); // NOTE: this is to throttle the non-network framerate
124//            COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl;
125        }*/
126        processQueue();
127        printLine();
128    }
129   
130    void GSDedicated::inputThread()
131    {
132        unsigned char c;
133        unsigned int  escapeChar=0;
134        while(!closeThread_)
135        {
136            c = getchar();
137            {
138//                 boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
139                if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )
140                    continue;
141                if( escapeChar > 0 )
142                {
143                    if( c == '[' )
144                    {
145                        escapeChar = 2;
146                        continue;
147                    }
148                    else if ( escapeChar == 2 )
149                    {
150                        switch (c)
151                        {
152                            case 'A': //keyup
153                               
154                                break;
155                            case 'B': //keydown
156                               
157                                break;
158                            case 'C': //keyright
159                                if(cursorX_<inputIterator_)
160                                    ++cursorX_;
161                                break;
162                            case 'D': //keyleft
163                                if(cursorX_>0)
164                                    --cursorX_;
165                                break;
166                            default: //not supported...
167//                                 std::cout << endl << c << endl;
168                                break;
169                        }
170                        escapeChar = 0;
171                    }
172                }
173                else // not in escape sequence mode
174                {
175                    switch (c)
176                    {
177                        case '\n':
178                            this->cleanLine_ = true;
179                            {
180                                boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);
181                                boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
182                                this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );
183                            }
184                            memset( this->commandLine_, 0, inputIterator_ );
185                            inputIterator_ = 0;
186                            this->cursorX_ = 0;
187                            this->cursorY_ = 0;
188                            std::cout << endl;
189                            break;
190                        case 127: // backspace
191                        case '\b':
192                            deleteCharacter( this->cursorX_ );
193                            break;
194                        case '\t':
195                        {
196//                             boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
197                            std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;
198                            strncpy((char*)this->commandLine_, CommandExecutor::complete( std::string((const char*)this->commandLine_,inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);
199                            this->inputIterator_ = strlen((const char*)this->commandLine_);
200                            this->cursorX_ = this->inputIterator_;
201                            break;
202                        }
203                        case '\033': // 1. escape character
204                            escapeChar = 1;
205                            break;
206                        default:
207                            insertCharacter( this->cursorX_, c );
208                            break;
209                    }
210                }
211            }
212        }
213    }
214   
215    void GSDedicated::printLine()
216    {
217#ifndef ORXONOX_PLATFORM_WINDOWS
218        // set cursor to the begining of the line and erase the line
219        std::cout << "\033[0G\033[K";
220//         boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
221        // print status line
222        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 position
224        std::cout << "\033[s";
225        //print commandLine buffer
226        std::cout << std::string((const char*)this->commandLine_, inputIterator_);
227        //restore cursor position and move it cursorX_ to the right
228        std::cout << "\033[u";
229        if( this->cursorX_ > 0 )
230            std::cout << "\033[" << this->cursorX_ << "C";
231        std::cout.flush();
232#endif
233    }
234   
235    void GSDedicated::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                else
249                    break;
250                CommandExecutor::execute(tempstr, true);
251            }
252        }
253    }
254   
255    void GSDedicated::setTerminalMode()
256    {
257#ifndef ORXONOX_PLATFORM_WINDOWS
258        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] = 0;
265        new_settings.c_cc[VMIN] = 1;
266        tcsetattr(0,TCSANOW,&new_settings);
267        COUT(0) << endl;
268//       atexit(&GSDedicated::resetTerminalMode);
269#endif
270    }
271   
272    void GSDedicated::resetTerminalMode()
273    {
274#ifndef ORXONOX_PLATFORM_WINDOWS
275        tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_);
276#endif
277    }
278   
279    void GSDedicated::insertCharacter( unsigned int position, char c )
280    {
281//         std::cout << endl << (unsigned int)c << endl;
282        // check that we do not exceed MAX_COMMAND_LENGTH
283        if( inputIterator_+1 < MAX_COMMAND_LENGTH )
284        {
285            // if cursor not at end of line then move the rest of the line
286            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 GSDedicated::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}
Note: See TracBrowser for help on using the repository browser.