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