Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3283 was 3283, checked in by rgrieder, 15 years ago

Fixed a bug that caused the game to crash at random locations when shutting it down.

  • Property svn:eol-style set to native
File size: 10.6 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        COUT(0) << "Loading scene in server mode" << std::endl;
88
89        server_->open();
90    }
91
92    void GSDedicated::deactivate()
93    {
94        this->server_->close();
95        delete this->server_;
96       
97        closeThread_ = true;
98#ifndef ORXONOX_PLATFORM_WINDOWS
99        std::cout << "\033[0G\033[K";
100        std::cout.flush();
101        resetTerminalMode();
102        delete this->originalTerminalSettings_;
103#endif
104        COUT(0) << "Press enter to end the game..." << std::endl;
105        inputThread_->join();
106        delete this->inputThread_;
107
108        if (this->commandLine_ )
109          delete[] this->commandLine_;
110
111        GameMode::setHasServer(false);
112    }
113
114    void GSDedicated::update(const Clock& time)
115    {
116        timeSinceLastUpdate_ += time.getDeltaTime();
117        //if (timeSinceLastUpdate_ >= NETWORK_PERIOD)
118        {
119            timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;
120            server_->update(time);
121        }
122        /*else
123        {
124            msleep(static_cast<unsigned int>((NETWORK_PERIOD - timeSinceLastUpdate_)*1000));
125            msleep(static_cast<unsigned int>(NETWORK_PERIOD*1000)); // NOTE: this is to throttle the non-network framerate
126//            COUT(0) << "sleeping for " << (int)((NETWORK_PERIOD - timeSinceLastUpdate_) * 1000 * 1000) << " usec" << endl;
127        }*/
128        processQueue();
129        printLine();
130    }
131   
132    void GSDedicated::inputThread()
133    {
134        unsigned char c;
135        unsigned int  escapeChar=0;
136        while(!closeThread_)
137        {
138            c = getchar();
139            {
140//                 boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
141                if ( inputIterator_>=MAX_COMMAND_LENGTH-1 && c!='\n' )
142                    continue;
143                if( escapeChar > 0 )
144                {
145                    if( c == '[' )
146                    {
147                        escapeChar = 2;
148                        continue;
149                    }
150                    else if ( escapeChar == 2 )
151                    {
152                        switch (c)
153                        {
154                            case 'A': //keyup
155                               
156                                break;
157                            case 'B': //keydown
158                               
159                                break;
160                            case 'C': //keyright
161                                if(cursorX_<inputIterator_)
162                                    ++cursorX_;
163                                break;
164                            case 'D': //keyleft
165                                if(cursorX_>0)
166                                    --cursorX_;
167                                break;
168                            default: //not supported...
169//                                 std::cout << endl << c << endl;
170                                break;
171                        }
172                        escapeChar = 0;
173                    }
174                }
175                else // not in escape sequence mode
176                {
177                    switch (c)
178                    {
179                        case '\n':
180                            this->cleanLine_ = true;
181                            {
182                                boost::recursive_mutex::scoped_lock(this->inputQueueMutex_);
183                                boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
184                                this->commandQueue_.push( std::string((const char*)this->commandLine_,inputIterator_) );
185                            }
186                            memset( this->commandLine_, 0, inputIterator_ );
187                            inputIterator_ = 0;
188                            this->cursorX_ = 0;
189                            this->cursorY_ = 0;
190                            std::cout << endl;
191                            break;
192                        case 127: // backspace
193                        case '\b':
194                            deleteCharacter( this->cursorX_ );
195                            break;
196                        case '\t':
197                        {
198//                             boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
199                            std::cout << endl << CommandExecutor::hint( std::string((const char*)this->commandLine_,inputIterator_) ) << endl;
200                            strncpy((char*)this->commandLine_, CommandExecutor::complete( std::string((const char*)this->commandLine_,inputIterator_) ).c_str(), MAX_COMMAND_LENGTH);
201                            this->inputIterator_ = strlen((const char*)this->commandLine_);
202                            this->cursorX_ = this->inputIterator_;
203                            break;
204                        }
205                        case '\033': // 1. escape character
206                            escapeChar = 1;
207                            break;
208                        default:
209                            insertCharacter( this->cursorX_, c );
210                            break;
211                    }
212                }
213            }
214        }
215    }
216   
217    void GSDedicated::printLine()
218    {
219#ifndef ORXONOX_PLATFORM_WINDOWS
220        // set cursor to the begining of the line and erase the line
221        std::cout << "\033[0G\033[K";
222//         boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
223        // print status line
224        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 # ";
225        //save cursor position
226        std::cout << "\033[s";
227        //print commandLine buffer
228        std::cout << std::string((const char*)this->commandLine_, inputIterator_);
229        //restore cursor position and move it cursorX_ to the right
230        std::cout << "\033[u";
231        if( this->cursorX_ > 0 )
232            std::cout << "\033[" << this->cursorX_ << "C";
233        std::cout.flush();
234#endif
235    }
236   
237    void GSDedicated::processQueue()
238    {
239        std::string tempstr;
240        {
241            boost::recursive_mutex::scoped_lock lock1(this->inputQueueMutex_);
242            while(true)
243            {
244                if ( !this->commandQueue_.empty() )
245                {
246                    tempstr = this->commandQueue_.front();
247                    this->commandQueue_.pop();
248                    lock1.unlock();
249                }
250                else
251                    break;
252                CommandExecutor::execute(tempstr, true);
253            }
254        }
255    }
256   
257    void GSDedicated::setTerminalMode()
258    {
259#ifndef ORXONOX_PLATFORM_WINDOWS
260        termios new_settings;
261     
262        tcgetattr(0,this->originalTerminalSettings_);
263        new_settings = *this->originalTerminalSettings_;
264        new_settings.c_lflag &= ~( ICANON | ECHO );
265//         new_settings.c_lflag |= ( ISIG | IEXTEN );
266        new_settings.c_cc[VTIME] = 0;
267        new_settings.c_cc[VMIN] = 1;
268        tcsetattr(0,TCSANOW,&new_settings);
269        COUT(0) << endl;
270//       atexit(&GSDedicated::resetTerminalMode);
271#endif
272    }
273   
274    void GSDedicated::resetTerminalMode()
275    {
276#ifndef ORXONOX_PLATFORM_WINDOWS
277        tcsetattr(0, TCSANOW, GSDedicated::originalTerminalSettings_);
278#endif
279    }
280   
281    void GSDedicated::insertCharacter( unsigned int position, char c )
282    {
283//         std::cout << endl << (unsigned int)c << endl;
284        // check that we do not exceed MAX_COMMAND_LENGTH
285        if( inputIterator_+1 < MAX_COMMAND_LENGTH )
286        {
287            // if cursor not at end of line then move the rest of the line
288            if( position != this->inputIterator_ )
289                    memmove( this->commandLine_+position+1, this->commandLine_+position, this->inputIterator_-position);
290//             boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
291            this->commandLine_[position] = c;
292            ++this->cursorX_;
293            ++this->inputIterator_;
294        }
295    }
296    void GSDedicated::deleteCharacter( unsigned int position )
297    {
298//         boost::recursive_mutex::scoped_lock(this->inputLineMutex_);
299        if ( this->inputIterator_>0 && position>0 )
300        {
301            if ( position != this->inputIterator_ )
302                memmove( this->commandLine_+position-1, this->commandLine_+position, this->inputIterator_-position);
303            --this->cursorX_;
304            --this->inputIterator_;
305        }
306    }
307   
308}
Note: See TracBrowser for help on using the repository browser.