| 1 | // |
|---|
| 2 | // C++ Implementation: Server |
|---|
| 3 | // |
|---|
| 4 | // Description: |
|---|
| 5 | // |
|---|
| 6 | // |
|---|
| 7 | // Author: Oliver Scheuss, (C) 2007 |
|---|
| 8 | // |
|---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
|---|
| 10 | // |
|---|
| 11 | // |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include "Server.h" |
|---|
| 15 | |
|---|
| 16 | namespace network{ |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Constructor for default values (bindaddress is set to ENET_HOST_ANY |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | Server::Server(){ |
|---|
| 23 | packet_gen = PacketGenerator(); |
|---|
| 24 | clients = new ClientInformation(true); |
|---|
| 25 | connection = new ConnectionManager(clients); |
|---|
| 26 | gamestates = new GameStateManager(clients); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Constructor |
|---|
| 31 | * @param port Port to listen on |
|---|
| 32 | * @param bindAddress Address to listen on |
|---|
| 33 | */ |
|---|
| 34 | Server::Server(int port, std::string bindAddress){ |
|---|
| 35 | packet_gen = PacketGenerator(); |
|---|
| 36 | clients = new ClientInformation(); |
|---|
| 37 | connection = new ConnectionManager(port, bindAddress, clients); |
|---|
| 38 | gamestates = new GameStateManager(clients); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Constructor |
|---|
| 43 | * @param port Port to listen on |
|---|
| 44 | * @param bindAddress Address to listen on |
|---|
| 45 | */ |
|---|
| 46 | Server::Server(int port, const char *bindAddress){ |
|---|
| 47 | packet_gen = PacketGenerator(); |
|---|
| 48 | clients = new ClientInformation(); |
|---|
| 49 | connection = new ConnectionManager(port, bindAddress, clients); |
|---|
| 50 | gamestates = new GameStateManager(clients); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * This function opens the server by creating the listener thread |
|---|
| 55 | */ |
|---|
| 56 | void Server::open(){ |
|---|
| 57 | connection->createListener(); |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * This function closes the server |
|---|
| 63 | */ |
|---|
| 64 | void Server::close(){ |
|---|
| 65 | connection->quitListener(); |
|---|
| 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()); |
|---|
| 76 | //std::cout <<"adding packets" << std::endl; |
|---|
| 77 | connection->addPacketAll(packet); |
|---|
| 78 | //std::cout <<"added packets" << std::endl; |
|---|
| 79 | return connection->sendPackets(); |
|---|
| 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); |
|---|
| 88 | //std::cout <<"adding packets" << std::endl; |
|---|
| 89 | connection->addPacketAll(packet); |
|---|
| 90 | //std::cout <<"added packets" << std::endl; |
|---|
| 91 | return connection->sendPackets(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Run this function once every tick |
|---|
| 96 | * calls processQueue and updateGamestate |
|---|
| 97 | * @param time time since last tick |
|---|
| 98 | */ |
|---|
| 99 | void Server::tick(float time){ |
|---|
| 100 | processQueue(); |
|---|
| 101 | updateGamestate(); |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * processes all the packets waiting in the queue |
|---|
| 107 | */ |
|---|
| 108 | void Server::processQueue(){ |
|---|
| 109 | ENetPacket *packet; |
|---|
| 110 | int clientID=-1; |
|---|
| 111 | while(!connection->queueEmpty()){ |
|---|
| 112 | //std::cout << "Client " << clientID << " sent: " << std::endl; |
|---|
| 113 | packet = connection->getPacket(clientID); |
|---|
| 114 | elaborate(packet, clientID); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * takes a new snapshot of the gamestate and sends it to the clients |
|---|
| 120 | */ |
|---|
| 121 | void Server::updateGamestate(){ |
|---|
| 122 | gamestates->update(); |
|---|
| 123 | sendGameState(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * sends the gamestate |
|---|
| 128 | */ |
|---|
| 129 | bool Server::sendGameState(){ |
|---|
| 130 | return true; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | } |
|---|