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 <cassert> |
---|
42 | #include <enet/enet.h> |
---|
43 | |
---|
44 | #include "Client.h" |
---|
45 | #include "Host.h" |
---|
46 | #include "synchronisable/Synchronisable.h" |
---|
47 | #include "core/CoreIncludes.h" |
---|
48 | #include "packet/Packet.h" |
---|
49 | |
---|
50 | // #include "packet/Acknowledgement.h" |
---|
51 | |
---|
52 | namespace orxonox |
---|
53 | { |
---|
54 | // SetConsoleCommandShortcut(Client, chat); |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * Constructor for the Client class |
---|
59 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
60 | */ |
---|
61 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
62 | // set server address to localhost |
---|
63 | isConnected=false; |
---|
64 | isSynched_=false; |
---|
65 | gameStateFailure_=false; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Constructor for the Client class |
---|
70 | * @param address the server address |
---|
71 | * @param port port of the application on the server |
---|
72 | */ |
---|
73 | Client::Client(const std::string& address, int port) : client_connection(port, address){ |
---|
74 | isConnected=false; |
---|
75 | isSynched_=false; |
---|
76 | gameStateFailure_=false; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Constructor for the Client class |
---|
81 | * @param address the server address |
---|
82 | * @param port port of the application on the server |
---|
83 | */ |
---|
84 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
85 | isConnected=false; |
---|
86 | isSynched_=false; |
---|
87 | gameStateFailure_=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(1) << "could not create connection laber" << std::endl; |
---|
104 | return isConnected; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * closes the Connection to the Server |
---|
109 | * @return true/false |
---|
110 | */ |
---|
111 | bool Client::closeConnection(){ |
---|
112 | isConnected=false; |
---|
113 | return client_connection.closeConnection(); |
---|
114 | } |
---|
115 | |
---|
116 | bool Client::queuePacket(ENetPacket *packet, int clientID){ |
---|
117 | return client_connection.addPacket(packet); |
---|
118 | } |
---|
119 | |
---|
120 | bool Client::processChat(const std::string& message, unsigned int playerID){ |
---|
121 | // COUT(1) << "Player " << playerID << ": " << message << std::endl; |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * This function implements the method of sending a chat message to the server |
---|
127 | * @param message message to be sent |
---|
128 | * @return result(true/false) |
---|
129 | */ |
---|
130 | bool Client::chat(const std::string& message){ |
---|
131 | packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); |
---|
132 | return m->send(); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | * Processes incoming packets, sends a gamestate to the server and does the cleanup |
---|
138 | * @param time |
---|
139 | */ |
---|
140 | void Client::tick(float time){ |
---|
141 | // COUT(3) << "."; |
---|
142 | if(client_connection.isConnected() && isSynched_){ |
---|
143 | COUT(4) << "popping partial gamestate: " << std::endl; |
---|
144 | packet::Gamestate *gs = gamestate.getGamestate(); |
---|
145 | if(gs){ |
---|
146 | COUT(4) << "client tick: sending gs " << gs << std::endl; |
---|
147 | if( !gs->send() ) |
---|
148 | COUT(3) << "Problem adding partial gamestate to queue" << std::endl; |
---|
149 | // gs gets automatically deleted by enet callback |
---|
150 | } |
---|
151 | } |
---|
152 | ENetEvent *event; |
---|
153 | // stop if the packet queue is empty |
---|
154 | while(!(client_connection.queueEmpty())){ |
---|
155 | event = client_connection.getEvent(); |
---|
156 | COUT(5) << "tick packet size " << event->packet->dataLength << std::endl; |
---|
157 | packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer); |
---|
158 | // note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler |
---|
159 | bool b = packet->process(); |
---|
160 | assert(b); |
---|
161 | } |
---|
162 | if(gamestate.processGamestates()) |
---|
163 | { |
---|
164 | if(!isSynched_) |
---|
165 | isSynched_=true; |
---|
166 | } |
---|
167 | gamestate.cleanup(); |
---|
168 | return; |
---|
169 | } |
---|
170 | |
---|
171 | } |
---|