1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _GLEST_GAME_NETWORKINTERFACE_H_
13 #define _GLEST_GAME_NETWORKINTERFACE_H_
14 
15 #include <string>
16 #include <vector>
17 
18 #include "checksum.h"
19 #include "network_message.h"
20 #include "network_types.h"
21 
22 using std::string;
23 using std::vector;
24 using Shared::Util::Checksum;
25 
26 namespace Glest{ namespace Game{
27 
28 // =====================================================
29 //	class NetworkInterface
30 // =====================================================
31 
32 class NetworkInterface{
33 public:
34 	static const int readyWaitTimeout;
35 
36 public:
~NetworkInterface()37 	virtual ~NetworkInterface(){}
38 
39 	virtual Socket* getSocket()= 0;
40 	virtual const Socket* getSocket() const= 0;
41 
getIp()42 	string getIp() const		{return getSocket()->getIp();}
getHostName()43 	string getHostName() const	{return getSocket()->getHostName();}
44 
45 	void sendMessage(const NetworkMessage* networkMessage);
46 	NetworkMessageType getNextMessageType();
47 	bool receiveMessage(NetworkMessage* networkMessage);
48 
49 	bool isConnected();
50 };
51 
52 // =====================================================
53 //	class GameNetworkInterface
54 //
55 // Adds functions common to servers and clients
56 // but not connection slots
57 // =====================================================
58 
59 class GameNetworkInterface: public NetworkInterface{
60 private:
61 	typedef vector<NetworkCommand> Commands;
62 
63 protected:
64 	Commands requestedCommands;	//commands requested by the user
65 	Commands pendingCommands;	//commands ready to be given
66 	bool quit;
67 	string chatText;
68 	string chatSender;
69 	int chatTeamIndex;
70 
71 public:
72 	GameNetworkInterface();
73 
74 	//message processimg
75 	virtual void update()= 0;
76 	virtual void updateLobby()= 0;
77 	virtual void updateKeyframe(int frameCount)= 0;
78 	virtual void waitUntilReady(Checksum* checksum)= 0;
79 
80 	//message sending
81 	virtual void sendTextMessage(const string &text, int teamIndex)= 0;
82 	virtual void quitGame()=0;
83 
84 	//misc
85 	virtual string getNetworkStatus() const= 0;
86 
87 	//access functions
requestCommand(const NetworkCommand * networkCommand)88 	void requestCommand(const NetworkCommand *networkCommand)	{requestedCommands.push_back(*networkCommand);}
getPendingCommandCount()89 	int getPendingCommandCount() const							{return pendingCommands.size();}
getPendingCommand(int i)90 	const NetworkCommand* getPendingCommand(int i) const		{return &pendingCommands[i];}
clearPendingCommands()91 	void clearPendingCommands()									{pendingCommands.clear();}
getQuit()92 	bool getQuit() const										{return quit;}
getChatText()93 	const string getChatText() const							{return chatText;}
getChatSender()94 	const string getChatSender() const							{return chatSender;}
getChatTeamIndex()95 	int getChatTeamIndex() const								{return chatTeamIndex;}
96 };
97 
98 }}//end namespace
99 
100 #endif
101