2010-03-27 08:09:34 +01:00
|
|
|
|
// ==============================================================
|
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
|
//
|
2010-04-22 01:13:39 +02:00
|
|
|
|
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
2010-03-27 08:09:34 +01:00
|
|
|
|
//
|
|
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
|
|
|
|
// License, or (at your option) any later version
|
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
|
|
#ifndef _GLEST_GAME_SERVERINTERFACE_H_
|
|
|
|
|
#define _GLEST_GAME_SERVERINTERFACE_H_
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "game_constants.h"
|
|
|
|
|
#include "network_interface.h"
|
|
|
|
|
#include "connection_slot.h"
|
|
|
|
|
#include "socket.h"
|
2010-09-07 07:25:40 +02:00
|
|
|
|
#include "leak_dumper.h"
|
2010-03-27 08:09:34 +01:00
|
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
using Shared::Platform::ServerSocket;
|
|
|
|
|
|
2010-12-29 02:10:53 +01:00
|
|
|
|
namespace Shared { namespace PlatformCommon { class FTPServerThread; }}
|
|
|
|
|
|
|
|
|
|
//using Shared::PlatformCommon::FTPServerThread;
|
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
2010-05-15 20:59:17 +02:00
|
|
|
|
// =====================================================
|
|
|
|
|
// class ServerInterface
|
|
|
|
|
// =====================================================
|
|
|
|
|
|
2011-01-07 07:21:23 +01:00
|
|
|
|
class ServerInterface: public GameNetworkInterface, public ConnectionSlotCallbackInterface, public SimpleTaskCallbackInterface, public FTPClientValidationInterface {
|
2010-03-27 08:09:34 +01:00
|
|
|
|
|
2011-01-11 23:09:46 +01:00
|
|
|
|
class TextMessageQueue {
|
|
|
|
|
public:
|
|
|
|
|
string text;
|
|
|
|
|
int teamIndex;
|
|
|
|
|
bool echoLocal;
|
|
|
|
|
};
|
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
|
private:
|
|
|
|
|
ConnectionSlot* slots[GameConstants::maxPlayers];
|
2010-12-18 20:52:47 +01:00
|
|
|
|
Mutex slotAccessorMutexes[GameConstants::maxPlayers];
|
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
|
ServerSocket serverSocket;
|
|
|
|
|
bool gameHasBeenInitiated;
|
|
|
|
|
int gameSettingsUpdateCount;
|
2010-04-29 01:59:37 +02:00
|
|
|
|
SwitchSetupRequest* switchSetupRequests[GameConstants::maxPlayers];
|
2010-05-15 20:59:17 +02:00
|
|
|
|
Mutex serverSynchAccessor;
|
2010-06-15 07:36:07 +02:00
|
|
|
|
int currentFrameCount;
|
2010-05-15 20:59:17 +02:00
|
|
|
|
|
2010-06-28 02:21:12 +02:00
|
|
|
|
time_t gameStartTime;
|
2010-03-27 08:09:34 +01:00
|
|
|
|
|
2010-10-23 11:06:47 +02:00
|
|
|
|
SimpleTaskThread *publishToMasterserverThread;
|
|
|
|
|
Mutex masterServerThreadAccessor;
|
|
|
|
|
time_t lastMasterserverHeartbeatTime;
|
2010-10-24 00:06:49 +02:00
|
|
|
|
bool needToRepublishToMasterserver;
|
2010-10-23 11:06:47 +02:00
|
|
|
|
|
2010-12-29 02:10:53 +01:00
|
|
|
|
Shared::PlatformCommon::FTPServerThread *ftpServer;
|
2011-01-10 16:50:16 +01:00
|
|
|
|
bool exitServer;
|
2011-01-11 09:45:58 +01:00
|
|
|
|
int64 nextEventId;
|
2010-12-29 02:10:53 +01:00
|
|
|
|
|
2011-01-11 23:09:46 +01:00
|
|
|
|
Mutex textMessageQueueThreadAccessor;
|
|
|
|
|
vector<TextMessageQueue> textMessageQueue;
|
|
|
|
|
|
2011-01-12 01:16:50 +01:00
|
|
|
|
Mutex broadcastMessageQueueThreadAccessor;
|
|
|
|
|
vector<pair<const NetworkMessage *,int> > broadcastMessageQueue;
|
|
|
|
|
|
2011-01-13 09:17:18 +01:00
|
|
|
|
Mutex inBroadcastMessageThreadAccessor;
|
|
|
|
|
bool inBroadcastMessage;
|
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
|
public:
|
2011-03-11 13:14:24 +01:00
|
|
|
|
ServerInterface(bool publishEnabled);
|
2010-03-27 08:09:34 +01:00
|
|
|
|
virtual ~ServerInterface();
|
|
|
|
|
|
|
|
|
|
virtual Socket* getSocket() {return &serverSocket;}
|
2010-10-24 00:06:49 +02:00
|
|
|
|
|
2011-01-13 02:46:32 +01:00
|
|
|
|
const virtual Socket *getSocket() const
|
|
|
|
|
{
|
|
|
|
|
return &serverSocket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void close();
|
|
|
|
|
virtual void update();
|
2011-01-13 09:17:18 +01:00
|
|
|
|
virtual void updateLobby() { };
|
2011-01-13 02:46:32 +01:00
|
|
|
|
virtual void updateKeyframe(int frameCount);
|
|
|
|
|
virtual void waitUntilReady(Checksum *checksum);
|
|
|
|
|
virtual void sendTextMessage(const string & text, int teamIndex, bool echoLocal = false);
|
|
|
|
|
void sendTextMessage(const string & text, int teamIndex, bool echoLocal, int lockedSlotIndex);
|
|
|
|
|
void queueTextMessage(const string & text, int teamIndex, bool echoLocal = false);
|
|
|
|
|
virtual void quitGame(bool userManuallyQuit);
|
|
|
|
|
virtual string getNetworkStatus();
|
|
|
|
|
ServerSocket *getServerSocket()
|
|
|
|
|
{
|
|
|
|
|
return &serverSocket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SwitchSetupRequest **getSwitchSetupRequests()
|
|
|
|
|
{
|
|
|
|
|
return &switchSetupRequests[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addSlot(int playerIndex);
|
|
|
|
|
bool switchSlot(int fromPlayerIndex, int toPlayerIndex);
|
|
|
|
|
void removeSlot(int playerIndex, int lockedSlotIndex = -1);
|
|
|
|
|
ConnectionSlot *getSlot(int playerIndex);
|
|
|
|
|
int getConnectedSlotCount();
|
|
|
|
|
int getOpenSlotCount();
|
|
|
|
|
bool launchGame(const GameSettings *gameSettings);
|
|
|
|
|
void setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck);
|
|
|
|
|
void broadcastGameSetup(const GameSettings *gameSettings);
|
|
|
|
|
void updateListen();
|
|
|
|
|
virtual bool getConnectHasHandshaked() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void slotUpdateTask(ConnectionSlotEvent *event);
|
|
|
|
|
bool hasClientConnection();
|
|
|
|
|
int getCurrentFrameCount() const
|
|
|
|
|
{
|
|
|
|
|
return currentFrameCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<bool,bool> clientLagCheck(ConnectionSlot *connectionSlot, bool skipNetworkBroadCast = false);
|
|
|
|
|
bool signalClientReceiveCommands(ConnectionSlot *connectionSlot, int slotIndex, bool socketTriggered, ConnectionSlotEvent & event);
|
|
|
|
|
void updateSocketTriggeredList(std::map<PLATFORM_SOCKET,bool> & socketTriggeredList);
|
|
|
|
|
bool isPortBound() const
|
|
|
|
|
{
|
|
|
|
|
return serverSocket.isPortBound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getBindPort() const
|
|
|
|
|
{
|
|
|
|
|
return serverSocket.getBindPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void broadcastPing(const NetworkMessagePing *networkMessage, int excludeSlot = -1)
|
|
|
|
|
{
|
|
|
|
|
this->broadcastMessage(networkMessage, excludeSlot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void queueBroadcastMessage(const NetworkMessage *networkMessage, int excludeSlot = -1);
|
|
|
|
|
virtual string getHumanPlayerName(int index = -1);
|
|
|
|
|
virtual int getHumanPlayerIndex() const;
|
|
|
|
|
bool getNeedToRepublishToMasterserver() const
|
|
|
|
|
{
|
|
|
|
|
return needToRepublishToMasterserver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setNeedToRepublishToMasterserver(bool value)
|
|
|
|
|
{
|
|
|
|
|
needToRepublishToMasterserver = value;
|
|
|
|
|
}
|
2010-05-21 18:36:08 +02:00
|
|
|
|
|
2011-03-11 13:14:24 +01:00
|
|
|
|
void setPublishEnabled(bool value);
|
|
|
|
|
|
2011-01-13 02:46:32 +01:00
|
|
|
|
public:
|
2011-01-13 09:17:18 +01:00
|
|
|
|
Mutex *getServerSynchAccessor() {
|
2011-01-13 02:46:32 +01:00
|
|
|
|
return &serverSynchAccessor;
|
|
|
|
|
}
|
2010-05-21 18:36:08 +02:00
|
|
|
|
|
2011-01-13 02:46:32 +01:00
|
|
|
|
virtual void simpleTask(BaseThread *callingThread);
|
|
|
|
|
void addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp);
|
2011-01-07 07:21:23 +01:00
|
|
|
|
virtual int isValidClientType(uint32 clientIp);
|
2011-03-11 13:14:24 +01:00
|
|
|
|
|
2010-03-27 08:09:34 +01:00
|
|
|
|
private:
|
2011-01-13 02:46:32 +01:00
|
|
|
|
void broadcastMessage(const NetworkMessage *networkMessage, int excludeSlot = -1, int lockedSlotIndex = -1);
|
|
|
|
|
void broadcastMessageToConnectedClients(const NetworkMessage *networkMessage, int excludeSlot = -1);
|
|
|
|
|
bool shouldDiscardNetworkMessage(NetworkMessageType networkMessageType, ConnectionSlot *connectionSlot);
|
|
|
|
|
void updateSlot(ConnectionSlotEvent *event);
|
|
|
|
|
void validateConnectedClients();
|
|
|
|
|
std::map<string,string> publishToMasterserver();
|
2011-01-11 09:45:58 +01:00
|
|
|
|
int64 getNextEventId();
|
2011-01-11 23:09:46 +01:00
|
|
|
|
void processTextMessageQueue();
|
2011-01-12 01:16:50 +01:00
|
|
|
|
void processBroadCastMessageQueue();
|
2011-01-13 02:46:32 +01:00
|
|
|
|
protected:
|
2011-01-13 09:17:18 +01:00
|
|
|
|
void signalClientsToRecieveData(std::map<PLATFORM_SOCKET,bool> & socketTriggeredList, std::map<int,ConnectionSlotEvent> & eventList, std::map<int,bool> & mapSlotSignalledList);
|
|
|
|
|
void checkForCompletedClients(std::map<int,bool> & mapSlotSignalledList,std::vector <string> &errorMsgList,std::map<int,ConnectionSlotEvent> &eventList);
|
|
|
|
|
void checForLaggingClients(std::map<int,bool> &mapSlotSignalledList, std::map<int,ConnectionSlotEvent> &eventList, std::map<PLATFORM_SOCKET,bool> &socketTriggeredList,std::vector <string> &errorMsgList);
|
|
|
|
|
void executeNetworkCommandsFromClients();
|
|
|
|
|
void dispatchPendingChatMessages(std::vector <string> &errorMsgList);
|
2010-03-27 08:09:34 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}}//end namespace
|
|
|
|
|
|
|
|
|
|
#endif
|