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 "Host.h" |
---|
43 | #include "Synchronisable.h" |
---|
44 | #include "core/CoreIncludes.h" |
---|
45 | #include "core/ConsoleCommand.h" |
---|
46 | #include "packet/Packet.h" |
---|
47 | #include "packet/Acknowledgement.h" |
---|
48 | |
---|
49 | namespace network |
---|
50 | { |
---|
51 | // SetConsoleCommandShortcut(Client, chat); |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * Constructor for the Client class |
---|
56 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
57 | */ |
---|
58 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
59 | // set server address to localhost |
---|
60 | isConnected=false; |
---|
61 | isSynched_=false; |
---|
62 | gameStateFailure_=false; |
---|
63 | isServer_ = false; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Constructor for the Client class |
---|
68 | * @param address the server address |
---|
69 | * @param port port of the application on the server |
---|
70 | */ |
---|
71 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
72 | isConnected=false; |
---|
73 | isSynched_=false; |
---|
74 | gameStateFailure_=false; |
---|
75 | isServer_ = false; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Constructor for the Client class |
---|
80 | * @param address the server address |
---|
81 | * @param port port of the application on the server |
---|
82 | */ |
---|
83 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
84 | isConnected=false; |
---|
85 | isSynched_=false; |
---|
86 | gameStateFailure_=false; |
---|
87 | isServer_ = false; |
---|
88 | } |
---|
89 | |
---|
90 | Client::~Client(){ |
---|
91 | if(isConnected) |
---|
92 | closeConnection(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Establish the Connection to the Server |
---|
97 | * @return true/false |
---|
98 | */ |
---|
99 | bool Client::establishConnection(){ |
---|
100 | Synchronisable::setClient(true); |
---|
101 | isConnected=client_connection.createConnection(); |
---|
102 | if(isConnected){ |
---|
103 | // COUT(3) << "sending connectrequest" << std::endl; |
---|
104 | // if(!client_connection.addPacket(pck_gen.generateConnectRequest()) || !client_connection.sendPackets()) |
---|
105 | // COUT(1) << "could not send connection request !!!!!!!!!" << std::endl; |
---|
106 | }else |
---|
107 | COUT(1) << "could not create connection laber" << std::endl; |
---|
108 | return isConnected; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * closes the Connection to the Server |
---|
113 | * @return true/false |
---|
114 | */ |
---|
115 | bool Client::closeConnection(){ |
---|
116 | isConnected=false; |
---|
117 | return client_connection.closeConnection(); |
---|
118 | } |
---|
119 | |
---|
120 | bool Client::queuePacket(ENetPacket *packet, int clientID){ |
---|
121 | return client_connection.addPacket(packet); |
---|
122 | } |
---|
123 | |
---|
124 | bool Client::processChat(packet::Chat *message, unsigned int clientID){ |
---|
125 | return message->process(); |
---|
126 | } |
---|
127 | |
---|
128 | /*bool Client::sendChat(packet::Chat *chat){ |
---|
129 | chat->process(); |
---|
130 | packet::Packet *p = new packet::Packet(chat); |
---|
131 | return p->send(); |
---|
132 | }*/ |
---|
133 | |
---|
134 | |
---|
135 | /** |
---|
136 | * submits a chat message to the server |
---|
137 | * @param message message to send |
---|
138 | * @return true/false |
---|
139 | */ |
---|
140 | bool Client::sendChat( std::string message ){ |
---|
141 | // generate packet and add it to queue |
---|
142 | if(!isConnected) |
---|
143 | return false; |
---|
144 | packet::Chat chat(message, 0); |
---|
145 | return chat.send(); |
---|
146 | // send packets |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Performs a GameState update |
---|
151 | */ |
---|
152 | void Client::tick(float time){ |
---|
153 | // COUT(3) << "."; |
---|
154 | if(client_connection.isConnected() && isSynched_){ |
---|
155 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
156 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
157 | if(gs){ |
---|
158 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
159 | if( !gs->send() ) |
---|
160 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
161 | // gs gets automatically deleted by enet callback |
---|
162 | } |
---|
163 | } |
---|
164 | ENetEvent *event; |
---|
165 | // stop if the packet queue is empty |
---|
166 | while(!(client_connection.queueEmpty())){ |
---|
167 | event = client_connection.getEvent(); |
---|
168 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
169 | packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); |
---|
170 | assert(packet->process()); |
---|
171 | } |
---|
172 | int gameStateID = gamestate.processGamestates(); |
---|
173 | if(gameStateID==GAMESTATEID_INITIAL) |
---|
174 | if(gameStateFailure_){ |
---|
175 | packet::Acknowledgement *ack = new packet::Acknowledgement(GAMESTATEID_INITIAL, 0); |
---|
176 | if(!ack->send()) |
---|
177 | COUT(3) << "could not (negatively) ack gamestate" << std::endl; |
---|
178 | else |
---|
179 | COUT(4) << "negatively acked a gamestate" << std::endl; |
---|
180 | } |
---|
181 | else |
---|
182 | gameStateFailure_=true; |
---|
183 | else if(gameStateID!=0){ |
---|
184 | // ack gamestate and set synched |
---|
185 | if(!isSynched_) |
---|
186 | isSynched_=true; |
---|
187 | gameStateFailure_=false; |
---|
188 | packet::Acknowledgement *ack = new packet::Acknowledgement(gameStateID, 0); |
---|
189 | if(!ack->send()) |
---|
190 | COUT(3) << "could not ack gamestate" << std::endl; |
---|
191 | }// otherwise we had no gamestate to load |
---|
192 | gamestate.cleanup(); |
---|
193 | return; |
---|
194 | } |
---|
195 | |
---|
196 | } |
---|