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 | * Oliver Scheuss |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | // |
---|
30 | // C++ Implementation: Client |
---|
31 | // |
---|
32 | // Description: |
---|
33 | // |
---|
34 | // |
---|
35 | // Author: Oliver Scheuss, (C) 2007 |
---|
36 | // |
---|
37 | // Copyright: See COPYING file that comes with this distribution |
---|
38 | // |
---|
39 | // |
---|
40 | |
---|
41 | #include "Client.h" |
---|
42 | |
---|
43 | #include <cassert> |
---|
44 | |
---|
45 | #include "util/Clock.h" |
---|
46 | #include "util/Output.h" |
---|
47 | #include "util/ScopedSingletonManager.h" |
---|
48 | #include "synchronisable/Synchronisable.h" |
---|
49 | #include "packet/Chat.h" |
---|
50 | #include "packet/Gamestate.h" |
---|
51 | #include "FunctionCallManager.h" |
---|
52 | #include "core/CoreIncludes.h" |
---|
53 | #include "core/CommandLineParser.h" |
---|
54 | #include "core/Game.h" |
---|
55 | |
---|
56 | namespace orxonox |
---|
57 | { |
---|
58 | |
---|
59 | ManageScopedSingleton( Client, ScopeID::Root, true ); |
---|
60 | |
---|
61 | /** |
---|
62 | * Constructor for the Client class |
---|
63 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
64 | */ |
---|
65 | Client::Client(): |
---|
66 | isSynched_(false), |
---|
67 | gameStateFailure_(false), |
---|
68 | timeSinceLastUpdate_(0) |
---|
69 | { |
---|
70 | this->setDestination( CommandLineParser::getValue("dest").getString(), CommandLineParser::getValue("port") ); |
---|
71 | } |
---|
72 | |
---|
73 | Client::~Client() |
---|
74 | { |
---|
75 | if ( ClientConnection::isConnected() ) |
---|
76 | closeConnection(); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Establish the Connection to the Server |
---|
81 | * @return true/false |
---|
82 | */ |
---|
83 | bool Client::establishConnection() |
---|
84 | { |
---|
85 | Synchronisable::setClient(true); |
---|
86 | if( ClientConnection::establishConnection() ) |
---|
87 | { |
---|
88 | Host::setActive(true); |
---|
89 | GamestateManager::addPeer(NETWORK_PEER_ID_SERVER); |
---|
90 | return true; |
---|
91 | } |
---|
92 | else |
---|
93 | return false; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * closes the Connection to the Server |
---|
98 | * @return true/false |
---|
99 | */ |
---|
100 | bool Client::closeConnection() |
---|
101 | { |
---|
102 | Host::setActive(false); |
---|
103 | GamestateManager::removePeer(NETWORK_PEER_ID_SERVER); |
---|
104 | return ClientConnection::closeConnection(); |
---|
105 | } |
---|
106 | |
---|
107 | void Client::setDestination(const std::string& serverAddress, unsigned int port) |
---|
108 | { |
---|
109 | ClientConnection::setServerAddress(serverAddress); |
---|
110 | ClientConnection::setPort(port); |
---|
111 | } |
---|
112 | |
---|
113 | void Client::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) |
---|
114 | { |
---|
115 | ClientConnection::addPacket(packet, channelID); |
---|
116 | } |
---|
117 | |
---|
118 | bool Client::processChat(const std::string& message, unsigned int playerID) |
---|
119 | { |
---|
120 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
121 | return true; |
---|
122 | } |
---|
123 | |
---|
124 | void Client::printRTT() |
---|
125 | { |
---|
126 | COUT(0) << "Round trip time to server is " << ClientConnection::getRTT() << " ms" << endl; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * This function implements the method of sending a chat message to the server |
---|
131 | * @param message message to be sent |
---|
132 | * @return result(true/false) |
---|
133 | */ |
---|
134 | bool Client::chat(const std::string& message) |
---|
135 | { |
---|
136 | packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); |
---|
137 | return m->send(static_cast<Host*>(this)); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | /** |
---|
142 | * Processes incoming packets, sends a gamestate to the server and does the cleanup |
---|
143 | * @param time |
---|
144 | */ |
---|
145 | void Client::update(const Clock& time) |
---|
146 | { |
---|
147 | //this steers our network frequency |
---|
148 | timeSinceLastUpdate_+=time.getDeltaTime(); |
---|
149 | if(timeSinceLastUpdate_>=NETWORK_PERIOD) |
---|
150 | { |
---|
151 | timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD; |
---|
152 | // COUT(3) << '.'; |
---|
153 | if ( isConnected() && isSynched_ ) |
---|
154 | { |
---|
155 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
156 | // packet::Gamestate *gs = GamestateClient::getGamestate(); |
---|
157 | if( GamestateManager::update() ) |
---|
158 | { |
---|
159 | std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); |
---|
160 | std::vector<packet::Gamestate*>::iterator it; |
---|
161 | for( it = gamestates.begin(); it != gamestates.end(); ++it ) |
---|
162 | { |
---|
163 | (*it)->send( static_cast<Host*>(this) ); |
---|
164 | } |
---|
165 | } |
---|
166 | //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now |
---|
167 | // if(gs){ |
---|
168 | // COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
169 | // if( !gs->send() ) |
---|
170 | // COUT(2) << "Problem adding partial gamestate to queue" << std::endl; |
---|
171 | // // gs gets automatically deleted by enet callback |
---|
172 | // } |
---|
173 | FunctionCallManager::sendCalls(static_cast<Host*>(this)); |
---|
174 | } |
---|
175 | } |
---|
176 | // sendPackets(); // flush the enet queue |
---|
177 | |
---|
178 | Connection::processQueue(); |
---|
179 | if(GamestateManager::processGamestates()) |
---|
180 | { |
---|
181 | FunctionCallManager::processBufferedFunctionCalls(); |
---|
182 | if(!isSynched_) |
---|
183 | isSynched_=true; |
---|
184 | } |
---|
185 | // GamestateManager::cleanup();; |
---|
186 | // Connection::sendPackets(); |
---|
187 | |
---|
188 | return; |
---|
189 | } |
---|
190 | |
---|
191 | void Client::connectionClosed() |
---|
192 | { |
---|
193 | ObjectList<Synchronisable>::iterator it; |
---|
194 | for(it = ObjectList<Synchronisable>::begin(); it; ) |
---|
195 | { |
---|
196 | if( it->getSyncMode() != 0x0 ) |
---|
197 | (it++)->destroy(); |
---|
198 | else |
---|
199 | { |
---|
200 | ++it; |
---|
201 | } |
---|
202 | } |
---|
203 | Game::getInstance().popState(); |
---|
204 | Game::getInstance().popState(); |
---|
205 | } |
---|
206 | |
---|
207 | void Client::processPacket(packet::Packet* packet) |
---|
208 | { |
---|
209 | if( packet->isReliable() ) |
---|
210 | { |
---|
211 | if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) |
---|
212 | packet->process(static_cast<Host*>(this)); |
---|
213 | else |
---|
214 | this->packetQueue_.push_back(packet); |
---|
215 | } |
---|
216 | else |
---|
217 | packet->process(static_cast<Host*>(this)); |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | } |
---|