File: NetEngine.h

package info (click to toggle)
glob2 0.9.4.4-5
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 34,824 kB
  • sloc: cpp: 89,685; python: 868; ansic: 259; sh: 49; makefile: 16
file content (105 lines) | stat: -rw-r--r-- 3,773 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
  Copyright (C) 2007 Bradley Arsenault

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef __NetEngine_h
#define __NetEngine_h

#include "Order.h"
#include <boost/shared_ptr.hpp>
#include <vector>
#include <queue>
#include "NetConnection.h"

///The purpose of this class is to sort Orders, and hand them out in
///the correct time slot. It serves partially to hide latency, Orders
///are set to execute a fixed number of ticks ahead, and this class
///handles that discrepency. It is used always, local or net games,
///as the central message pump for Orders.
class NetEngine
{
public:
	///Constructs the NetEngine
	NetEngine(int numberOfPlayers, int localPlayer, int networkOrderRate = 1, boost::shared_ptr<NetConnection> router = boost::shared_ptr<NetConnection>());

	///Sets the network game info
	void setNetworkInfo(int networkOrderRate, boost::shared_ptr<NetConnection> client);

	///Advances the step
	void advanceStep(Uint32 checksum);

	///Clears all the orders at the top of the queues
	void clearTopOrders();

	//Pushes an order to the NetEngine. AI's are special because they don't have padding arround orders
	void pushOrder(boost::shared_ptr<Order> order, int playerNumber, bool isAI);
	
	///Retrieves the order for the given player for this turn
	boost::shared_ptr<Order> retrieveOrder(int playerNumber);

	///Adds a order from the local player, which will be queued and sent across the network when needed
	void addLocalOrder(boost::shared_ptr<Order> order);
	
	///Tells whether the network is ready at the current tick. For
	///the network to be ready, all Orders from all players must be
	///present, otherwise it will have to hold for recieved Orders.
	bool allOrdersRecieved();
	
	///Returns the current step number
	int getStep();

	///Sends all pending orders across the network without a checksum. This is used if the game has to end immediettly
	void flushAllOrders();
	
	///Adds padding for the player for the given latency,
	///this is used because with latency, there aren't any
	///orders for the first few frames
	void prepareForLatency(int playerNumber, int latency);
	
	///Returns true if the given player has provided an order and is ready to go
	bool orderRecieved(int playerNumber);
	
	///Returns the mask representing each player that the NetEngine is waiting
	///on for this step
	Uint32 getWaitingOnMask();

	///Checks the checksums of all players for this step.
	///returns false if they don't match
	bool matchCheckSums();

	///This sends an order through the network that causes the latency adjustment to be increased
	void increaseLatencyAdjustment();
	
private:

	///This stores the queues with the orders from each player
	std::vector<std::vector<boost::shared_ptr<Order> > > orders;
	///This queue stores all of the local orders that have to be sent out
	///on their turn
	std::queue<boost::shared_ptr<Order> > outgoing;
	int step;
	int numberOfPlayers;
	///This count-downs steps until an order is sent across the network
	int localOrderSendCountdown;
	int localPlayer;
	boost::shared_ptr<NetConnection> router;
	int networkOrderRate;
	int currentLatency;
};


#endif