#ifndef FIGHTSUBSERVER_HPP_INCLUDED
#define FIGHTSUBSERVER_HPP_INCLUDED
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <vector>
#include <iostream>
#include "ServerClients.hpp"
#include "../src/operators.hpp"
#include "../src/AIControlledEntity.hpp"
#include "structures.hpp"
extern bool aboutToStop;
class FightSubserver
{
public:
friend int main();
FightSubserver():
m_clients(),
m_serverThread(&FightSubserver::threadFunction, this),
m_stopServer(0)
{
m_serverThread.launch();
}
~FightSubserver()
{
m_stopServer = true;
m_serverThread.wait();
delete m_teams[0];
delete m_teams[1];
}
bool isFull() const
{
if(m_clients[0].isFree() or m_clients[1].isFree())
{
return false;
}
else
{
return true;
}
}
void reset()
{
disconnect(true);
disconnect(false);
}
ServerClient* connect(sf::TcpListener& listener)
{
if (m_clients[0].isFree())
{
//Connection to first Socket, and verification that the client has the right version of client
sf::Packet versionPacket;
m_clients[0].connect(listener);
m_clients[0].receive(versionPacket);
versionPacket >> m_eventsStructure.versEv_number.status >> m_eventsStructure.versEv_number.major >> m_eventsStructure.versEv_number.minor;
if (m_eventsStructure.versEv_number.status == AutoVersion::SUPPORTED_STATUS
and m_eventsStructure.versEv_number.major == AutoVersion::SUPPORTED_MAJOR
and m_eventsStructure.versEv_number.minor == AutoVersion::SUPPORTED_MINOR)
{
m_teams[0] = new std::vector<EntityInformations>;
m_eventsStructure.isEventTreated = true;
m_clients[0].confirmConnection();
return &m_clients[0];
}
else
{
disconnect(true);
m_eventsStructure.isEventTreated = true;
return 0;
}
}
else if (m_clients[1].isFree())
{
//Connection to second Socket, and verification that the client has the right version of client
sf::Packet versionPacket;
m_clients[1].connect(listener);
m_clients[1].receive(versionPacket);
versionPacket >> m_eventsStructure.versEv_number.status >> m_eventsStructure.versEv_number.major >> m_eventsStructure.versEv_number.minor;
if (m_eventsStructure.versEv_number.status == AutoVersion::SUPPORTED_STATUS
and m_eventsStructure.versEv_number.major == AutoVersion::SUPPORTED_MAJOR
and m_eventsStructure.versEv_number.minor == AutoVersion::SUPPORTED_MINOR)
{
m_teams[1] = new std::vector<EntityInformations>;
m_eventsStructure.isEventTreated = true;
m_clients[1].confirmConnection();
return &m_clients[1];
}
else
{
disconnect(false);
m_eventsStructure.isEventTreated = true;
return 0;
}
}
else
{
ServerClient buffer;
buffer.connect(listener);
buffer.disconnect();
return 0;
}
}
ServerClient* disconnect(bool isClient1)
{
if(isClient1)
{
m_clients[0].disconnect();
delete m_teams[0];
return &m_clients[0];
}
else
{
m_clients[1].disconnect();
delete m_teams[1];
return &m_clients[1];
}
}
void newEvent(const InteractionInfos& eventInfos)
{
m_eventsStructure.typeOfEvent = FIGHT_INTERACTION;
m_eventsStructure.intEv_infos = eventInfos;
m_eventsStructure.isEventTreated = false;
}
void newEvent(const std::vector<EntityInformations>& teamInfo, bool isTeam1)
{
m_eventsStructure.typeOfEvent = TEAM_DATA;
m_eventsStructure.teamEv_infos = teamInfo;
m_eventsStructure.event_team1 = isTeam1;
m_eventsStructure.isEventTreated = false;
}
void newEvent(const VersionNumber& versionNumber, bool isTeam1)
{
m_eventsStructure.typeOfEvent = VERSION_NUMBER;
m_eventsStructure.versEv_number = versionNumber;
m_eventsStructure.event_team1 = isTeam1;
m_eventsStructure.isEventTreated = false;
}
bool isLastEventTreated()
{
return m_eventsStructure.isEventTreated;
}
ServerClient m_clients[2];
protected:
void threadFunction()
{
//Function who will assure the functionning of its own subserver
while (!m_stopServer)
{
if (!isFull())
{
sf::sleep(sf::seconds(1));
}
else
{
while (isFull() and !m_stopServer)
{
}
}
}
}
sf::Thread m_serverThread;
EventsStructure m_eventsStructure;
bool m_stopServer,
m_readyToLaunch;
InteractionInfos m_interactionToTreat;
std::vector<EntityInformations> m_receivedTeamData,
*m_teams[2];
SentInfosType m_typeOfPendingInfos;
};
bool findOpenSubserver(std::vector<FightSubserver*> serverVector, FightSubserver* returnedSubServer);
#endif // FIGHTSUBSERVER_HPP_INCLUDED