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