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, (C) 2007 |
---|
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 | #include "Synchronisable.h" |
---|
43 | #include "core/CoreIncludes.h" |
---|
44 | |
---|
45 | namespace network |
---|
46 | { |
---|
47 | Client* Client::_sClient = 0; |
---|
48 | |
---|
49 | Client* Client::createSingleton(){ |
---|
50 | if(!_sClient){ |
---|
51 | _sClient = new Client(); |
---|
52 | } |
---|
53 | return _sClient; |
---|
54 | } |
---|
55 | |
---|
56 | Client* Client::createSingleton(std::string address, int port){ |
---|
57 | if(!_sClient) |
---|
58 | _sClient = new Client(address, port); |
---|
59 | return _sClient; |
---|
60 | } |
---|
61 | |
---|
62 | Client* Client::createSingleton(const char *address, int port){ |
---|
63 | if(!_sClient) |
---|
64 | _sClient = new Client(address, port); |
---|
65 | return _sClient; |
---|
66 | } |
---|
67 | |
---|
68 | void Client::destroySingleton(){ |
---|
69 | if(_sClient){ |
---|
70 | delete _sClient; |
---|
71 | _sClient = 0; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | Client* Client::getSingleton(){ |
---|
76 | return _sClient; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Constructor for the Client class |
---|
81 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
82 | */ |
---|
83 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
84 | // set server address to localhost |
---|
85 | isConnected=false; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Constructor for the Client class |
---|
90 | * @param address the server address |
---|
91 | * @param port port of the application on the server |
---|
92 | */ |
---|
93 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
94 | isConnected=false; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Constructor for the Client class |
---|
99 | * @param address the server address |
---|
100 | * @param port port of the application on the server |
---|
101 | */ |
---|
102 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
103 | isConnected=false; |
---|
104 | } |
---|
105 | |
---|
106 | Client::~Client(){ |
---|
107 | if(isConnected) |
---|
108 | closeConnection(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Establish the Connection to the Server |
---|
113 | * @return true/false |
---|
114 | */ |
---|
115 | bool Client::establishConnection(){ |
---|
116 | Synchronisable::setClient(true); |
---|
117 | isConnected=client_connection.createConnection(); |
---|
118 | if(isConnected){ |
---|
119 | COUT(4) << "sending connectrequest" << std::endl; |
---|
120 | client_connection.addPacket(pck_gen.generateConnectRequest()); |
---|
121 | client_connection.sendPackets(); |
---|
122 | }else |
---|
123 | COUT(1) << "could not create connection" << std::endl; |
---|
124 | return isConnected; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * closes the Connection to the Server |
---|
129 | * @return true/false |
---|
130 | */ |
---|
131 | bool Client::closeConnection(){ |
---|
132 | isConnected=false; |
---|
133 | return client_connection.closeConnection(); |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * submits a MouseAction to the server |
---|
138 | * @param x x Coordinate |
---|
139 | * @param y y Coordinate |
---|
140 | * @return true/false |
---|
141 | */ |
---|
142 | bool Client::sendMouse(double x, double y){ |
---|
143 | // generate packet and add it to the queue |
---|
144 | if(!isConnected) |
---|
145 | return false; |
---|
146 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
147 | return false; |
---|
148 | // send packets |
---|
149 | return client_connection.sendPackets(); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * submits a Keystrike to the server |
---|
154 | * @param key_code code to submit |
---|
155 | * @return true/false |
---|
156 | */ |
---|
157 | bool Client::sendKeyboard(char key_code){ |
---|
158 | // generate packet and add it to queue |
---|
159 | if(!isConnected) |
---|
160 | return false; |
---|
161 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
162 | return false; |
---|
163 | // send packets |
---|
164 | return client_connection.sendPackets(); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * submits a chat message to the server |
---|
169 | * @param message message to send |
---|
170 | * @return true/false |
---|
171 | */ |
---|
172 | bool Client::sendChat( std::string message ){ |
---|
173 | // generate packet and add it to queue |
---|
174 | if(!isConnected) |
---|
175 | return false; |
---|
176 | if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() ))) |
---|
177 | return client_connection.sendPackets(); |
---|
178 | // send packets |
---|
179 | return false; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Adds a MouseAction to the PacketQueue |
---|
184 | * @param x x Coordinate |
---|
185 | * @param y y Coordinate |
---|
186 | * @return true/false |
---|
187 | */ |
---|
188 | bool Client::addMouse(double x, double y){ |
---|
189 | // generate packet and add it to the queue |
---|
190 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
191 | return true; |
---|
192 | else |
---|
193 | return false; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Adds a Keystrike to the PacketQueue |
---|
198 | * @param key_code |
---|
199 | * @return true/false |
---|
200 | */ |
---|
201 | bool Client::addKeyboard(char key_code){ |
---|
202 | // generate packet and add it to queue |
---|
203 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
204 | return true; |
---|
205 | else |
---|
206 | return false; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Sends out all the packets queued by addXXX |
---|
211 | */ |
---|
212 | bool Client::sendPackets(){ |
---|
213 | if(!isConnected) |
---|
214 | return false; |
---|
215 | ENetEvent event; |
---|
216 | // send packets |
---|
217 | client_connection.sendPackets(&event); |
---|
218 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
219 | return true; |
---|
220 | else |
---|
221 | return false; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Performs a GameState update |
---|
226 | */ |
---|
227 | void Client::tick(float time){ |
---|
228 | ENetPacket *packet; |
---|
229 | // stop if the packet queue is empty |
---|
230 | while(!(client_connection.queueEmpty())){ |
---|
231 | packet = client_connection.getPacket(); |
---|
232 | COUT(5) << "tick packet size " << packet->dataLength << std::endl; |
---|
233 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
234 | } |
---|
235 | return; |
---|
236 | } |
---|
237 | |
---|
238 | void Client::processGamestate( GameStateCompressed *data){ |
---|
239 | int id = data->id; |
---|
240 | COUT(5) << "received gamestate id: " << data->id << std::endl; |
---|
241 | if(gamestate.pushGameState(data)){ |
---|
242 | client_connection.addPacket(pck_gen.acknowledgement(id)); |
---|
243 | if(!client_connection.sendPackets()) |
---|
244 | COUT(2) << "Could not send acknowledgment" << std::endl; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | void Client::processClassid(classid *clid){ |
---|
249 | orxonox::Identifier *id; |
---|
250 | id=ID(std::string(clid->message)); |
---|
251 | if(id!=NULL) |
---|
252 | id->setNetworkID(clid->clid); |
---|
253 | COUT(4) << "Client: received and set network id: " << clid->clid << "; classname: " << clid->message << std::endl; |
---|
254 | return; |
---|
255 | } |
---|
256 | |
---|
257 | void Client::processChat( chat *data){ |
---|
258 | COUT(0) << "Server: " << data->message << std::endl; |
---|
259 | } |
---|
260 | |
---|
261 | bool Client::processWelcome( welcome *w ){ |
---|
262 | COUT(4) << "processing welcome message" << std::endl; |
---|
263 | clientID_ = w->clientID; |
---|
264 | shipID_ = w->shipID; |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | } |
---|