[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 | |
---|
| 13 | |
---|
[285] | 14 | #include "Server.h" |
---|
[230] | 15 | |
---|
| 16 | namespace network{ |
---|
[285] | 17 | |
---|
[230] | 18 | /** |
---|
| 19 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
---|
[285] | 20 | * |
---|
[230] | 21 | */ |
---|
| 22 | Server::Server(){ |
---|
[369] | 23 | packet_gen = PacketGenerator(); |
---|
[444] | 24 | clients = new ClientInformation(true); |
---|
[436] | 25 | connection = new ConnectionManager(clients); |
---|
| 26 | gamestates = new GameStateManager(clients); |
---|
[230] | 27 | } |
---|
[285] | 28 | |
---|
[230] | 29 | /** |
---|
| 30 | * Constructor |
---|
| 31 | * @param port Port to listen on |
---|
| 32 | * @param bindAddress Address to listen on |
---|
| 33 | */ |
---|
[369] | 34 | Server::Server(int port, std::string bindAddress){ |
---|
| 35 | packet_gen = PacketGenerator(); |
---|
[436] | 36 | clients = new ClientInformation(); |
---|
| 37 | connection = new ConnectionManager(port, bindAddress, clients); |
---|
| 38 | gamestates = new GameStateManager(clients); |
---|
[230] | 39 | } |
---|
[285] | 40 | |
---|
[230] | 41 | /** |
---|
| 42 | * Constructor |
---|
| 43 | * @param port Port to listen on |
---|
| 44 | * @param bindAddress Address to listen on |
---|
| 45 | */ |
---|
[369] | 46 | Server::Server(int port, const char *bindAddress){ |
---|
| 47 | packet_gen = PacketGenerator(); |
---|
[436] | 48 | clients = new ClientInformation(); |
---|
| 49 | connection = new ConnectionManager(port, bindAddress, clients); |
---|
| 50 | gamestates = new GameStateManager(clients); |
---|
[230] | 51 | } |
---|
[369] | 52 | |
---|
| 53 | /** |
---|
| 54 | * This function opens the server by creating the listener thread |
---|
| 55 | */ |
---|
| 56 | void Server::open(){ |
---|
[436] | 57 | connection->createListener(); |
---|
[369] | 58 | return; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * This function closes the server |
---|
| 63 | */ |
---|
| 64 | void Server::close(){ |
---|
[436] | 65 | connection->quitListener(); |
---|
[369] | 66 | return; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * This function sends out a message to all clients |
---|
| 71 | * @param msg message |
---|
| 72 | * @return true/false |
---|
| 73 | */ |
---|
| 74 | bool Server::sendMSG(std::string msg){ |
---|
| 75 | ENetPacket *packet = packet_gen.chatMessage(msg.c_str()); |
---|
[442] | 76 | std::cout <<"adding packets" << std::endl; |
---|
[436] | 77 | connection->addPacketAll(packet); |
---|
[442] | 78 | std::cout <<"added packets" << std::endl; |
---|
[436] | 79 | return connection->sendPackets(); |
---|
[369] | 80 | } |
---|
| 81 | /** |
---|
| 82 | * This function sends out a message to all clients |
---|
| 83 | * @param msg message |
---|
| 84 | * @return true/false |
---|
| 85 | */ |
---|
| 86 | bool Server::sendMSG(const char *msg){ |
---|
| 87 | ENetPacket *packet = packet_gen.chatMessage(msg); |
---|
[442] | 88 | std::cout <<"adding packets" << std::endl; |
---|
[436] | 89 | connection->addPacketAll(packet); |
---|
[442] | 90 | std::cout <<"added packets" << std::endl; |
---|
[436] | 91 | return connection->sendPackets(); |
---|
[369] | 92 | } |
---|
| 93 | |
---|
[380] | 94 | /** |
---|
| 95 | * Run this function once every tick |
---|
| 96 | * calls processQueue and updateGamestate |
---|
| 97 | */ |
---|
[369] | 98 | void Server::tick(){ |
---|
[380] | 99 | processQueue(); |
---|
| 100 | updateGamestate(); |
---|
| 101 | return; |
---|
[369] | 102 | } |
---|
| 103 | |
---|
[380] | 104 | /** |
---|
| 105 | * processes all the packets waiting in the queue |
---|
| 106 | */ |
---|
| 107 | void Server::processQueue(){ |
---|
| 108 | ENetPacket *packet; |
---|
[381] | 109 | int clientID=-1; |
---|
[436] | 110 | while(!connection->queueEmpty()){ |
---|
[444] | 111 | std::cout << "Client " << clientID << " sent: " << std::endl; |
---|
[436] | 112 | packet = connection->getPacket(clientID); |
---|
[422] | 113 | elaborate(packet, clientID); |
---|
[380] | 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * takes a new snapshot of the gamestate and sends it to the clients |
---|
| 119 | */ |
---|
| 120 | void Server::updateGamestate(){ |
---|
[436] | 121 | gamestates->update(); |
---|
[422] | 122 | sendGameState(); |
---|
[380] | 123 | } |
---|
| 124 | |
---|
[425] | 125 | /** |
---|
| 126 | * sends the gamestate |
---|
| 127 | */ |
---|
[422] | 128 | bool Server::sendGameState(){ |
---|
[425] | 129 | return true; |
---|
[422] | 130 | } |
---|
[400] | 131 | |
---|
[422] | 132 | |
---|
[230] | 133 | } |
---|