[1056] | 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 | |
---|
[230] | 29 | // |
---|
| 30 | // C++ Implementation: Server |
---|
| 31 | // |
---|
[285] | 32 | // Description: |
---|
[230] | 33 | // |
---|
| 34 | // |
---|
| 35 | // Author: Oliver Scheuss, (C) 2007 |
---|
| 36 | // |
---|
| 37 | // Copyright: See COPYING file that comes with this distribution |
---|
| 38 | // |
---|
| 39 | // |
---|
| 40 | |
---|
[1062] | 41 | #include "Server.h" |
---|
| 42 | |
---|
[777] | 43 | #include <iostream> |
---|
[230] | 44 | |
---|
[777] | 45 | #include "ConnectionManager.h" |
---|
| 46 | #include "PacketTypes.h" |
---|
| 47 | #include "GameStateManager.h" |
---|
| 48 | #include "ClientInformation.h" |
---|
| 49 | //#include "NetworkFrameListener.h" |
---|
[1021] | 50 | #include "util/Sleep.h" |
---|
[230] | 51 | |
---|
[285] | 52 | |
---|
[777] | 53 | namespace network |
---|
| 54 | { |
---|
[230] | 55 | /** |
---|
[777] | 56 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
---|
| 57 | * |
---|
| 58 | */ |
---|
| 59 | Server::Server() { |
---|
[369] | 60 | packet_gen = PacketGenerator(); |
---|
[444] | 61 | clients = new ClientInformation(true); |
---|
[436] | 62 | connection = new ConnectionManager(clients); |
---|
| 63 | gamestates = new GameStateManager(clients); |
---|
[230] | 64 | } |
---|
[285] | 65 | |
---|
[230] | 66 | /** |
---|
[777] | 67 | * Constructor |
---|
| 68 | * @param port Port to listen on |
---|
| 69 | * @param bindAddress Address to listen on |
---|
| 70 | */ |
---|
| 71 | Server::Server(int port, std::string bindAddress) { |
---|
[369] | 72 | packet_gen = PacketGenerator(); |
---|
[436] | 73 | clients = new ClientInformation(); |
---|
| 74 | connection = new ConnectionManager(port, bindAddress, clients); |
---|
| 75 | gamestates = new GameStateManager(clients); |
---|
[230] | 76 | } |
---|
[285] | 77 | |
---|
[230] | 78 | /** |
---|
[777] | 79 | * Constructor |
---|
| 80 | * @param port Port to listen on |
---|
| 81 | * @param bindAddress Address to listen on |
---|
| 82 | */ |
---|
| 83 | Server::Server(int port, const char *bindAddress) { |
---|
[369] | 84 | packet_gen = PacketGenerator(); |
---|
[436] | 85 | clients = new ClientInformation(); |
---|
| 86 | connection = new ConnectionManager(port, bindAddress, clients); |
---|
| 87 | gamestates = new GameStateManager(clients); |
---|
[230] | 88 | } |
---|
[660] | 89 | |
---|
[369] | 90 | /** |
---|
[777] | 91 | * This function opens the server by creating the listener thread |
---|
| 92 | */ |
---|
| 93 | void Server::open() { |
---|
[436] | 94 | connection->createListener(); |
---|
[369] | 95 | return; |
---|
| 96 | } |
---|
[660] | 97 | |
---|
[369] | 98 | /** |
---|
[777] | 99 | * This function closes the server |
---|
| 100 | */ |
---|
| 101 | void Server::close() { |
---|
[436] | 102 | connection->quitListener(); |
---|
[369] | 103 | return; |
---|
| 104 | } |
---|
[660] | 105 | |
---|
[369] | 106 | /** |
---|
[777] | 107 | * This function sends out a message to all clients |
---|
| 108 | * @param msg message |
---|
| 109 | * @return true/false |
---|
| 110 | */ |
---|
| 111 | bool Server::sendMSG(std::string msg) { |
---|
[369] | 112 | ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); |
---|
[479] | 113 | //std::cout <<"adding packets" << std::endl; |
---|
[436] | 114 | connection->addPacketAll(packet); |
---|
[479] | 115 | //std::cout <<"added packets" << std::endl; |
---|
[436] | 116 | return connection->sendPackets(); |
---|
[369] | 117 | } |
---|
[777] | 118 | |
---|
[369] | 119 | /** |
---|
[777] | 120 | * This function sends out a message to all clients |
---|
| 121 | * @param msg message |
---|
| 122 | * @return true/false |
---|
| 123 | */ |
---|
| 124 | bool Server::sendMSG(const char *msg) { |
---|
[369] | 125 | ENetPacket *packet = packet_gen.chatMessage(msg); |
---|
[1098] | 126 | COUT(4) <<"Server: adding Packets" << std::endl; |
---|
[436] | 127 | connection->addPacketAll(packet); |
---|
[479] | 128 | //std::cout <<"added packets" << std::endl; |
---|
[601] | 129 | if (connection->sendPackets()){ |
---|
[1098] | 130 | COUT(4) << "Server: Sucessfully" << std::endl; |
---|
[777] | 131 | return true; |
---|
[601] | 132 | } |
---|
| 133 | return false; |
---|
[369] | 134 | } |
---|
[660] | 135 | |
---|
[380] | 136 | /** |
---|
[777] | 137 | * Run this function once every tick |
---|
| 138 | * calls processQueue and updateGamestate |
---|
| 139 | * @param time time since last tick |
---|
| 140 | */ |
---|
| 141 | void Server::tick(float time) { |
---|
[380] | 142 | processQueue(); |
---|
| 143 | updateGamestate(); |
---|
[1021] | 144 | |
---|
| 145 | sleep(1); // TODO remove |
---|
[380] | 146 | return; |
---|
[369] | 147 | } |
---|
[660] | 148 | |
---|
[380] | 149 | /** |
---|
[777] | 150 | * processes all the packets waiting in the queue |
---|
| 151 | */ |
---|
| 152 | void Server::processQueue() { |
---|
[380] | 153 | ENetPacket *packet; |
---|
[381] | 154 | int clientID=-1; |
---|
[436] | 155 | while(!connection->queueEmpty()){ |
---|
[479] | 156 | //std::cout << "Client " << clientID << " sent: " << std::endl; |
---|
[1098] | 157 | //clientID here is a reference to grab clientID from ClientInformation |
---|
[436] | 158 | packet = connection->getPacket(clientID); |
---|
[1098] | 159 | //if statement to catch case that packetbuffer is empty |
---|
| 160 | if( !elaborate(packet, clientID) ) |
---|
| 161 | COUT(4) << "Server: PacketBuffer empty" << std::endl; |
---|
[380] | 162 | } |
---|
| 163 | } |
---|
[660] | 164 | |
---|
[380] | 165 | /** |
---|
[777] | 166 | * takes a new snapshot of the gamestate and sends it to the clients |
---|
| 167 | */ |
---|
| 168 | void Server::updateGamestate() { |
---|
[436] | 169 | gamestates->update(); |
---|
[1098] | 170 | COUT(4) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; |
---|
[606] | 171 | //std::cout << "updated gamestate, sending it" << std::endl; |
---|
[1021] | 172 | //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) |
---|
[1098] | 173 | sendGameState(); |
---|
| 174 | COUT(4) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; |
---|
[606] | 175 | //std::cout << "sent gamestate" << std::endl; |
---|
[380] | 176 | } |
---|
[660] | 177 | |
---|
[425] | 178 | /** |
---|
[777] | 179 | * sends the gamestate |
---|
| 180 | */ |
---|
| 181 | bool Server::sendGameState() { |
---|
[1098] | 182 | COUT(5) << "Server: starting function sendGameState" << std::endl; |
---|
[607] | 183 | ClientInformation *temp = clients; |
---|
| 184 | bool added=false; |
---|
[1098] | 185 | while(temp != NULL){ |
---|
[607] | 186 | if(temp->head){ |
---|
| 187 | temp=temp->next(); |
---|
[1098] | 188 | //think this works without continue |
---|
[607] | 189 | continue; |
---|
| 190 | } |
---|
[636] | 191 | if( !(temp->getSynched()) ){ |
---|
[1098] | 192 | COUT(5) << "Server: not sending gamestate" << std::endl; |
---|
[636] | 193 | temp=temp->next(); |
---|
[1098] | 194 | //think this works without continue |
---|
[636] | 195 | continue; |
---|
| 196 | } |
---|
[1098] | 197 | COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; |
---|
| 198 | int gid = temp->getGamestateID(); //get gamestate id |
---|
| 199 | int cid = temp->getID(); //get client id |
---|
| 200 | COUT(5) << "Server: got acked (gamestate) ID from clientlist: " << gid << std::endl; |
---|
[1021] | 201 | GameStateCompressed *gs = gamestates->popGameState(cid); |
---|
| 202 | if(gs==NULL){ |
---|
[1098] | 203 | COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; |
---|
[1021] | 204 | return false; |
---|
| 205 | } |
---|
[624] | 206 | //std::cout << "adding gamestate" << std::endl; |
---|
[1098] | 207 | if ( !(connection->addPacket(packet_gen.gstate(gs), cid)) ) |
---|
| 208 | COUT(4) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl; |
---|
[624] | 209 | //std::cout << "added gamestate" << std::endl; |
---|
[607] | 210 | added=true; |
---|
| 211 | temp=temp->next(); |
---|
[605] | 212 | } |
---|
[1098] | 213 | if(added) { |
---|
| 214 | //std::cout << "send gamestates from server.cc in sendGameState" << std::endl; |
---|
[607] | 215 | return connection->sendPackets(); |
---|
[1098] | 216 | } |
---|
| 217 | COUT(5) << "Server: had no gamestates to send" << std::endl; |
---|
[1021] | 218 | return false; |
---|
[422] | 219 | } |
---|
[660] | 220 | |
---|
[777] | 221 | void Server::processAck( ack *data, int clientID) { |
---|
[1098] | 222 | COUT(5) << "Server: processing ack from client: " << clientID << "; ack-id: " << data->id << std::endl; |
---|
[624] | 223 | clients->findClient(clientID)->setGamestateID(data->a); |
---|
[620] | 224 | } |
---|
[660] | 225 | |
---|
[230] | 226 | } |
---|