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