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