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 "core/Clock.h" |
---|
32 | #include "core/CommandLine.h" |
---|
33 | #include "core/CommandExecutor.h" |
---|
34 | #include "core/Game.h" |
---|
35 | #include "core/GameMode.h" |
---|
36 | #include "core/Iterator.h" |
---|
37 | #include "network/Server.h" |
---|
38 | #include "objects/Tickable.h" |
---|
39 | #include "util/Sleep.h" |
---|
40 | |
---|
41 | #include <iostream> |
---|
42 | #include <iomanip> |
---|
43 | #include <boost/bind.hpp> |
---|
44 | |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | const unsigned int MAX_COMMAND_LENGTH = 255; |
---|
49 | |
---|
50 | AddGameState(GSDedicated, "dedicated"); |
---|
51 | |
---|
52 | GSDedicated::GSDedicated(const std::string& name) |
---|
53 | : GameState(name) |
---|
54 | , server_(0) |
---|
55 | , timeSinceLastUpdate_(0) |
---|
56 | , closeThread_(false) |
---|
57 | , inputIterator_(0) |
---|
58 | , cleanLine_(true) |
---|
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 ); |
---|
63 | } |
---|
64 | |
---|
65 | GSDedicated::~GSDedicated() |
---|
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(); |
---|
73 | } |
---|
74 | |
---|
75 | void GSDedicated::activate() |
---|
76 | { |
---|
77 | GameMode::setHasServer(true); |
---|
78 | |
---|
79 | this->server_ = new Server(CommandLine::getValue("port")); |
---|
80 | COUT(0) << "Loading scene in server mode" << std::endl; |
---|
81 | |
---|
82 | server_->open(); |
---|
83 | } |
---|
84 | |
---|
85 | void GSDedicated::deactivate() |
---|
86 | { |
---|
87 | this->server_->close(); |
---|
88 | delete this->server_; |
---|
89 | |
---|
90 | GameMode::setHasServer(false); |
---|
91 | } |
---|
92 | |
---|
93 | void GSDedicated::update(const Clock& time) |
---|
94 | { |
---|
95 | timeSinceLastUpdate_ += time.getDeltaTime(); |
---|
96 | if (timeSinceLastUpdate_ >= NETWORK_PERIOD) |
---|
97 | { |
---|
98 | timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; |
---|
99 | server_->update(time); |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | usleep((unsigned int)((NETWORK_PERIOD - timeSinceLastUpdate_)*1000*1000 )); |
---|
104 | usleep(NETWORK_PERIOD*1000*1000); // NOTE: this is to throttle the non-network framerate |
---|
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 | } |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|