Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/Server.cc @ 436

Last change on this file since 436 was 436, checked in by scheusso, 16 years ago

extended gamestatehandling for diffed and not diffed gamestates (initial states, etc)

File size: 2.7 KB
Line 
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
16namespace 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();
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    connection->addPacketAll(packet);
77    return connection->sendPackets();
78  }
79  /**
80   * This function sends out a message to all clients
81   * @param msg message
82   * @return true/false
83   */
84  bool Server::sendMSG(const char *msg){
85    ENetPacket *packet = packet_gen.chatMessage(msg);
86    connection->addPacketAll(packet);
87    return connection->sendPackets();
88  }
89 
90  /**
91   * Run this function once every tick
92   * calls processQueue and updateGamestate
93   */
94  void Server::tick(){
95    processQueue();
96    updateGamestate();
97    return;
98  }
99 
100  /**
101   * processes all the packets waiting in the queue
102   */
103  void Server::processQueue(){
104    ENetPacket *packet;
105    int clientID=-1;
106    while(!connection->queueEmpty()){
107      packet = connection->getPacket(clientID);
108      elaborate(packet, clientID);
109    }
110  }
111 
112  /**
113   * takes a new snapshot of the gamestate and sends it to the clients
114   */
115  void Server::updateGamestate(){
116    gamestates->update();
117    sendGameState();
118  }
119 
120  /**
121   * sends the gamestate
122   */
123  bool Server::sendGameState(){
124    return true;
125  }
126 
127 
128}
Note: See TracBrowser for help on using the repository browser.